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

Wed, 23 Sep 2009 18:48:13 -0700

author
jjg
date
Wed, 23 Sep 2009 18:48:13 -0700
changeset 415
49359d0e6a9c
parent 400
35e29f51a7c3
child 418
4776a869fdfa
permissions
-rw-r--r--

6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
6747645: ZipFileObject.getName is incorrectly deprecated
6885123: JavaFileObject getName issues
Reviewed-by: mcimadamore

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

mercurial