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

Tue, 22 Sep 2009 14:06:29 -0700

author
xdono
date
Tue, 22 Sep 2009 14:06:29 -0700
changeset 404
14735c7932d7
parent 400
35e29f51a7c3
child 418
4776a869fdfa
permissions
-rw-r--r--

6884624: Update copyright year
Summary: Update copyright for files that have been modified in 2009 through Septermber
Reviewed-by: tbell, ohair

jjg@57 1 /*
xdono@404 2 * Copyright 2005-2009 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@57 126 public InputStream openInputStream() throws IOException {
jjg@57 127
jjg@57 128 if (inputStream == null) {
jjg@57 129 inputStream = new ByteArrayInputStream(read());
jjg@57 130 }
jjg@57 131 return inputStream;
jjg@57 132 }
jjg@57 133
jjg@400 134 @Override
jjg@57 135 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@57 136 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@57 137 }
jjg@57 138
jjg@57 139 public OutputStream openOutputStream() throws IOException {
jjg@57 140 throw new UnsupportedOperationException();
jjg@57 141 }
jjg@57 142
jjg@57 143 public Writer openWriter() throws IOException {
jjg@57 144 throw new UnsupportedOperationException();
jjg@57 145 }
jjg@57 146
jjg@57 147 /** @deprecated see bug 6410637 */
jjg@57 148 @Deprecated
jjg@57 149 public String getName() {
jjg@57 150 return name;
jjg@57 151 }
jjg@57 152
jjg@57 153 public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
jjg@57 154 cn.getClass(); // null check
jjg@57 155 if (k == Kind.OTHER && getKind() != k)
jjg@57 156 return false;
jjg@57 157 return name.equals(cn + k.extension);
jjg@57 158 }
jjg@57 159
jjg@57 160 /** @deprecated see bug 6410637 */
jjg@57 161 @Deprecated
jjg@400 162 @Override
jjg@57 163 public String getPath() {
jjg@57 164 return zipName + "(" + entry.getName() + ")";
jjg@57 165 }
jjg@57 166
jjg@57 167 public long getLastModified() {
jjg@57 168 return entry.getLastModified();
jjg@57 169 }
jjg@57 170
jjg@57 171 public boolean delete() {
jjg@57 172 throw new UnsupportedOperationException();
jjg@57 173 }
jjg@57 174
jjg@57 175 @Override
jjg@57 176 public boolean equals(Object other) {
jjg@57 177 if (!(other instanceof ZipFileIndexFileObject))
jjg@57 178 return false;
jjg@57 179 ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
jjg@57 180 return entry.equals(o.entry);
jjg@57 181 }
jjg@57 182
jjg@57 183 @Override
jjg@57 184 public int hashCode() {
jjg@57 185 return zipName.hashCode() + (name.hashCode() << 10);
jjg@57 186 }
jjg@57 187
jjg@57 188 public String getZipName() {
jjg@400 189 return zipName.getPath();
jjg@57 190 }
jjg@57 191
jjg@57 192 public String getZipEntryName() {
jjg@57 193 return entry.getName();
jjg@57 194 }
jjg@57 195
jjg@57 196 public URI toUri() {
jjg@400 197 if (zfIndex.symbolFilePrefix != null)
jjg@400 198 return createJarUri(zipName, zfIndex.symbolFilePrefix.path + entry.getName());
jjg@400 199 else
jjg@400 200 return createJarUri(zipName, entry.getName());
jjg@57 201 }
jjg@57 202
jjg@57 203 private byte[] read() throws IOException {
jjg@103 204 assert entry != null; // see constructor
jjg@57 205 return zfIndex.read(entry);
jjg@57 206 }
jjg@57 207
jjg@57 208 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 209 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 210 if (cb == null) {
jjg@57 211 InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
jjg@57 212 try {
jjg@57 213 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 214 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 215 try {
jjg@57 216 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 217 } finally {
jjg@57 218 fileManager.log.useSource(prev);
jjg@57 219 }
jjg@57 220 fileManager.recycleByteBuffer(bb); // save for next time
jjg@57 221 if (!ignoreEncodingErrors)
jjg@57 222 fileManager.cache(this, cb);
jjg@57 223 } finally {
jjg@57 224 in.close();
jjg@57 225 }
jjg@57 226 }
jjg@57 227 return cb;
jjg@57 228 }
jjg@57 229
jjg@57 230 @Override
jjg@57 231 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@57 232 String entryName = getZipEntryName();
jjg@57 233 if (zfIndex.symbolFilePrefix != null) {
jjg@103 234 String prefix = zfIndex.symbolFilePrefix.path;
jjg@57 235 if (entryName.startsWith(prefix))
jjg@57 236 entryName = entryName.substring(prefix.length());
jjg@57 237 }
jjg@103 238 return removeExtension(entryName).replace('/', '.');
jjg@57 239 }
jjg@57 240 }
jjg@57 241
jjg@57 242 }

mercurial