aoqi@0: /*
aoqi@0: * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javac.file;
aoqi@0:
aoqi@0: import java.io.File;
aoqi@0: import java.io.IOException;
aoqi@0: import java.io.InputStream;
aoqi@0: import java.io.OutputStream;
aoqi@0: import java.io.Writer;
aoqi@0: import java.net.URI;
aoqi@0: import java.nio.ByteBuffer;
aoqi@0: import java.nio.CharBuffer;
aoqi@0: import java.nio.charset.CharsetDecoder;
aoqi@0: import java.util.Enumeration;
aoqi@0: import java.util.HashMap;
aoqi@0: import java.util.Map;
aoqi@0: import java.util.Set;
aoqi@0: import java.util.zip.ZipEntry;
aoqi@0: import java.util.zip.ZipFile;
aoqi@0:
aoqi@0: import javax.tools.JavaFileObject;
aoqi@0:
aoqi@0: import com.sun.tools.javac.file.JavacFileManager.Archive;
aoqi@0: import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
aoqi@0: import com.sun.tools.javac.file.RelativePath.RelativeFile;
aoqi@0: import com.sun.tools.javac.util.List;
aoqi@0: import java.lang.ref.Reference;
aoqi@0: import java.lang.ref.SoftReference;
aoqi@0:
aoqi@0: /**
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: */
aoqi@0: public class ZipArchive implements Archive {
aoqi@0:
aoqi@0: public ZipArchive(JavacFileManager fm, ZipFile zfile) throws IOException {
aoqi@0: this(fm, zfile, true);
aoqi@0: }
aoqi@0:
aoqi@0: protected ZipArchive(JavacFileManager fm, ZipFile zfile, boolean initMap) throws IOException {
aoqi@0: this.fileManager = fm;
aoqi@0: this.zfile = zfile;
aoqi@0: this.map = new HashMap>();
aoqi@0: if (initMap)
aoqi@0: initMap();
aoqi@0: }
aoqi@0:
aoqi@0: protected void initMap() throws IOException {
aoqi@0: for (Enumeration extends ZipEntry> e = zfile.entries(); e.hasMoreElements(); ) {
aoqi@0: ZipEntry entry;
aoqi@0: try {
aoqi@0: entry = e.nextElement();
aoqi@0: } catch (InternalError ex) {
aoqi@0: IOException io = new IOException();
aoqi@0: io.initCause(ex); // convenience constructors added in Mustang :-(
aoqi@0: throw io;
aoqi@0: }
aoqi@0: addZipEntry(entry);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: void addZipEntry(ZipEntry entry) {
aoqi@0: String name = entry.getName();
aoqi@0: int i = name.lastIndexOf('/');
aoqi@0: RelativeDirectory dirname = new RelativeDirectory(name.substring(0, i+1));
aoqi@0: String basename = name.substring(i+1);
aoqi@0: if (basename.length() == 0)
aoqi@0: return;
aoqi@0: List list = map.get(dirname);
aoqi@0: if (list == null)
aoqi@0: list = List.nil();
aoqi@0: list = list.prepend(basename);
aoqi@0: map.put(dirname, list);
aoqi@0: }
aoqi@0:
aoqi@0: public boolean contains(RelativePath name) {
aoqi@0: RelativeDirectory dirname = name.dirname();
aoqi@0: String basename = name.basename();
aoqi@0: if (basename.length() == 0)
aoqi@0: return false;
aoqi@0: List list = map.get(dirname);
aoqi@0: return (list != null && list.contains(basename));
aoqi@0: }
aoqi@0:
aoqi@0: public List getFiles(RelativeDirectory subdirectory) {
aoqi@0: return map.get(subdirectory);
aoqi@0: }
aoqi@0:
aoqi@0: public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
aoqi@0: ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zfile);
aoqi@0: return new ZipFileObject(this, file, ze);
aoqi@0: }
aoqi@0:
aoqi@0: public Set getSubdirectories() {
aoqi@0: return map.keySet();
aoqi@0: }
aoqi@0:
aoqi@0: public void close() throws IOException {
aoqi@0: zfile.close();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return "ZipArchive[" + zfile.getName() + "]";
aoqi@0: }
aoqi@0:
aoqi@0: private File getAbsoluteFile() {
aoqi@0: File absFile = (absFileRef == null ? null : absFileRef.get());
aoqi@0: if (absFile == null) {
aoqi@0: absFile = new File(zfile.getName()).getAbsoluteFile();
aoqi@0: absFileRef = new SoftReference(absFile);
aoqi@0: }
aoqi@0: return absFile;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * The file manager that created this archive.
aoqi@0: */
aoqi@0: protected JavacFileManager fileManager;
aoqi@0: /**
aoqi@0: * The index for the contents of this archive.
aoqi@0: */
aoqi@0: protected final Map> map;
aoqi@0: /**
aoqi@0: * The zip file for the archive.
aoqi@0: */
aoqi@0: protected final ZipFile zfile;
aoqi@0: /**
aoqi@0: * A reference to the absolute filename for the zip file for the archive.
aoqi@0: */
aoqi@0: protected Reference absFileRef;
aoqi@0:
aoqi@0: /**
aoqi@0: * A subclass of JavaFileObject representing zip entries.
aoqi@0: */
aoqi@0: public static class ZipFileObject extends BaseFileObject {
aoqi@0:
aoqi@0: private String name;
aoqi@0: ZipArchive zarch;
aoqi@0: ZipEntry entry;
aoqi@0:
aoqi@0: protected ZipFileObject(ZipArchive zarch, String name, ZipEntry entry) {
aoqi@0: super(zarch.fileManager);
aoqi@0: this.zarch = zarch;
aoqi@0: this.name = name;
aoqi@0: this.entry = entry;
aoqi@0: }
aoqi@0:
aoqi@0: public URI toUri() {
aoqi@0: File zipFile = new File(zarch.zfile.getName());
aoqi@0: return createJarUri(zipFile, entry.getName());
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String getName() {
aoqi@0: return zarch.zfile.getName() + "(" + entry.getName() + ")";
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String getShortName() {
aoqi@0: return new File(zarch.zfile.getName()).getName() + "(" + entry + ")";
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public JavaFileObject.Kind getKind() {
aoqi@0: return getKind(entry.getName());
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public InputStream openInputStream() throws IOException {
aoqi@0: return zarch.zfile.getInputStream(entry);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public OutputStream openOutputStream() throws IOException {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
aoqi@0: CharBuffer cb = fileManager.getCachedContent(this);
aoqi@0: if (cb == null) {
aoqi@0: InputStream in = zarch.zfile.getInputStream(entry);
aoqi@0: try {
aoqi@0: ByteBuffer bb = fileManager.makeByteBuffer(in);
aoqi@0: JavaFileObject prev = fileManager.log.useSource(this);
aoqi@0: try {
aoqi@0: cb = fileManager.decode(bb, ignoreEncodingErrors);
aoqi@0: } finally {
aoqi@0: fileManager.log.useSource(prev);
aoqi@0: }
aoqi@0: fileManager.recycleByteBuffer(bb);
aoqi@0: if (!ignoreEncodingErrors) {
aoqi@0: fileManager.cache(this, cb);
aoqi@0: }
aoqi@0: } finally {
aoqi@0: in.close();
aoqi@0: }
aoqi@0: }
aoqi@0: return cb;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Writer openWriter() throws IOException {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public long getLastModified() {
aoqi@0: return entry.getTime();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public boolean delete() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
aoqi@0: return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: protected String inferBinaryName(Iterable extends File> path) {
aoqi@0: String entryName = entry.getName();
aoqi@0: return removeExtension(entryName).replace('/', '.');
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
aoqi@0: cn.getClass();
aoqi@0: // null check
aoqi@0: if (k == Kind.OTHER && getKind() != k) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: return name.equals(cn + k.extension);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Check if two file objects are equal.
aoqi@0: * Two ZipFileObjects are equal if the absolute paths of the underlying
aoqi@0: * zip files are equal and if the paths within those zip files are equal.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public boolean equals(Object other) {
aoqi@0: if (this == other)
aoqi@0: return true;
aoqi@0:
aoqi@0: if (!(other instanceof ZipFileObject))
aoqi@0: return false;
aoqi@0:
aoqi@0: ZipFileObject o = (ZipFileObject) other;
aoqi@0: return zarch.getAbsoluteFile().equals(o.zarch.getAbsoluteFile())
aoqi@0: && name.equals(o.name);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public int hashCode() {
aoqi@0: return zarch.getAbsoluteFile().hashCode() + name.hashCode();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: }