src/share/classes/com/sun/tools/javac/file/ZipArchive.java

changeset 424
86b773b7cb40
parent 418
4776a869fdfa
child 554
9d9f26857129
     1.1 --- a/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Tue Oct 13 15:26:30 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Wed Oct 14 15:41:28 2009 -0700
     1.3 @@ -47,6 +47,8 @@
     1.4  import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
     1.5  import com.sun.tools.javac.file.RelativePath.RelativeFile;
     1.6  import com.sun.tools.javac.util.List;
     1.7 +import java.lang.ref.Reference;
     1.8 +import java.lang.ref.SoftReference;
     1.9  
    1.10  /**
    1.11   * <p><b>This is NOT part of any API supported by Sun Microsystems.
    1.12 @@ -56,20 +58,20 @@
    1.13   */
    1.14  public class ZipArchive implements Archive {
    1.15  
    1.16 -    public ZipArchive(JavacFileManager fm, ZipFile zdir) throws IOException {
    1.17 -        this(fm, zdir, true);
    1.18 +    public ZipArchive(JavacFileManager fm, ZipFile zfile) throws IOException {
    1.19 +        this(fm, zfile, true);
    1.20      }
    1.21  
    1.22 -    protected ZipArchive(JavacFileManager fm, ZipFile zdir, boolean initMap) throws IOException {
    1.23 +    protected ZipArchive(JavacFileManager fm, ZipFile zfile, boolean initMap) throws IOException {
    1.24          this.fileManager = fm;
    1.25 -        this.zdir = zdir;
    1.26 +        this.zfile = zfile;
    1.27          this.map = new HashMap<RelativeDirectory,List<String>>();
    1.28          if (initMap)
    1.29              initMap();
    1.30      }
    1.31  
    1.32      protected void initMap() throws IOException {
    1.33 -        for (Enumeration<? extends ZipEntry> e = zdir.entries(); e.hasMoreElements(); ) {
    1.34 +        for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements(); ) {
    1.35              ZipEntry entry;
    1.36              try {
    1.37                  entry = e.nextElement();
    1.38 @@ -110,7 +112,7 @@
    1.39      }
    1.40  
    1.41      public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
    1.42 -        ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zdir);
    1.43 +        ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zfile);
    1.44          return new ZipFileObject(this, file, ze);
    1.45      }
    1.46  
    1.47 @@ -119,17 +121,39 @@
    1.48      }
    1.49  
    1.50      public void close() throws IOException {
    1.51 -        zdir.close();
    1.52 +        zfile.close();
    1.53      }
    1.54  
    1.55      @Override
    1.56      public String toString() {
    1.57 -        return "ZipArchive[" + zdir.getName() + "]";
    1.58 +        return "ZipArchive[" + zfile.getName() + "]";
    1.59      }
    1.60  
    1.61 +    private File getAbsoluteFile() {
    1.62 +        File absFile = (absFileRef == null ? null : absFileRef.get());
    1.63 +        if (absFile == null) {
    1.64 +            absFile = new File(zfile.getName()).getAbsoluteFile();
    1.65 +            absFileRef = new SoftReference<File>(absFile);
    1.66 +        }
    1.67 +        return absFile;
    1.68 +    }
    1.69 +
    1.70 +    /**
    1.71 +     * The file manager that created this archive.
    1.72 +     */
    1.73      protected JavacFileManager fileManager;
    1.74 +    /**
    1.75 +     * The index for the contents of this archive.
    1.76 +     */
    1.77      protected final Map<RelativeDirectory,List<String>> map;
    1.78 -    protected final ZipFile zdir;
    1.79 +    /**
    1.80 +     * The zip file for the archive.
    1.81 +     */
    1.82 +    protected final ZipFile zfile;
    1.83 +    /**
    1.84 +     * A reference to the absolute filename for the zip file for the archive.
    1.85 +     */
    1.86 +    protected Reference<File> absFileRef;
    1.87  
    1.88      /**
    1.89       * A subclass of JavaFileObject representing zip entries.
    1.90 @@ -148,18 +172,18 @@
    1.91          }
    1.92  
    1.93          public URI toUri() {
    1.94 -            File zipFile = new File(zarch.zdir.getName());
    1.95 +            File zipFile = new File(zarch.zfile.getName());
    1.96              return createJarUri(zipFile, entry.getName());
    1.97          }
    1.98  
    1.99          @Override
   1.100          public String getName() {
   1.101 -            return zarch.zdir.getName() + "(" + entry.getName() + ")";
   1.102 +            return zarch.zfile.getName() + "(" + entry.getName() + ")";
   1.103          }
   1.104  
   1.105          @Override
   1.106          public String getShortName() {
   1.107 -            return new File(zarch.zdir.getName()).getName() + "(" + entry + ")";
   1.108 +            return new File(zarch.zfile.getName()).getName() + "(" + entry + ")";
   1.109          }
   1.110  
   1.111          @Override
   1.112 @@ -169,7 +193,7 @@
   1.113  
   1.114          @Override
   1.115          public InputStream openInputStream() throws IOException {
   1.116 -            return zarch.zdir.getInputStream(entry);
   1.117 +            return zarch.zfile.getInputStream(entry);
   1.118          }
   1.119  
   1.120          @Override
   1.121 @@ -181,7 +205,7 @@
   1.122          public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.123              CharBuffer cb = fileManager.getCachedContent(this);
   1.124              if (cb == null) {
   1.125 -                InputStream in = zarch.zdir.getInputStream(entry);
   1.126 +                InputStream in = zarch.zfile.getInputStream(entry);
   1.127                  try {
   1.128                      ByteBuffer bb = fileManager.makeByteBuffer(in);
   1.129                      JavaFileObject prev = fileManager.log.useSource(this);
   1.130 @@ -237,18 +261,27 @@
   1.131              return name.equals(cn + k.extension);
   1.132          }
   1.133  
   1.134 +        /**
   1.135 +         * Check if two file objects are equal.
   1.136 +         * Two ZipFileObjects are equal if the absolute paths of the underlying
   1.137 +         * zip files are equal and if the paths within those zip files are equal.
   1.138 +         */
   1.139          @Override
   1.140          public boolean equals(Object other) {
   1.141 -            if (!(other instanceof ZipFileObject)) {
   1.142 +            if (this == other)
   1.143 +                return true;
   1.144 +
   1.145 +            if (!(other instanceof ZipFileObject))
   1.146                  return false;
   1.147 -            }
   1.148 +
   1.149              ZipFileObject o = (ZipFileObject) other;
   1.150 -            return zarch.zdir.equals(o.zarch.zdir) || name.equals(o.name);
   1.151 +            return zarch.getAbsoluteFile().equals(o.zarch.getAbsoluteFile())
   1.152 +                    && name.equals(o.name);
   1.153          }
   1.154  
   1.155          @Override
   1.156          public int hashCode() {
   1.157 -            return zarch.zdir.hashCode() + name.hashCode();
   1.158 +            return zarch.getAbsoluteFile().hashCode() + name.hashCode();
   1.159          }
   1.160      }
   1.161  

mercurial