langtools/src/share/classes/com/sun/tools/javac/file/ZipArchive.java
changeset 4073 9788f4549740
parent 3998 c66be272f350
child 5520 86e4b9a9da40
--- a/langtools/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Tue Oct 13 15:26:30 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Wed Oct 14 15:41:28 2009 -0700
@@ -47,6 +47,8 @@
 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
 import com.sun.tools.javac.file.RelativePath.RelativeFile;
 import com.sun.tools.javac.util.List;
+import java.lang.ref.Reference;
+import java.lang.ref.SoftReference;
 
 /**
  * <p><b>This is NOT part of any API supported by Sun Microsystems.
@@ -56,20 +58,20 @@
  */
 public class ZipArchive implements Archive {
 
-    public ZipArchive(JavacFileManager fm, ZipFile zdir) throws IOException {
-        this(fm, zdir, true);
+    public ZipArchive(JavacFileManager fm, ZipFile zfile) throws IOException {
+        this(fm, zfile, true);
     }
 
-    protected ZipArchive(JavacFileManager fm, ZipFile zdir, boolean initMap) throws IOException {
+    protected ZipArchive(JavacFileManager fm, ZipFile zfile, boolean initMap) throws IOException {
         this.fileManager = fm;
-        this.zdir = zdir;
+        this.zfile = zfile;
         this.map = new HashMap<RelativeDirectory,List<String>>();
         if (initMap)
             initMap();
     }
 
     protected void initMap() throws IOException {
-        for (Enumeration<? extends ZipEntry> e = zdir.entries(); e.hasMoreElements(); ) {
+        for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements(); ) {
             ZipEntry entry;
             try {
                 entry = e.nextElement();
@@ -110,7 +112,7 @@
     }
 
     public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
-        ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zdir);
+        ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zfile);
         return new ZipFileObject(this, file, ze);
     }
 
@@ -119,17 +121,39 @@
     }
 
     public void close() throws IOException {
-        zdir.close();
+        zfile.close();
     }
 
     @Override
     public String toString() {
-        return "ZipArchive[" + zdir.getName() + "]";
+        return "ZipArchive[" + zfile.getName() + "]";
+    }
+
+    private File getAbsoluteFile() {
+        File absFile = (absFileRef == null ? null : absFileRef.get());
+        if (absFile == null) {
+            absFile = new File(zfile.getName()).getAbsoluteFile();
+            absFileRef = new SoftReference<File>(absFile);
+        }
+        return absFile;
     }
 
+    /**
+     * The file manager that created this archive.
+     */
     protected JavacFileManager fileManager;
+    /**
+     * The index for the contents of this archive.
+     */
     protected final Map<RelativeDirectory,List<String>> map;
-    protected final ZipFile zdir;
+    /**
+     * The zip file for the archive.
+     */
+    protected final ZipFile zfile;
+    /**
+     * A reference to the absolute filename for the zip file for the archive.
+     */
+    protected Reference<File> absFileRef;
 
     /**
      * A subclass of JavaFileObject representing zip entries.
@@ -148,18 +172,18 @@
         }
 
         public URI toUri() {
-            File zipFile = new File(zarch.zdir.getName());
+            File zipFile = new File(zarch.zfile.getName());
             return createJarUri(zipFile, entry.getName());
         }
 
         @Override
         public String getName() {
-            return zarch.zdir.getName() + "(" + entry.getName() + ")";
+            return zarch.zfile.getName() + "(" + entry.getName() + ")";
         }
 
         @Override
         public String getShortName() {
-            return new File(zarch.zdir.getName()).getName() + "(" + entry + ")";
+            return new File(zarch.zfile.getName()).getName() + "(" + entry + ")";
         }
 
         @Override
@@ -169,7 +193,7 @@
 
         @Override
         public InputStream openInputStream() throws IOException {
-            return zarch.zdir.getInputStream(entry);
+            return zarch.zfile.getInputStream(entry);
         }
 
         @Override
@@ -181,7 +205,7 @@
         public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
             CharBuffer cb = fileManager.getCachedContent(this);
             if (cb == null) {
-                InputStream in = zarch.zdir.getInputStream(entry);
+                InputStream in = zarch.zfile.getInputStream(entry);
                 try {
                     ByteBuffer bb = fileManager.makeByteBuffer(in);
                     JavaFileObject prev = fileManager.log.useSource(this);
@@ -237,18 +261,27 @@
             return name.equals(cn + k.extension);
         }
 
+        /**
+         * Check if two file objects are equal.
+         * Two ZipFileObjects are equal if the absolute paths of the underlying
+         * zip files are equal and if the paths within those zip files are equal.
+         */
         @Override
         public boolean equals(Object other) {
-            if (!(other instanceof ZipFileObject)) {
+            if (this == other)
+                return true;
+
+            if (!(other instanceof ZipFileObject))
                 return false;
-            }
+
             ZipFileObject o = (ZipFileObject) other;
-            return zarch.zdir.equals(o.zarch.zdir) || name.equals(o.name);
+            return zarch.getAbsoluteFile().equals(o.zarch.getAbsoluteFile())
+                    && name.equals(o.name);
         }
 
         @Override
         public int hashCode() {
-            return zarch.zdir.hashCode() + name.hashCode();
+            return zarch.getAbsoluteFile().hashCode() + name.hashCode();
         }
     }