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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,288 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.file;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.io.IOException;
    1.33 +import java.io.InputStream;
    1.34 +import java.io.OutputStream;
    1.35 +import java.io.Writer;
    1.36 +import java.net.URI;
    1.37 +import java.nio.ByteBuffer;
    1.38 +import java.nio.CharBuffer;
    1.39 +import java.nio.charset.CharsetDecoder;
    1.40 +import java.util.Enumeration;
    1.41 +import java.util.HashMap;
    1.42 +import java.util.Map;
    1.43 +import java.util.Set;
    1.44 +import java.util.zip.ZipEntry;
    1.45 +import java.util.zip.ZipFile;
    1.46 +
    1.47 +import javax.tools.JavaFileObject;
    1.48 +
    1.49 +import com.sun.tools.javac.file.JavacFileManager.Archive;
    1.50 +import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
    1.51 +import com.sun.tools.javac.file.RelativePath.RelativeFile;
    1.52 +import com.sun.tools.javac.util.List;
    1.53 +import java.lang.ref.Reference;
    1.54 +import java.lang.ref.SoftReference;
    1.55 +
    1.56 +/**
    1.57 + * <p><b>This is NOT part of any supported API.
    1.58 + * If you write code that depends on this, you do so at your own risk.
    1.59 + * This code and its internal interfaces are subject to change or
    1.60 + * deletion without notice.</b>
    1.61 + */
    1.62 +public class ZipArchive implements Archive {
    1.63 +
    1.64 +    public ZipArchive(JavacFileManager fm, ZipFile zfile) throws IOException {
    1.65 +        this(fm, zfile, true);
    1.66 +    }
    1.67 +
    1.68 +    protected ZipArchive(JavacFileManager fm, ZipFile zfile, boolean initMap) throws IOException {
    1.69 +        this.fileManager = fm;
    1.70 +        this.zfile = zfile;
    1.71 +        this.map = new HashMap<RelativeDirectory,List<String>>();
    1.72 +        if (initMap)
    1.73 +            initMap();
    1.74 +    }
    1.75 +
    1.76 +    protected void initMap() throws IOException {
    1.77 +        for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements(); ) {
    1.78 +            ZipEntry entry;
    1.79 +            try {
    1.80 +                entry = e.nextElement();
    1.81 +            } catch (InternalError ex) {
    1.82 +                IOException io = new IOException();
    1.83 +                io.initCause(ex); // convenience constructors added in Mustang :-(
    1.84 +                throw io;
    1.85 +            }
    1.86 +            addZipEntry(entry);
    1.87 +        }
    1.88 +    }
    1.89 +
    1.90 +    void addZipEntry(ZipEntry entry) {
    1.91 +        String name = entry.getName();
    1.92 +        int i = name.lastIndexOf('/');
    1.93 +        RelativeDirectory dirname = new RelativeDirectory(name.substring(0, i+1));
    1.94 +        String basename = name.substring(i+1);
    1.95 +        if (basename.length() == 0)
    1.96 +            return;
    1.97 +        List<String> list = map.get(dirname);
    1.98 +        if (list == null)
    1.99 +            list = List.nil();
   1.100 +        list = list.prepend(basename);
   1.101 +        map.put(dirname, list);
   1.102 +    }
   1.103 +
   1.104 +    public boolean contains(RelativePath name) {
   1.105 +        RelativeDirectory dirname = name.dirname();
   1.106 +        String basename = name.basename();
   1.107 +        if (basename.length() == 0)
   1.108 +            return false;
   1.109 +        List<String> list = map.get(dirname);
   1.110 +        return (list != null && list.contains(basename));
   1.111 +    }
   1.112 +
   1.113 +    public List<String> getFiles(RelativeDirectory subdirectory) {
   1.114 +        return map.get(subdirectory);
   1.115 +    }
   1.116 +
   1.117 +    public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
   1.118 +        ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zfile);
   1.119 +        return new ZipFileObject(this, file, ze);
   1.120 +    }
   1.121 +
   1.122 +    public Set<RelativeDirectory> getSubdirectories() {
   1.123 +        return map.keySet();
   1.124 +    }
   1.125 +
   1.126 +    public void close() throws IOException {
   1.127 +        zfile.close();
   1.128 +    }
   1.129 +
   1.130 +    @Override
   1.131 +    public String toString() {
   1.132 +        return "ZipArchive[" + zfile.getName() + "]";
   1.133 +    }
   1.134 +
   1.135 +    private File getAbsoluteFile() {
   1.136 +        File absFile = (absFileRef == null ? null : absFileRef.get());
   1.137 +        if (absFile == null) {
   1.138 +            absFile = new File(zfile.getName()).getAbsoluteFile();
   1.139 +            absFileRef = new SoftReference<File>(absFile);
   1.140 +        }
   1.141 +        return absFile;
   1.142 +    }
   1.143 +
   1.144 +    /**
   1.145 +     * The file manager that created this archive.
   1.146 +     */
   1.147 +    protected JavacFileManager fileManager;
   1.148 +    /**
   1.149 +     * The index for the contents of this archive.
   1.150 +     */
   1.151 +    protected final Map<RelativeDirectory,List<String>> map;
   1.152 +    /**
   1.153 +     * The zip file for the archive.
   1.154 +     */
   1.155 +    protected final ZipFile zfile;
   1.156 +    /**
   1.157 +     * A reference to the absolute filename for the zip file for the archive.
   1.158 +     */
   1.159 +    protected Reference<File> absFileRef;
   1.160 +
   1.161 +    /**
   1.162 +     * A subclass of JavaFileObject representing zip entries.
   1.163 +     */
   1.164 +    public static class ZipFileObject extends BaseFileObject {
   1.165 +
   1.166 +        private String name;
   1.167 +        ZipArchive zarch;
   1.168 +        ZipEntry entry;
   1.169 +
   1.170 +        protected ZipFileObject(ZipArchive zarch, String name, ZipEntry entry) {
   1.171 +            super(zarch.fileManager);
   1.172 +            this.zarch = zarch;
   1.173 +            this.name = name;
   1.174 +            this.entry = entry;
   1.175 +        }
   1.176 +
   1.177 +        public URI toUri() {
   1.178 +            File zipFile = new File(zarch.zfile.getName());
   1.179 +            return createJarUri(zipFile, entry.getName());
   1.180 +        }
   1.181 +
   1.182 +        @Override
   1.183 +        public String getName() {
   1.184 +            return zarch.zfile.getName() + "(" + entry.getName() + ")";
   1.185 +        }
   1.186 +
   1.187 +        @Override
   1.188 +        public String getShortName() {
   1.189 +            return new File(zarch.zfile.getName()).getName() + "(" + entry + ")";
   1.190 +        }
   1.191 +
   1.192 +        @Override
   1.193 +        public JavaFileObject.Kind getKind() {
   1.194 +            return getKind(entry.getName());
   1.195 +        }
   1.196 +
   1.197 +        @Override
   1.198 +        public InputStream openInputStream() throws IOException {
   1.199 +            return zarch.zfile.getInputStream(entry);
   1.200 +        }
   1.201 +
   1.202 +        @Override
   1.203 +        public OutputStream openOutputStream() throws IOException {
   1.204 +            throw new UnsupportedOperationException();
   1.205 +        }
   1.206 +
   1.207 +        @Override
   1.208 +        public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.209 +            CharBuffer cb = fileManager.getCachedContent(this);
   1.210 +            if (cb == null) {
   1.211 +                InputStream in = zarch.zfile.getInputStream(entry);
   1.212 +                try {
   1.213 +                    ByteBuffer bb = fileManager.makeByteBuffer(in);
   1.214 +                    JavaFileObject prev = fileManager.log.useSource(this);
   1.215 +                    try {
   1.216 +                        cb = fileManager.decode(bb, ignoreEncodingErrors);
   1.217 +                    } finally {
   1.218 +                        fileManager.log.useSource(prev);
   1.219 +                    }
   1.220 +                    fileManager.recycleByteBuffer(bb);
   1.221 +                    if (!ignoreEncodingErrors) {
   1.222 +                        fileManager.cache(this, cb);
   1.223 +                    }
   1.224 +                } finally {
   1.225 +                    in.close();
   1.226 +                }
   1.227 +            }
   1.228 +            return cb;
   1.229 +        }
   1.230 +
   1.231 +        @Override
   1.232 +        public Writer openWriter() throws IOException {
   1.233 +            throw new UnsupportedOperationException();
   1.234 +        }
   1.235 +
   1.236 +        @Override
   1.237 +        public long getLastModified() {
   1.238 +            return entry.getTime();
   1.239 +        }
   1.240 +
   1.241 +        @Override
   1.242 +        public boolean delete() {
   1.243 +            throw new UnsupportedOperationException();
   1.244 +        }
   1.245 +
   1.246 +        @Override
   1.247 +        protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
   1.248 +            return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
   1.249 +        }
   1.250 +
   1.251 +        @Override
   1.252 +        protected String inferBinaryName(Iterable<? extends File> path) {
   1.253 +            String entryName = entry.getName();
   1.254 +            return removeExtension(entryName).replace('/', '.');
   1.255 +        }
   1.256 +
   1.257 +        @Override
   1.258 +        public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
   1.259 +            cn.getClass();
   1.260 +            // null check
   1.261 +            if (k == Kind.OTHER && getKind() != k) {
   1.262 +                return false;
   1.263 +            }
   1.264 +            return name.equals(cn + k.extension);
   1.265 +        }
   1.266 +
   1.267 +        /**
   1.268 +         * Check if two file objects are equal.
   1.269 +         * Two ZipFileObjects are equal if the absolute paths of the underlying
   1.270 +         * zip files are equal and if the paths within those zip files are equal.
   1.271 +         */
   1.272 +        @Override
   1.273 +        public boolean equals(Object other) {
   1.274 +            if (this == other)
   1.275 +                return true;
   1.276 +
   1.277 +            if (!(other instanceof ZipFileObject))
   1.278 +                return false;
   1.279 +
   1.280 +            ZipFileObject o = (ZipFileObject) other;
   1.281 +            return zarch.getAbsoluteFile().equals(o.zarch.getAbsoluteFile())
   1.282 +                    && name.equals(o.name);
   1.283 +        }
   1.284 +
   1.285 +        @Override
   1.286 +        public int hashCode() {
   1.287 +            return zarch.getAbsoluteFile().hashCode() + name.hashCode();
   1.288 +        }
   1.289 +    }
   1.290 +
   1.291 +}

mercurial