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

Mon, 27 Jul 2009 19:52:42 -0700

author
jjg
date
Mon, 27 Jul 2009 19:52:42 -0700
changeset 333
7c2d6da61646
parent 103
e571266ae14f
child 400
35e29f51a7c3
permissions
-rw-r--r--

6865399: some javac files are missing Sun internal API comment
Reviewed-by: darcy

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@103 125 public String toString() {
jjg@103 126 return "ZipArchive[" + zdir.getName() + "]";
jjg@103 127 }
jjg@103 128
jjg@57 129 protected JavacFileManager fileManager;
jjg@103 130 protected final Map<RelativeDirectory,List<String>> map;
jjg@57 131 protected final ZipFile zdir;
jjg@57 132
jjg@57 133 /**
jjg@57 134 * A subclass of JavaFileObject representing zip entries.
jjg@57 135 */
jjg@57 136 public static class ZipFileObject extends BaseFileObject {
jjg@57 137
jjg@57 138 private String name;
jjg@57 139 ZipArchive zarch;
jjg@57 140 ZipEntry entry;
jjg@57 141
jjg@103 142 protected ZipFileObject(ZipArchive zarch, String name, ZipEntry entry) {
jjg@57 143 super(zarch.fileManager);
jjg@57 144 this.zarch = zarch;
jjg@57 145 this.name = name;
jjg@57 146 this.entry = entry;
jjg@57 147 }
jjg@57 148
jjg@57 149 public InputStream openInputStream() throws IOException {
jjg@57 150 return zarch.zdir.getInputStream(entry);
jjg@57 151 }
jjg@57 152
jjg@57 153 public OutputStream openOutputStream() throws IOException {
jjg@57 154 throw new UnsupportedOperationException();
jjg@57 155 }
jjg@57 156
jjg@57 157 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@57 158 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@57 159 }
jjg@57 160
jjg@57 161 public Writer openWriter() throws IOException {
jjg@57 162 throw new UnsupportedOperationException();
jjg@57 163 }
jjg@57 164
jjg@57 165 @Deprecated
jjg@57 166 public String getName() {
jjg@57 167 return name;
jjg@57 168 }
jjg@57 169
jjg@57 170 public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
jjg@57 171 cn.getClass();
jjg@57 172 // null check
jjg@57 173 if (k == Kind.OTHER && getKind() != k) {
jjg@57 174 return false;
jjg@57 175 }
jjg@57 176 return name.equals(cn + k.extension);
jjg@57 177 }
jjg@57 178
jjg@57 179 @Deprecated
jjg@57 180 public String getPath() {
jjg@57 181 return zarch.zdir.getName() + "(" + entry + ")";
jjg@57 182 }
jjg@57 183
jjg@57 184 public long getLastModified() {
jjg@57 185 return entry.getTime();
jjg@57 186 }
jjg@57 187
jjg@57 188 public boolean delete() {
jjg@57 189 throw new UnsupportedOperationException();
jjg@57 190 }
jjg@57 191
jjg@57 192 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 193 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 194 if (cb == null) {
jjg@57 195 InputStream in = zarch.zdir.getInputStream(entry);
jjg@57 196 try {
jjg@57 197 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 198 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 199 try {
jjg@57 200 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 201 } finally {
jjg@57 202 fileManager.log.useSource(prev);
jjg@57 203 }
jjg@57 204 fileManager.recycleByteBuffer(bb);
jjg@57 205 if (!ignoreEncodingErrors) {
jjg@57 206 fileManager.cache(this, cb);
jjg@57 207 }
jjg@57 208 } finally {
jjg@57 209 in.close();
jjg@57 210 }
jjg@57 211 }
jjg@57 212 return cb;
jjg@57 213 }
jjg@57 214
jjg@57 215 @Override
jjg@57 216 public boolean equals(Object other) {
jjg@57 217 if (!(other instanceof ZipFileObject)) {
jjg@57 218 return false;
jjg@57 219 }
jjg@57 220 ZipFileObject o = (ZipFileObject) other;
jjg@57 221 return zarch.zdir.equals(o.zarch.zdir) || name.equals(o.name);
jjg@57 222 }
jjg@57 223
jjg@57 224 @Override
jjg@57 225 public int hashCode() {
jjg@57 226 return zarch.zdir.hashCode() + name.hashCode();
jjg@57 227 }
jjg@57 228
jjg@57 229 public String getZipName() {
jjg@57 230 return zarch.zdir.getName();
jjg@57 231 }
jjg@57 232
jjg@57 233 public String getZipEntryName() {
jjg@57 234 return entry.getName();
jjg@57 235 }
jjg@57 236
jjg@57 237 public URI toUri() {
jjg@57 238 String zipName = new File(getZipName()).toURI().normalize().getPath();
jjg@57 239 String entryName = getZipEntryName();
jjg@57 240 return URI.create("jar:" + zipName + "!" + entryName);
jjg@57 241 }
jjg@57 242
jjg@57 243 @Override
jjg@57 244 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@57 245 String entryName = getZipEntryName();
jjg@57 246 return removeExtension(entryName).replace('/', '.');
jjg@57 247 }
jjg@57 248 }
jjg@57 249
jjg@57 250 }

mercurial