src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.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@57 28 import java.io.IOException;
jjg@57 29 import java.util.Set;
jjg@57 30 import javax.tools.JavaFileObject;
jjg@57 31
jjg@57 32 import java.io.ByteArrayInputStream;
jjg@57 33 import java.io.File;
jjg@57 34 import java.io.InputStream;
jjg@57 35 import java.io.OutputStream;
jjg@57 36 import java.io.Writer;
jjg@57 37 import java.net.URI;
jjg@57 38 import java.nio.ByteBuffer;
jjg@57 39 import java.nio.CharBuffer;
jjg@57 40 import java.nio.charset.CharsetDecoder;
jjg@57 41
jjg@103 42 import com.sun.tools.javac.file.JavacFileManager.Archive;
jjg@103 43 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
jjg@103 44 import com.sun.tools.javac.file.RelativePath.RelativeFile;
jjg@103 45 import com.sun.tools.javac.util.List;
jjg@103 46
jjg@333 47 /**
jjg@333 48 * <p><b>This is NOT part of any API supported by Sun Microsystems.
jjg@333 49 * If you write code that depends on this, you do so at your own risk.
jjg@333 50 * This code and its internal interfaces are subject to change or
jjg@333 51 * deletion without notice.</b>
jjg@333 52 */
jjg@57 53 public class ZipFileIndexArchive implements Archive {
jjg@57 54
jjg@57 55 private final ZipFileIndex zfIndex;
jjg@57 56 private JavacFileManager fileManager;
jjg@57 57
jjg@57 58 public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException {
jjg@57 59 super();
jjg@57 60 this.fileManager = fileManager;
jjg@57 61 this.zfIndex = zdir;
jjg@57 62 }
jjg@57 63
jjg@103 64 public boolean contains(RelativePath name) {
jjg@57 65 return zfIndex.contains(name);
jjg@57 66 }
jjg@57 67
jjg@103 68 public List<String> getFiles(RelativeDirectory subdirectory) {
jjg@103 69 return zfIndex.getFiles(subdirectory);
jjg@57 70 }
jjg@57 71
jjg@103 72 public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
jjg@103 73 RelativeFile fullZipFileName = new RelativeFile(subdirectory, file);
jjg@57 74 ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
jjg@57 75 JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath());
jjg@57 76 return ret;
jjg@57 77 }
jjg@57 78
jjg@103 79 public Set<RelativeDirectory> getSubdirectories() {
jjg@57 80 return zfIndex.getAllDirectories();
jjg@57 81 }
jjg@57 82
jjg@57 83 public void close() throws IOException {
jjg@57 84 zfIndex.close();
jjg@57 85 }
jjg@57 86
jjg@103 87 public String toString() {
jjg@103 88 return "ZipFileIndexArchive[" + zfIndex + "]";
jjg@103 89 }
jjg@103 90
jjg@57 91 /**
jjg@57 92 * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation.
jjg@57 93 */
jjg@57 94 public static class ZipFileIndexFileObject extends BaseFileObject {
jjg@57 95
jjg@57 96 /** The entry's name.
jjg@57 97 */
jjg@57 98 private String name;
jjg@57 99
jjg@57 100 /** The zipfile containing the entry.
jjg@57 101 */
jjg@57 102 ZipFileIndex zfIndex;
jjg@57 103
jjg@57 104 /** The underlying zip entry object.
jjg@57 105 */
jjg@57 106 ZipFileIndex.Entry entry;
jjg@57 107
jjg@57 108 /** The InputStream for this zip entry (file.)
jjg@57 109 */
jjg@57 110 InputStream inputStream = null;
jjg@57 111
jjg@57 112 /** The name of the zip file where this entry resides.
jjg@57 113 */
jjg@57 114 String zipName;
jjg@57 115
jjg@57 116
jjg@57 117 ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, String zipFileName) {
jjg@57 118 super(fileManager);
jjg@57 119 this.name = entry.getFileName();
jjg@57 120 this.zfIndex = zfIndex;
jjg@57 121 this.entry = entry;
jjg@57 122 this.zipName = zipFileName;
jjg@57 123 }
jjg@57 124
jjg@57 125 public InputStream openInputStream() throws IOException {
jjg@57 126
jjg@57 127 if (inputStream == null) {
jjg@57 128 inputStream = new ByteArrayInputStream(read());
jjg@57 129 }
jjg@57 130 return inputStream;
jjg@57 131 }
jjg@57 132
jjg@57 133 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@57 134 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@57 135 }
jjg@57 136
jjg@57 137 public OutputStream openOutputStream() throws IOException {
jjg@57 138 throw new UnsupportedOperationException();
jjg@57 139 }
jjg@57 140
jjg@57 141 public Writer openWriter() throws IOException {
jjg@57 142 throw new UnsupportedOperationException();
jjg@57 143 }
jjg@57 144
jjg@57 145 /** @deprecated see bug 6410637 */
jjg@57 146 @Deprecated
jjg@57 147 public String getName() {
jjg@57 148 return name;
jjg@57 149 }
jjg@57 150
jjg@57 151 public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
jjg@57 152 cn.getClass(); // null check
jjg@57 153 if (k == Kind.OTHER && getKind() != k)
jjg@57 154 return false;
jjg@57 155 return name.equals(cn + k.extension);
jjg@57 156 }
jjg@57 157
jjg@57 158 /** @deprecated see bug 6410637 */
jjg@57 159 @Deprecated
jjg@57 160 public String getPath() {
jjg@57 161 return zipName + "(" + entry.getName() + ")";
jjg@57 162 }
jjg@57 163
jjg@57 164 public long getLastModified() {
jjg@57 165 return entry.getLastModified();
jjg@57 166 }
jjg@57 167
jjg@57 168 public boolean delete() {
jjg@57 169 throw new UnsupportedOperationException();
jjg@57 170 }
jjg@57 171
jjg@57 172 @Override
jjg@57 173 public boolean equals(Object other) {
jjg@57 174 if (!(other instanceof ZipFileIndexFileObject))
jjg@57 175 return false;
jjg@57 176 ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
jjg@57 177 return entry.equals(o.entry);
jjg@57 178 }
jjg@57 179
jjg@57 180 @Override
jjg@57 181 public int hashCode() {
jjg@57 182 return zipName.hashCode() + (name.hashCode() << 10);
jjg@57 183 }
jjg@57 184
jjg@57 185 public String getZipName() {
jjg@57 186 return zipName;
jjg@57 187 }
jjg@57 188
jjg@57 189 public String getZipEntryName() {
jjg@57 190 return entry.getName();
jjg@57 191 }
jjg@57 192
jjg@57 193 public URI toUri() {
jjg@57 194 String zipName = new File(getZipName()).toURI().normalize().getPath();
jjg@57 195 String entryName = getZipEntryName();
jjg@57 196 return URI.create("jar:" + zipName + "!" + entryName);
jjg@57 197 }
jjg@57 198
jjg@57 199 private byte[] read() throws IOException {
jjg@103 200 assert entry != null; // see constructor
jjg@57 201 return zfIndex.read(entry);
jjg@57 202 }
jjg@57 203
jjg@57 204 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 205 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 206 if (cb == null) {
jjg@57 207 InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
jjg@57 208 try {
jjg@57 209 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 210 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 211 try {
jjg@57 212 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 213 } finally {
jjg@57 214 fileManager.log.useSource(prev);
jjg@57 215 }
jjg@57 216 fileManager.recycleByteBuffer(bb); // save for next time
jjg@57 217 if (!ignoreEncodingErrors)
jjg@57 218 fileManager.cache(this, cb);
jjg@57 219 } finally {
jjg@57 220 in.close();
jjg@57 221 }
jjg@57 222 }
jjg@57 223 return cb;
jjg@57 224 }
jjg@57 225
jjg@57 226 @Override
jjg@57 227 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@57 228 String entryName = getZipEntryName();
jjg@57 229 if (zfIndex.symbolFilePrefix != null) {
jjg@103 230 String prefix = zfIndex.symbolFilePrefix.path;
jjg@57 231 if (entryName.startsWith(prefix))
jjg@57 232 entryName = entryName.substring(prefix.length());
jjg@57 233 }
jjg@103 234 return removeExtension(entryName).replace('/', '.');
jjg@57 235 }
jjg@57 236 }
jjg@57 237
jjg@57 238 }

mercurial