src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.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@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@400 75 JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile());
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@400 87 @Override
jjg@103 88 public String toString() {
jjg@103 89 return "ZipFileIndexArchive[" + zfIndex + "]";
jjg@103 90 }
jjg@103 91
jjg@57 92 /**
jjg@57 93 * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation.
jjg@57 94 */
jjg@57 95 public static class ZipFileIndexFileObject extends BaseFileObject {
jjg@57 96
jjg@57 97 /** The entry's name.
jjg@57 98 */
jjg@57 99 private String name;
jjg@57 100
jjg@57 101 /** The zipfile containing the entry.
jjg@57 102 */
jjg@57 103 ZipFileIndex zfIndex;
jjg@57 104
jjg@57 105 /** The underlying zip entry object.
jjg@57 106 */
jjg@57 107 ZipFileIndex.Entry entry;
jjg@57 108
jjg@57 109 /** The InputStream for this zip entry (file.)
jjg@57 110 */
jjg@57 111 InputStream inputStream = null;
jjg@57 112
jjg@57 113 /** The name of the zip file where this entry resides.
jjg@57 114 */
jjg@400 115 File zipName;
jjg@57 116
jjg@57 117
jjg@400 118 ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, File zipFileName) {
jjg@57 119 super(fileManager);
jjg@57 120 this.name = entry.getFileName();
jjg@57 121 this.zfIndex = zfIndex;
jjg@57 122 this.entry = entry;
jjg@57 123 this.zipName = zipFileName;
jjg@57 124 }
jjg@57 125
jjg@415 126 @Override
jjg@415 127 public URI toUri() {
jjg@415 128 return createJarUri(zipName, getPrefixedEntryName());
jjg@415 129 }
jjg@415 130
jjg@415 131 @Override
jjg@415 132 public String getName() {
jjg@415 133 return zipName + "(" + getPrefixedEntryName() + ")";
jjg@415 134 }
jjg@415 135
jjg@415 136 @Override
jjg@415 137 public String getShortName() {
jjg@415 138 return zipName.getName() + "(" + entry.getName() + ")";
jjg@415 139 }
jjg@415 140
jjg@415 141 @Override
jjg@415 142 public JavaFileObject.Kind getKind() {
jjg@415 143 return getKind(entry.getName());
jjg@415 144 }
jjg@415 145
jjg@415 146 @Override
jjg@57 147 public InputStream openInputStream() throws IOException {
jjg@57 148 if (inputStream == null) {
jjg@415 149 assert entry != null; // see constructor
jjg@415 150 inputStream = new ByteArrayInputStream(zfIndex.read(entry));
jjg@57 151 }
jjg@57 152 return inputStream;
jjg@57 153 }
jjg@57 154
jjg@400 155 @Override
jjg@57 156 public OutputStream openOutputStream() throws IOException {
jjg@57 157 throw new UnsupportedOperationException();
jjg@57 158 }
jjg@57 159
jjg@400 160 @Override
jjg@57 161 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 162 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 163 if (cb == null) {
jjg@57 164 InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
jjg@57 165 try {
jjg@57 166 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 167 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 168 try {
jjg@57 169 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 170 } finally {
jjg@57 171 fileManager.log.useSource(prev);
jjg@57 172 }
jjg@57 173 fileManager.recycleByteBuffer(bb); // save for next time
jjg@57 174 if (!ignoreEncodingErrors)
jjg@57 175 fileManager.cache(this, cb);
jjg@57 176 } finally {
jjg@57 177 in.close();
jjg@57 178 }
jjg@57 179 }
jjg@57 180 return cb;
jjg@57 181 }
jjg@57 182
jjg@57 183 @Override
jjg@415 184 public Writer openWriter() throws IOException {
jjg@415 185 throw new UnsupportedOperationException();
jjg@415 186 }
jjg@415 187
jjg@415 188 @Override
jjg@415 189 public long getLastModified() {
jjg@415 190 return entry.getLastModified();
jjg@415 191 }
jjg@415 192
jjg@415 193 @Override
jjg@415 194 public boolean delete() {
jjg@415 195 throw new UnsupportedOperationException();
jjg@415 196 }
jjg@415 197
jjg@415 198 @Override
jjg@415 199 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@415 200 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@415 201 }
jjg@415 202
jjg@415 203 @Override
jjg@57 204 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@415 205 String entryName = entry.getName();
jjg@57 206 if (zfIndex.symbolFilePrefix != null) {
jjg@103 207 String prefix = zfIndex.symbolFilePrefix.path;
jjg@57 208 if (entryName.startsWith(prefix))
jjg@57 209 entryName = entryName.substring(prefix.length());
jjg@57 210 }
jjg@103 211 return removeExtension(entryName).replace('/', '.');
jjg@57 212 }
jjg@415 213
jjg@415 214 @Override
jjg@415 215 public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
jjg@415 216 cn.getClass(); // null check
jjg@415 217 if (k == Kind.OTHER && getKind() != k)
jjg@415 218 return false;
jjg@415 219 return name.equals(cn + k.extension);
jjg@415 220 }
jjg@415 221
jjg@415 222 @Override
jjg@415 223 public boolean equals(Object other) {
jjg@415 224 if (!(other instanceof ZipFileIndexFileObject))
jjg@415 225 return false;
jjg@415 226 ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
jjg@415 227 return entry.equals(o.entry);
jjg@415 228 }
jjg@415 229
jjg@415 230 @Override
jjg@415 231 public int hashCode() {
jjg@415 232 return zipName.hashCode() + (name.hashCode() << 10);
jjg@415 233 }
jjg@415 234
jjg@415 235 private String getPrefixedEntryName() {
jjg@415 236 if (zfIndex.symbolFilePrefix != null)
jjg@415 237 return zfIndex.symbolFilePrefix.path + entry.getName();
jjg@415 238 else
jjg@415 239 return entry.getName();
jjg@415 240 }
jjg@57 241 }
jjg@57 242
jjg@57 243 }

mercurial