ZipFileIndexArchive.java revision 423
893N/A/*
2362N/A * Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Sun designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Sun in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
893N/A */
893N/A
893N/Apackage com.sun.tools.javac.file;
893N/A
3497N/Aimport java.io.IOException;
3497N/Aimport java.util.Set;
893N/Aimport javax.tools.JavaFileObject;
893N/A
3497N/Aimport java.io.ByteArrayInputStream;
893N/Aimport java.io.File;
893N/Aimport java.io.InputStream;
893N/Aimport java.io.OutputStream;
893N/Aimport java.io.Writer;
893N/Aimport java.net.URI;
893N/Aimport java.nio.ByteBuffer;
893N/Aimport java.nio.CharBuffer;
893N/Aimport java.nio.charset.CharsetDecoder;
893N/A
893N/Aimport com.sun.tools.javac.file.JavacFileManager.Archive;
893N/Aimport com.sun.tools.javac.file.RelativePath.RelativeDirectory;
3497N/Aimport com.sun.tools.javac.file.RelativePath.RelativeFile;
893N/Aimport com.sun.tools.javac.util.List;
893N/A
893N/A/**
893N/A * <p><b>This is NOT part of any API supported by Sun Microsystems.
893N/A * If you write code that depends on this, you do so at your own risk.
893N/A * This code and its internal interfaces are subject to change or
893N/A * deletion without notice.</b>
893N/A */
893N/Apublic class ZipFileIndexArchive implements Archive {
893N/A
893N/A private final ZipFileIndex zfIndex;
893N/A private JavacFileManager fileManager;
893N/A
893N/A public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException {
3497N/A super();
3497N/A this.fileManager = fileManager;
3497N/A this.zfIndex = zdir;
3497N/A }
3497N/A
3497N/A public boolean contains(RelativePath name) {
3497N/A return zfIndex.contains(name);
3497N/A }
893N/A
893N/A public List<String> getFiles(RelativeDirectory subdirectory) {
3497N/A return zfIndex.getFiles(subdirectory);
3497N/A }
3497N/A
3497N/A public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
3497N/A RelativeFile fullZipFileName = new RelativeFile(subdirectory, file);
3497N/A ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
3497N/A JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile());
3497N/A return ret;
3497N/A }
3497N/A
3497N/A public Set<RelativeDirectory> getSubdirectories() {
3497N/A return zfIndex.getAllDirectories();
3497N/A }
3497N/A
3497N/A public void close() throws IOException {
3497N/A zfIndex.close();
3497N/A }
3497N/A
3497N/A @Override
3497N/A public String toString() {
3497N/A return "ZipFileIndexArchive[" + zfIndex + "]";
893N/A }
3497N/A
3497N/A /**
3497N/A * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation.
893N/A */
893N/A public static class ZipFileIndexFileObject extends BaseFileObject {
893N/A
893N/A /** The entry's name.
893N/A */
893N/A private String name;
893N/A
893N/A /** The zipfile containing the entry.
893N/A */
893N/A ZipFileIndex zfIndex;
893N/A
893N/A /** The underlying zip entry object.
893N/A */
893N/A ZipFileIndex.Entry entry;
893N/A
893N/A /** The InputStream for this zip entry (file.)
893N/A */
3497N/A InputStream inputStream = null;
893N/A
893N/A /** The name of the zip file where this entry resides.
893N/A */
893N/A File zipName;
893N/A
893N/A
893N/A ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, File zipFileName) {
893N/A super(fileManager);
893N/A this.name = entry.getFileName();
893N/A this.zfIndex = zfIndex;
893N/A this.entry = entry;
893N/A this.zipName = zipFileName;
893N/A }
893N/A
893N/A @Override
893N/A public URI toUri() {
893N/A return createJarUri(zipName, getPrefixedEntryName());
893N/A }
893N/A
893N/A @Override
893N/A public String getName() {
893N/A return zipName + "(" + getPrefixedEntryName() + ")";
893N/A }
893N/A
893N/A @Override
893N/A public String getShortName() {
893N/A return zipName.getName() + "(" + entry.getName() + ")";
893N/A }
893N/A
893N/A @Override
893N/A public JavaFileObject.Kind getKind() {
893N/A return getKind(entry.getName());
893N/A }
893N/A
893N/A @Override
893N/A public InputStream openInputStream() throws IOException {
893N/A if (inputStream == null) {
893N/A assert entry != null; // see constructor
893N/A inputStream = new ByteArrayInputStream(zfIndex.read(entry));
893N/A }
893N/A return inputStream;
893N/A }
893N/A
893N/A @Override
893N/A public OutputStream openOutputStream() throws IOException {
893N/A throw new UnsupportedOperationException();
893N/A }
893N/A
893N/A @Override
893N/A public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
893N/A CharBuffer cb = fileManager.getCachedContent(this);
893N/A if (cb == null) {
893N/A InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
893N/A try {
893N/A ByteBuffer bb = fileManager.makeByteBuffer(in);
893N/A JavaFileObject prev = fileManager.log.useSource(this);
893N/A try {
893N/A cb = fileManager.decode(bb, ignoreEncodingErrors);
893N/A } finally {
893N/A fileManager.log.useSource(prev);
893N/A }
893N/A fileManager.recycleByteBuffer(bb); // save for next time
893N/A if (!ignoreEncodingErrors)
893N/A fileManager.cache(this, cb);
893N/A } finally {
893N/A in.close();
893N/A }
893N/A }
893N/A return cb;
893N/A }
893N/A
893N/A @Override
893N/A public Writer openWriter() throws IOException {
893N/A throw new UnsupportedOperationException();
893N/A }
893N/A
893N/A @Override
3497N/A public long getLastModified() {
3497N/A return entry.getLastModified();
3497N/A }
3497N/A
3497N/A @Override
3497N/A public boolean delete() {
3497N/A throw new UnsupportedOperationException();
3497N/A }
3497N/A
3497N/A @Override
3497N/A protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
893N/A return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
893N/A }
893N/A
893N/A @Override
893N/A protected String inferBinaryName(Iterable<? extends File> path) {
893N/A String entryName = entry.getName();
893N/A if (zfIndex.symbolFilePrefix != null) {
893N/A String prefix = zfIndex.symbolFilePrefix.path;
893N/A if (entryName.startsWith(prefix))
893N/A entryName = entryName.substring(prefix.length());
893N/A }
893N/A return removeExtension(entryName).replace('/', '.');
893N/A }
893N/A
893N/A @Override
893N/A public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
893N/A cn.getClass(); // null check
893N/A if (k == Kind.OTHER && getKind() != k)
893N/A return false;
893N/A return name.equals(cn + k.extension);
893N/A }
893N/A
893N/A /**
893N/A * Check if two file objects are equal.
893N/A * Two ZipFileIndexFileObjects are equal if the absolute paths of the underlying
893N/A * zip files are equal and if the paths within those zip files are equal.
893N/A */
893N/A @Override
893N/A public boolean equals(Object other) {
893N/A if (this == other)
893N/A return true;
893N/A
893N/A if (!(other instanceof ZipFileIndexFileObject))
893N/A return false;
893N/A
893N/A ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
893N/A return zfIndex.getAbsoluteFile().equals(o.zfIndex.getAbsoluteFile())
893N/A && name.equals(o.name);
893N/A }
893N/A
893N/A @Override
893N/A public int hashCode() {
893N/A return zfIndex.getAbsoluteFile().hashCode() + name.hashCode();
893N/A }
893N/A
893N/A private String getPrefixedEntryName() {
893N/A if (zfIndex.symbolFilePrefix != null)
893N/A return zfIndex.symbolFilePrefix.path + entry.getName();
893N/A else
893N/A return entry.getName();
}
}
}