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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.file;
aoqi@0 27
aoqi@0 28 import java.io.File;
aoqi@0 29 import java.io.IOException;
aoqi@0 30 import java.io.InputStream;
aoqi@0 31 import java.io.OutputStream;
aoqi@0 32 import java.io.Writer;
aoqi@0 33 import java.net.URI;
aoqi@0 34 import java.nio.ByteBuffer;
aoqi@0 35 import java.nio.CharBuffer;
aoqi@0 36 import java.nio.charset.CharsetDecoder;
aoqi@0 37 import java.util.Enumeration;
aoqi@0 38 import java.util.HashMap;
aoqi@0 39 import java.util.Map;
aoqi@0 40 import java.util.Set;
aoqi@0 41 import java.util.zip.ZipEntry;
aoqi@0 42 import java.util.zip.ZipFile;
aoqi@0 43
aoqi@0 44 import javax.tools.JavaFileObject;
aoqi@0 45
aoqi@0 46 import com.sun.tools.javac.file.JavacFileManager.Archive;
aoqi@0 47 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
aoqi@0 48 import com.sun.tools.javac.file.RelativePath.RelativeFile;
aoqi@0 49 import com.sun.tools.javac.util.List;
aoqi@0 50 import java.lang.ref.Reference;
aoqi@0 51 import java.lang.ref.SoftReference;
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * <p><b>This is NOT part of any supported API.
aoqi@0 55 * If you write code that depends on this, you do so at your own risk.
aoqi@0 56 * This code and its internal interfaces are subject to change or
aoqi@0 57 * deletion without notice.</b>
aoqi@0 58 */
aoqi@0 59 public class ZipArchive implements Archive {
aoqi@0 60
aoqi@0 61 public ZipArchive(JavacFileManager fm, ZipFile zfile) throws IOException {
aoqi@0 62 this(fm, zfile, true);
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 protected ZipArchive(JavacFileManager fm, ZipFile zfile, boolean initMap) throws IOException {
aoqi@0 66 this.fileManager = fm;
aoqi@0 67 this.zfile = zfile;
aoqi@0 68 this.map = new HashMap<RelativeDirectory,List<String>>();
aoqi@0 69 if (initMap)
aoqi@0 70 initMap();
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 protected void initMap() throws IOException {
aoqi@0 74 for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements(); ) {
aoqi@0 75 ZipEntry entry;
aoqi@0 76 try {
aoqi@0 77 entry = e.nextElement();
aoqi@0 78 } catch (InternalError ex) {
aoqi@0 79 IOException io = new IOException();
aoqi@0 80 io.initCause(ex); // convenience constructors added in Mustang :-(
aoqi@0 81 throw io;
aoqi@0 82 }
aoqi@0 83 addZipEntry(entry);
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 void addZipEntry(ZipEntry entry) {
aoqi@0 88 String name = entry.getName();
aoqi@0 89 int i = name.lastIndexOf('/');
aoqi@0 90 RelativeDirectory dirname = new RelativeDirectory(name.substring(0, i+1));
aoqi@0 91 String basename = name.substring(i+1);
aoqi@0 92 if (basename.length() == 0)
aoqi@0 93 return;
aoqi@0 94 List<String> list = map.get(dirname);
aoqi@0 95 if (list == null)
aoqi@0 96 list = List.nil();
aoqi@0 97 list = list.prepend(basename);
aoqi@0 98 map.put(dirname, list);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 public boolean contains(RelativePath name) {
aoqi@0 102 RelativeDirectory dirname = name.dirname();
aoqi@0 103 String basename = name.basename();
aoqi@0 104 if (basename.length() == 0)
aoqi@0 105 return false;
aoqi@0 106 List<String> list = map.get(dirname);
aoqi@0 107 return (list != null && list.contains(basename));
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 public List<String> getFiles(RelativeDirectory subdirectory) {
aoqi@0 111 return map.get(subdirectory);
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
aoqi@0 115 ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zfile);
aoqi@0 116 return new ZipFileObject(this, file, ze);
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 public Set<RelativeDirectory> getSubdirectories() {
aoqi@0 120 return map.keySet();
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 public void close() throws IOException {
aoqi@0 124 zfile.close();
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 @Override
aoqi@0 128 public String toString() {
aoqi@0 129 return "ZipArchive[" + zfile.getName() + "]";
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 private File getAbsoluteFile() {
aoqi@0 133 File absFile = (absFileRef == null ? null : absFileRef.get());
aoqi@0 134 if (absFile == null) {
aoqi@0 135 absFile = new File(zfile.getName()).getAbsoluteFile();
aoqi@0 136 absFileRef = new SoftReference<File>(absFile);
aoqi@0 137 }
aoqi@0 138 return absFile;
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 /**
aoqi@0 142 * The file manager that created this archive.
aoqi@0 143 */
aoqi@0 144 protected JavacFileManager fileManager;
aoqi@0 145 /**
aoqi@0 146 * The index for the contents of this archive.
aoqi@0 147 */
aoqi@0 148 protected final Map<RelativeDirectory,List<String>> map;
aoqi@0 149 /**
aoqi@0 150 * The zip file for the archive.
aoqi@0 151 */
aoqi@0 152 protected final ZipFile zfile;
aoqi@0 153 /**
aoqi@0 154 * A reference to the absolute filename for the zip file for the archive.
aoqi@0 155 */
aoqi@0 156 protected Reference<File> absFileRef;
aoqi@0 157
aoqi@0 158 /**
aoqi@0 159 * A subclass of JavaFileObject representing zip entries.
aoqi@0 160 */
aoqi@0 161 public static class ZipFileObject extends BaseFileObject {
aoqi@0 162
aoqi@0 163 private String name;
aoqi@0 164 ZipArchive zarch;
aoqi@0 165 ZipEntry entry;
aoqi@0 166
aoqi@0 167 protected ZipFileObject(ZipArchive zarch, String name, ZipEntry entry) {
aoqi@0 168 super(zarch.fileManager);
aoqi@0 169 this.zarch = zarch;
aoqi@0 170 this.name = name;
aoqi@0 171 this.entry = entry;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 public URI toUri() {
aoqi@0 175 File zipFile = new File(zarch.zfile.getName());
aoqi@0 176 return createJarUri(zipFile, entry.getName());
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 @Override
aoqi@0 180 public String getName() {
aoqi@0 181 return zarch.zfile.getName() + "(" + entry.getName() + ")";
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 @Override
aoqi@0 185 public String getShortName() {
aoqi@0 186 return new File(zarch.zfile.getName()).getName() + "(" + entry + ")";
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 @Override
aoqi@0 190 public JavaFileObject.Kind getKind() {
aoqi@0 191 return getKind(entry.getName());
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 @Override
aoqi@0 195 public InputStream openInputStream() throws IOException {
aoqi@0 196 return zarch.zfile.getInputStream(entry);
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 @Override
aoqi@0 200 public OutputStream openOutputStream() throws IOException {
aoqi@0 201 throw new UnsupportedOperationException();
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 @Override
aoqi@0 205 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
aoqi@0 206 CharBuffer cb = fileManager.getCachedContent(this);
aoqi@0 207 if (cb == null) {
aoqi@0 208 InputStream in = zarch.zfile.getInputStream(entry);
aoqi@0 209 try {
aoqi@0 210 ByteBuffer bb = fileManager.makeByteBuffer(in);
aoqi@0 211 JavaFileObject prev = fileManager.log.useSource(this);
aoqi@0 212 try {
aoqi@0 213 cb = fileManager.decode(bb, ignoreEncodingErrors);
aoqi@0 214 } finally {
aoqi@0 215 fileManager.log.useSource(prev);
aoqi@0 216 }
aoqi@0 217 fileManager.recycleByteBuffer(bb);
aoqi@0 218 if (!ignoreEncodingErrors) {
aoqi@0 219 fileManager.cache(this, cb);
aoqi@0 220 }
aoqi@0 221 } finally {
aoqi@0 222 in.close();
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225 return cb;
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 @Override
aoqi@0 229 public Writer openWriter() throws IOException {
aoqi@0 230 throw new UnsupportedOperationException();
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 @Override
aoqi@0 234 public long getLastModified() {
aoqi@0 235 return entry.getTime();
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 @Override
aoqi@0 239 public boolean delete() {
aoqi@0 240 throw new UnsupportedOperationException();
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 @Override
aoqi@0 244 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
aoqi@0 245 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 @Override
aoqi@0 249 protected String inferBinaryName(Iterable<? extends File> path) {
aoqi@0 250 String entryName = entry.getName();
aoqi@0 251 return removeExtension(entryName).replace('/', '.');
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 @Override
aoqi@0 255 public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
aoqi@0 256 cn.getClass();
aoqi@0 257 // null check
aoqi@0 258 if (k == Kind.OTHER && getKind() != k) {
aoqi@0 259 return false;
aoqi@0 260 }
aoqi@0 261 return name.equals(cn + k.extension);
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 /**
aoqi@0 265 * Check if two file objects are equal.
aoqi@0 266 * Two ZipFileObjects are equal if the absolute paths of the underlying
aoqi@0 267 * zip files are equal and if the paths within those zip files are equal.
aoqi@0 268 */
aoqi@0 269 @Override
aoqi@0 270 public boolean equals(Object other) {
aoqi@0 271 if (this == other)
aoqi@0 272 return true;
aoqi@0 273
aoqi@0 274 if (!(other instanceof ZipFileObject))
aoqi@0 275 return false;
aoqi@0 276
aoqi@0 277 ZipFileObject o = (ZipFileObject) other;
aoqi@0 278 return zarch.getAbsoluteFile().equals(o.zarch.getAbsoluteFile())
aoqi@0 279 && name.equals(o.name);
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 @Override
aoqi@0 283 public int hashCode() {
aoqi@0 284 return zarch.getAbsoluteFile().hashCode() + name.hashCode();
aoqi@0 285 }
aoqi@0 286 }
aoqi@0 287
aoqi@0 288 }

mercurial