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