langtools/src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java
changeset 22163 3651128c74eb
parent 14964 391288e42c67
equal deleted inserted replaced
22162:3b3e23e67329 22163:3651128c74eb
   108     private final boolean usePreindexedCache;
   108     private final boolean usePreindexedCache;
   109     private final String preindexedCacheLocation;
   109     private final String preindexedCacheLocation;
   110 
   110 
   111     private boolean writeIndex = false;
   111     private boolean writeIndex = false;
   112 
   112 
   113     private Map<String, SoftReference<RelativeDirectory>> relativeDirectoryCache =
   113     private Map<String, SoftReference<RelativeDirectory>> relativeDirectoryCache = new HashMap<>();
   114             new HashMap<String, SoftReference<RelativeDirectory>>();
       
   115 
   114 
   116 
   115 
   117     public synchronized boolean isOpen() {
   116     public synchronized boolean isOpen() {
   118         return (zipRandomFile != null);
   117         return (zipRandomFile != null);
   119     }
   118     }
   282 
   281 
   283     public synchronized Set<RelativeDirectory> getAllDirectories() {
   282     public synchronized Set<RelativeDirectory> getAllDirectories() {
   284         try {
   283         try {
   285             checkIndex();
   284             checkIndex();
   286             if (allDirs == Collections.EMPTY_SET) {
   285             if (allDirs == Collections.EMPTY_SET) {
   287                 allDirs = new java.util.LinkedHashSet<RelativeDirectory>(directories.keySet());
   286                 allDirs = new java.util.LinkedHashSet<>(directories.keySet());
   288             }
   287             }
   289 
   288 
   290             return allDirs;
   289             return allDirs;
   291         }
   290         }
   292         catch (IOException e) {
   291         catch (IOException e) {
   447     private int inflate(byte[] src, byte[] dest) {
   446     private int inflate(byte[] src, byte[] dest) {
   448         Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());
   447         Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());
   449 
   448 
   450         // construct the inflater object or reuse an existing one
   449         // construct the inflater object or reuse an existing one
   451         if (inflater == null)
   450         if (inflater == null)
   452             inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));
   451             inflaterRef = new SoftReference<>(inflater = new Inflater(true));
   453 
   452 
   454         inflater.reset();
   453         inflater.reset();
   455         inflater.setInput(src);
   454         inflater.setInput(src);
   456         try {
   455         try {
   457             return inflater.inflate(dest);
   456             return inflater.inflate(dest);
   568         private void buildIndex() throws IOException {
   567         private void buildIndex() throws IOException {
   569             int len = zipDir.length;
   568             int len = zipDir.length;
   570 
   569 
   571             // Add each of the files
   570             // Add each of the files
   572             if (len > 0) {
   571             if (len > 0) {
   573                 directories = new LinkedHashMap<RelativeDirectory, DirectoryEntry>();
   572                 directories = new LinkedHashMap<>();
   574                 ArrayList<Entry> entryList = new ArrayList<Entry>();
   573                 ArrayList<Entry> entryList = new ArrayList<>();
   575                 for (int pos = 0; pos < len; ) {
   574                 for (int pos = 0; pos < len; ) {
   576                     pos = readEntry(pos, entryList, directories);
   575                     pos = readEntry(pos, entryList, directories);
   577                 }
   576                 }
   578 
   577 
   579                 // Add the accumulated dirs into the same list
   578                 // Add the accumulated dirs into the same list
   706 
   705 
   707         private com.sun.tools.javac.util.List<String> zipFileEntriesFiles = com.sun.tools.javac.util.List.<String>nil();
   706         private com.sun.tools.javac.util.List<String> zipFileEntriesFiles = com.sun.tools.javac.util.List.<String>nil();
   708         private com.sun.tools.javac.util.List<String> zipFileEntriesDirectories = com.sun.tools.javac.util.List.<String>nil();
   707         private com.sun.tools.javac.util.List<String> zipFileEntriesDirectories = com.sun.tools.javac.util.List.<String>nil();
   709         private com.sun.tools.javac.util.List<Entry>  zipFileEntries = com.sun.tools.javac.util.List.<Entry>nil();
   708         private com.sun.tools.javac.util.List<Entry>  zipFileEntries = com.sun.tools.javac.util.List.<Entry>nil();
   710 
   709 
   711         private List<Entry> entries = new ArrayList<Entry>();
   710         private List<Entry> entries = new ArrayList<>();
   712 
   711 
   713         private ZipFileIndex zipFileIndex;
   712         private ZipFileIndex zipFileIndex;
   714 
   713 
   715         private int numEntries;
   714         private int numEntries;
   716 
   715 
   862 
   861 
   863                 long fileStamp = raf.readLong();
   862                 long fileStamp = raf.readLong();
   864                 if (zipFile.lastModified() != fileStamp) {
   863                 if (zipFile.lastModified() != fileStamp) {
   865                     ret = false;
   864                     ret = false;
   866                 } else {
   865                 } else {
   867                     directories = new LinkedHashMap<RelativeDirectory, DirectoryEntry>();
   866                     directories = new LinkedHashMap<>();
   868                     int numDirs = raf.readInt();
   867                     int numDirs = raf.readInt();
   869                     for (int nDirs = 0; nDirs < numDirs; nDirs++) {
   868                     for (int nDirs = 0; nDirs < numDirs; nDirs++) {
   870                         int dirNameBytesLen = raf.readInt();
   869                         int dirNameBytesLen = raf.readInt();
   871                         byte [] dirNameBytes = new byte[dirNameBytesLen];
   870                         byte [] dirNameBytes = new byte[dirNameBytesLen];
   872                         raf.read(dirNameBytes);
   871                         raf.read(dirNameBytes);
   920             raf = new RandomAccessFile(indexFile, "rw");
   919             raf = new RandomAccessFile(indexFile, "rw");
   921 
   920 
   922             raf.writeLong(zipFileLastModified);
   921             raf.writeLong(zipFileLastModified);
   923             writtenSoFar += 8;
   922             writtenSoFar += 8;
   924 
   923 
   925             List<DirectoryEntry> directoriesToWrite = new ArrayList<DirectoryEntry>();
   924             List<DirectoryEntry> directoriesToWrite = new ArrayList<>();
   926             Map<RelativeDirectory, Long> offsets = new HashMap<RelativeDirectory, Long>();
   925             Map<RelativeDirectory, Long> offsets = new HashMap<>();
   927             raf.writeInt(directories.keySet().size());
   926             raf.writeInt(directories.keySet().size());
   928             writtenSoFar += 4;
   927             writtenSoFar += 4;
   929 
   928 
   930             for (RelativeDirectory dirName: directories.keySet()) {
   929             for (RelativeDirectory dirName: directories.keySet()) {
   931                 DirectoryEntry dirEntry = directories.get(dirName);
   930                 DirectoryEntry dirEntry = directories.get(dirName);
  1036 
  1035 
  1037     File getAbsoluteFile() {
  1036     File getAbsoluteFile() {
  1038         File absFile = (absFileRef == null ? null : absFileRef.get());
  1037         File absFile = (absFileRef == null ? null : absFileRef.get());
  1039         if (absFile == null) {
  1038         if (absFile == null) {
  1040             absFile = zipFile.getAbsoluteFile();
  1039             absFile = zipFile.getAbsoluteFile();
  1041             absFileRef = new SoftReference<File>(absFile);
  1040             absFileRef = new SoftReference<>(absFile);
  1042         }
  1041         }
  1043         return absFile;
  1042         return absFile;
  1044     }
  1043     }
  1045 
  1044 
  1046     private RelativeDirectory getRelativeDirectory(String path) {
  1045     private RelativeDirectory getRelativeDirectory(String path) {
  1050             rd = ref.get();
  1049             rd = ref.get();
  1051             if (rd != null)
  1050             if (rd != null)
  1052                 return rd;
  1051                 return rd;
  1053         }
  1052         }
  1054         rd = new RelativeDirectory(path);
  1053         rd = new RelativeDirectory(path);
  1055         relativeDirectoryCache.put(path, new SoftReference<RelativeDirectory>(rd));
  1054         relativeDirectoryCache.put(path, new SoftReference<>(rd));
  1056         return rd;
  1055         return rd;
  1057     }
  1056     }
  1058 
  1057 
  1059     static class Entry implements Comparable<Entry> {
  1058     static class Entry implements Comparable<Entry> {
  1060         public static final Entry[] EMPTY_ARRAY = {};
  1059         public static final Entry[] EMPTY_ARRAY = {};