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

Mon, 10 Jan 2011 15:08:31 -0800

author
jjg
date
Mon, 10 Jan 2011 15:08:31 -0800
changeset 816
7c537f4298fb
parent 581
f2fdd52e4e87
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6396503: javac should not require assertions enabled
Reviewed-by: mcimadamore

jjg@57 1 /*
jjg@816 2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@57 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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@816 45 import com.sun.tools.javac.util.Assert;
jjg@103 46 import com.sun.tools.javac.util.List;
jjg@103 47
jjg@333 48 /**
jjg@581 49 * <p><b>This is NOT part of any supported API.
jjg@333 50 * If you write code that depends on this, you do so at your own risk.
jjg@333 51 * This code and its internal interfaces are subject to change or
jjg@333 52 * deletion without notice.</b>
jjg@333 53 */
jjg@57 54 public class ZipFileIndexArchive implements Archive {
jjg@57 55
jjg@57 56 private final ZipFileIndex zfIndex;
jjg@57 57 private JavacFileManager fileManager;
jjg@57 58
jjg@57 59 public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException {
jjg@57 60 super();
jjg@57 61 this.fileManager = fileManager;
jjg@57 62 this.zfIndex = zdir;
jjg@57 63 }
jjg@57 64
jjg@103 65 public boolean contains(RelativePath name) {
jjg@57 66 return zfIndex.contains(name);
jjg@57 67 }
jjg@57 68
jjg@103 69 public List<String> getFiles(RelativeDirectory subdirectory) {
jjg@103 70 return zfIndex.getFiles(subdirectory);
jjg@57 71 }
jjg@57 72
jjg@103 73 public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
jjg@103 74 RelativeFile fullZipFileName = new RelativeFile(subdirectory, file);
jjg@57 75 ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
jjg@400 76 JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile());
jjg@57 77 return ret;
jjg@57 78 }
jjg@57 79
jjg@103 80 public Set<RelativeDirectory> getSubdirectories() {
jjg@57 81 return zfIndex.getAllDirectories();
jjg@57 82 }
jjg@57 83
jjg@57 84 public void close() throws IOException {
jjg@57 85 zfIndex.close();
jjg@57 86 }
jjg@57 87
jjg@400 88 @Override
jjg@103 89 public String toString() {
jjg@103 90 return "ZipFileIndexArchive[" + zfIndex + "]";
jjg@103 91 }
jjg@103 92
jjg@57 93 /**
jjg@57 94 * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation.
jjg@57 95 */
jjg@57 96 public static class ZipFileIndexFileObject extends BaseFileObject {
jjg@57 97
jjg@57 98 /** The entry's name.
jjg@57 99 */
jjg@57 100 private String name;
jjg@57 101
jjg@57 102 /** The zipfile containing the entry.
jjg@57 103 */
jjg@57 104 ZipFileIndex zfIndex;
jjg@57 105
jjg@57 106 /** The underlying zip entry object.
jjg@57 107 */
jjg@57 108 ZipFileIndex.Entry entry;
jjg@57 109
jjg@57 110 /** The InputStream for this zip entry (file.)
jjg@57 111 */
jjg@57 112 InputStream inputStream = null;
jjg@57 113
jjg@57 114 /** The name of the zip file where this entry resides.
jjg@57 115 */
jjg@400 116 File zipName;
jjg@57 117
jjg@57 118
jjg@400 119 ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, File zipFileName) {
jjg@57 120 super(fileManager);
jjg@57 121 this.name = entry.getFileName();
jjg@57 122 this.zfIndex = zfIndex;
jjg@57 123 this.entry = entry;
jjg@57 124 this.zipName = zipFileName;
jjg@57 125 }
jjg@57 126
jjg@415 127 @Override
jjg@415 128 public URI toUri() {
jjg@415 129 return createJarUri(zipName, getPrefixedEntryName());
jjg@415 130 }
jjg@415 131
jjg@415 132 @Override
jjg@415 133 public String getName() {
jjg@415 134 return zipName + "(" + getPrefixedEntryName() + ")";
jjg@415 135 }
jjg@415 136
jjg@415 137 @Override
jjg@415 138 public String getShortName() {
jjg@415 139 return zipName.getName() + "(" + entry.getName() + ")";
jjg@415 140 }
jjg@415 141
jjg@415 142 @Override
jjg@415 143 public JavaFileObject.Kind getKind() {
jjg@415 144 return getKind(entry.getName());
jjg@415 145 }
jjg@415 146
jjg@415 147 @Override
jjg@57 148 public InputStream openInputStream() throws IOException {
jjg@57 149 if (inputStream == null) {
jjg@816 150 Assert.checkNonNull(entry); // see constructor
jjg@415 151 inputStream = new ByteArrayInputStream(zfIndex.read(entry));
jjg@57 152 }
jjg@57 153 return inputStream;
jjg@57 154 }
jjg@57 155
jjg@400 156 @Override
jjg@57 157 public OutputStream openOutputStream() throws IOException {
jjg@57 158 throw new UnsupportedOperationException();
jjg@57 159 }
jjg@57 160
jjg@400 161 @Override
jjg@57 162 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 163 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 164 if (cb == null) {
jjg@57 165 InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
jjg@57 166 try {
jjg@57 167 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 168 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 169 try {
jjg@57 170 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 171 } finally {
jjg@57 172 fileManager.log.useSource(prev);
jjg@57 173 }
jjg@57 174 fileManager.recycleByteBuffer(bb); // save for next time
jjg@57 175 if (!ignoreEncodingErrors)
jjg@57 176 fileManager.cache(this, cb);
jjg@57 177 } finally {
jjg@57 178 in.close();
jjg@57 179 }
jjg@57 180 }
jjg@57 181 return cb;
jjg@57 182 }
jjg@57 183
jjg@57 184 @Override
jjg@415 185 public Writer openWriter() throws IOException {
jjg@415 186 throw new UnsupportedOperationException();
jjg@415 187 }
jjg@415 188
jjg@415 189 @Override
jjg@415 190 public long getLastModified() {
jjg@415 191 return entry.getLastModified();
jjg@415 192 }
jjg@415 193
jjg@415 194 @Override
jjg@415 195 public boolean delete() {
jjg@415 196 throw new UnsupportedOperationException();
jjg@415 197 }
jjg@415 198
jjg@415 199 @Override
jjg@415 200 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@415 201 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@415 202 }
jjg@415 203
jjg@415 204 @Override
jjg@57 205 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@415 206 String entryName = entry.getName();
jjg@57 207 if (zfIndex.symbolFilePrefix != null) {
jjg@103 208 String prefix = zfIndex.symbolFilePrefix.path;
jjg@57 209 if (entryName.startsWith(prefix))
jjg@57 210 entryName = entryName.substring(prefix.length());
jjg@57 211 }
jjg@103 212 return removeExtension(entryName).replace('/', '.');
jjg@57 213 }
jjg@415 214
jjg@415 215 @Override
jjg@415 216 public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
jjg@415 217 cn.getClass(); // null check
jjg@415 218 if (k == Kind.OTHER && getKind() != k)
jjg@415 219 return false;
jjg@415 220 return name.equals(cn + k.extension);
jjg@415 221 }
jjg@415 222
jjg@424 223 /**
jjg@424 224 * Check if two file objects are equal.
jjg@424 225 * Two ZipFileIndexFileObjects are equal if the absolute paths of the underlying
jjg@424 226 * zip files are equal and if the paths within those zip files are equal.
jjg@424 227 */
jjg@415 228 @Override
jjg@415 229 public boolean equals(Object other) {
jjg@424 230 if (this == other)
jjg@424 231 return true;
jjg@424 232
jjg@415 233 if (!(other instanceof ZipFileIndexFileObject))
jjg@415 234 return false;
jjg@424 235
jjg@415 236 ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
jjg@424 237 return zfIndex.getAbsoluteFile().equals(o.zfIndex.getAbsoluteFile())
jjg@424 238 && name.equals(o.name);
jjg@415 239 }
jjg@415 240
jjg@415 241 @Override
jjg@415 242 public int hashCode() {
jjg@424 243 return zfIndex.getAbsoluteFile().hashCode() + name.hashCode();
jjg@415 244 }
jjg@415 245
jjg@415 246 private String getPrefixedEntryName() {
jjg@415 247 if (zfIndex.symbolFilePrefix != null)
jjg@415 248 return zfIndex.symbolFilePrefix.path + entry.getName();
jjg@415 249 else
jjg@415 250 return entry.getName();
jjg@415 251 }
jjg@57 252 }
jjg@57 253
jjg@57 254 }

mercurial