ZipFileIndex.java revision 332
35N/A/*
35N/A * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
35N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35N/A *
35N/A * This code is free software; you can redistribute it and/or modify it
35N/A * under the terms of the GNU General Public License version 2 only, as
35N/A * published by the Free Software Foundation. Sun designates this
35N/A * particular file as subject to the "Classpath" exception as provided
35N/A * by Sun in the LICENSE file that accompanied this code.
35N/A *
35N/A * This code is distributed in the hope that it will be useful, but WITHOUT
35N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
35N/A * version 2 for more details (a copy is included in the LICENSE file that
35N/A * accompanied this code).
35N/A *
35N/A * You should have received a copy of the GNU General Public License version
35N/A * 2 along with this work; if not, write to the Free Software Foundation,
35N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35N/A *
35N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
35N/A * CA 95054 USA or visit www.sun.com if you need additional information or
35N/A * have any questions.
35N/A */
35N/A
49N/Apackage com.sun.tools.javac.file;
0N/A
102N/A
49N/Aimport java.io.File;
49N/Aimport java.io.FileNotFoundException;
49N/Aimport java.io.IOException;
49N/Aimport java.io.RandomAccessFile;
102N/Aimport java.lang.ref.SoftReference;
49N/Aimport java.util.ArrayList;
49N/Aimport java.util.Arrays;
70N/Aimport java.util.Calendar;
49N/Aimport java.util.Collections;
49N/Aimport java.util.HashMap;
49N/Aimport java.util.HashSet;
49N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
49N/Aimport java.util.Map;
49N/Aimport java.util.Set;
0N/Aimport java.util.concurrent.locks.ReentrantLock;
49N/Aimport java.util.zip.DataFormatException;
49N/Aimport java.util.zip.Inflater;
49N/Aimport java.util.zip.ZipException;
0N/A
102N/Aimport com.sun.tools.javac.file.RelativePath.RelativeDirectory;
102N/Aimport com.sun.tools.javac.file.RelativePath.RelativeFile;
102N/A
0N/A/** This class implements building of index of a zip archive and access to it's context.
0N/A * It also uses prebuild index if available. It supports invocations where it will
0N/A * serialize an optimized zip index file to disk.
0N/A *
0N/A * In oreder to use secondary index file make sure the option "usezipindex" is in the Options object,
0N/A * when JavacFileManager is invoked. (You can pass "-XDusezipindex" on the command line.
0N/A *
0N/A * Location where to look for/generate optimized zip index files can be provided using
0N/A * "-XDcachezipindexdir=<directory>". If this flag is not provided, the dfault location is
0N/A * the value of the "java.io.tmpdir" system property.
0N/A *
0N/A * If key "-XDwritezipindexfiles" is specified, there will be new optimized index file
0N/A * created for each archive, used by the compiler for compilation, at location,
0N/A * specified by "cachezipindexdir" option.
0N/A *
0N/A * If nonBatchMode option is specified (-XDnonBatchMode) the compiler will use timestamp
0N/A * checking to reindex the zip files if it is needed. In batch mode the timestamps are not checked
0N/A * and the compiler uses the cached indexes.
332N/A *
332N/A * <p><b>This is NOT part of any API supported by Sun Microsystems.
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>
0N/A */
0N/Apublic class ZipFileIndex {
0N/A private static final String MIN_CHAR = String.valueOf(Character.MIN_VALUE);
0N/A private static final String MAX_CHAR = String.valueOf(Character.MAX_VALUE);
0N/A
0N/A public final static long NOT_MODIFIED = Long.MIN_VALUE;
0N/A
0N/A private static Map<File, ZipFileIndex> zipFileIndexCache = new HashMap<File, ZipFileIndex>();
0N/A private static ReentrantLock lock = new ReentrantLock();
0N/A
0N/A private static boolean NON_BATCH_MODE = System.getProperty("nonBatchMode") != null;// TODO: Use -XD compiler switch for this.
0N/A
102N/A private Map<RelativeDirectory, DirectoryEntry> directories = Collections.<RelativeDirectory, DirectoryEntry>emptyMap();
102N/A private Set<RelativeDirectory> allDirs = Collections.<RelativeDirectory>emptySet();
0N/A
0N/A // ZipFileIndex data entries
0N/A private File zipFile;
0N/A private long zipFileLastModified = NOT_MODIFIED;
0N/A private RandomAccessFile zipRandomFile;
56N/A private Entry[] entries;
0N/A
0N/A private boolean readFromIndex = false;
0N/A private File zipIndexFile = null;
0N/A private boolean triedToReadIndex = false;
102N/A final RelativeDirectory symbolFilePrefix;
0N/A private int symbolFilePrefixLength = 0;
0N/A private boolean hasPopulatedData = false;
0N/A private long lastReferenceTimeStamp = NOT_MODIFIED;
0N/A
0N/A private boolean usePreindexedCache = false;
0N/A private String preindexedCacheLocation = null;
0N/A
0N/A private boolean writeIndex = false;
0N/A
102N/A private Map <String, SoftReference<RelativeDirectory>> relativeDirectoryCache =
102N/A new HashMap<String, SoftReference<RelativeDirectory>>();
102N/A
0N/A /**
0N/A * Returns a list of all ZipFileIndex entries
0N/A *
0N/A * @return A list of ZipFileIndex entries, or an empty list
0N/A */
0N/A public static List<ZipFileIndex> getZipFileIndexes() {
0N/A return getZipFileIndexes(false);
0N/A }
0N/A
0N/A /**
0N/A * Returns a list of all ZipFileIndex entries
0N/A *
0N/A * @param openedOnly If true it returns a list of only opened ZipFileIndex entries, otherwise
0N/A * all ZipFileEntry(s) are included into the list.
0N/A * @return A list of ZipFileIndex entries, or an empty list
0N/A */
0N/A public static List<ZipFileIndex> getZipFileIndexes(boolean openedOnly) {
0N/A List<ZipFileIndex> zipFileIndexes = new ArrayList<ZipFileIndex>();
0N/A lock.lock();
0N/A try {
0N/A zipFileIndexes.addAll(zipFileIndexCache.values());
0N/A
0N/A if (openedOnly) {
0N/A for(ZipFileIndex elem : zipFileIndexes) {
0N/A if (!elem.isOpen()) {
0N/A zipFileIndexes.remove(elem);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A return zipFileIndexes;
0N/A }
0N/A
0N/A public boolean isOpen() {
0N/A lock.lock();
0N/A try {
0N/A return zipRandomFile != null;
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public static ZipFileIndex getZipFileIndex(File zipFile,
102N/A RelativeDirectory symbolFilePrefix,
102N/A boolean useCache, String cacheLocation,
102N/A boolean writeIndex) throws IOException {
0N/A ZipFileIndex zi = null;
0N/A lock.lock();
0N/A try {
0N/A zi = getExistingZipIndex(zipFile);
0N/A
0N/A if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) {
56N/A zi = new ZipFileIndex(zipFile, symbolFilePrefix, writeIndex,
0N/A useCache, cacheLocation);
0N/A zipFileIndexCache.put(zipFile, zi);
0N/A }
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A return zi;
0N/A }
0N/A
0N/A public static ZipFileIndex getExistingZipIndex(File zipFile) {
0N/A lock.lock();
0N/A try {
0N/A return zipFileIndexCache.get(zipFile);
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A public static void clearCache() {
0N/A lock.lock();
0N/A try {
0N/A zipFileIndexCache.clear();
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A public static void clearCache(long timeNotUsed) {
0N/A lock.lock();
0N/A try {
0N/A Iterator<File> cachedFileIterator = zipFileIndexCache.keySet().iterator();
0N/A while (cachedFileIterator.hasNext()) {
0N/A File cachedFile = cachedFileIterator.next();
0N/A ZipFileIndex cachedZipIndex = zipFileIndexCache.get(cachedFile);
0N/A if (cachedZipIndex != null) {
0N/A long timeToTest = cachedZipIndex.lastReferenceTimeStamp + timeNotUsed;
0N/A if (timeToTest < cachedZipIndex.lastReferenceTimeStamp || // Overflow...
0N/A System.currentTimeMillis() > timeToTest) {
0N/A zipFileIndexCache.remove(cachedFile);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A public static void removeFromCache(File file) {
0N/A lock.lock();
0N/A try {
0N/A zipFileIndexCache.remove(file);
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A /** Sets already opened list of ZipFileIndexes from an outside client
0N/A * of the compiler. This functionality should be used in a non-batch clients of the compiler.
0N/A */
0N/A public static void setOpenedIndexes(List<ZipFileIndex>indexes) throws IllegalStateException {
0N/A lock.lock();
0N/A try {
0N/A if (zipFileIndexCache.isEmpty()) {
0N/A throw new IllegalStateException("Setting opened indexes should be called only when the ZipFileCache is empty. Call JavacFileManager.flush() before calling this method.");
0N/A }
0N/A
0N/A for (ZipFileIndex zfi : indexes) {
0N/A zipFileIndexCache.put(zfi.zipFile, zfi);
0N/A }
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A private ZipFileIndex(File zipFile, RelativeDirectory symbolFilePrefix, boolean writeIndex,
0N/A boolean useCache, String cacheLocation) throws IOException {
0N/A this.zipFile = zipFile;
56N/A this.symbolFilePrefix = symbolFilePrefix;
56N/A this.symbolFilePrefixLength = (symbolFilePrefix == null ? 0 :
102N/A symbolFilePrefix.getPath().getBytes("UTF-8").length);
0N/A this.writeIndex = writeIndex;
0N/A this.usePreindexedCache = useCache;
0N/A this.preindexedCacheLocation = cacheLocation;
0N/A
0N/A if (zipFile != null) {
0N/A this.zipFileLastModified = zipFile.lastModified();
0N/A }
0N/A
0N/A // Validate integrity of the zip file
0N/A checkIndex();
0N/A }
0N/A
0N/A public String toString() {
102N/A return "ZipFileIndex[" + zipFile + "]";
0N/A }
0N/A
0N/A // Just in case...
0N/A protected void finalize() {
0N/A closeFile();
0N/A }
0N/A
0N/A private boolean isUpToDate() {
0N/A if (zipFile != null &&
0N/A ((!NON_BATCH_MODE) || zipFileLastModified == zipFile.lastModified()) &&
0N/A hasPopulatedData) {
0N/A return true;
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Here we need to make sure that the ZipFileIndex is valid. Check the timestamp of the file and
0N/A * if its the same as the one at the time the index was build we don't need to reopen anything.
0N/A */
0N/A private void checkIndex() throws IOException {
0N/A boolean isUpToDate = true;
0N/A if (!isUpToDate()) {
0N/A closeFile();
0N/A isUpToDate = false;
0N/A }
0N/A
0N/A if (zipRandomFile != null || isUpToDate) {
0N/A lastReferenceTimeStamp = System.currentTimeMillis();
0N/A return;
0N/A }
0N/A
0N/A hasPopulatedData = true;
0N/A
0N/A if (readIndex()) {
0N/A lastReferenceTimeStamp = System.currentTimeMillis();
0N/A return;
0N/A }
0N/A
102N/A directories = Collections.<RelativeDirectory, DirectoryEntry>emptyMap();
102N/A allDirs = Collections.<RelativeDirectory>emptySet();
0N/A
0N/A try {
0N/A openFile();
0N/A long totalLength = zipRandomFile.length();
0N/A ZipDirectory directory = new ZipDirectory(zipRandomFile, 0L, totalLength, this);
0N/A directory.buildIndex();
0N/A } finally {
0N/A if (zipRandomFile != null) {
0N/A closeFile();
0N/A }
0N/A }
0N/A
0N/A lastReferenceTimeStamp = System.currentTimeMillis();
0N/A }
0N/A
0N/A private void openFile() throws FileNotFoundException {
0N/A if (zipRandomFile == null && zipFile != null) {
0N/A zipRandomFile = new RandomAccessFile(zipFile, "r");
0N/A }
0N/A }
0N/A
0N/A private void cleanupState() {
0N/A // Make sure there is a valid but empty index if the file doesn't exist
56N/A entries = Entry.EMPTY_ARRAY;
102N/A directories = Collections.<RelativeDirectory, DirectoryEntry>emptyMap();
0N/A zipFileLastModified = NOT_MODIFIED;
102N/A allDirs = Collections.<RelativeDirectory>emptySet();
0N/A }
0N/A
0N/A public void close() {
0N/A lock.lock();
0N/A try {
0N/A writeIndex();
0N/A closeFile();
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A private void closeFile() {
0N/A if (zipRandomFile != null) {
0N/A try {
0N/A zipRandomFile.close();
0N/A } catch (IOException ex) {
0N/A }
0N/A zipRandomFile = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the ZipFileIndexEntry for an absolute path, if there is one.
0N/A */
102N/A Entry getZipIndexEntry(RelativePath path) {
0N/A lock.lock();
0N/A try {
0N/A checkIndex();
102N/A DirectoryEntry de = directories.get(path.dirname());
102N/A String lookFor = path.basename();
0N/A return de == null ? null : de.getEntry(lookFor);
0N/A }
0N/A catch (IOException e) {
0N/A return null;
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a javac List of filenames within an absolute path in the ZipFileIndex.
0N/A */
102N/A public com.sun.tools.javac.util.List<String> getFiles(RelativeDirectory path) {
0N/A lock.lock();
0N/A try {
0N/A checkIndex();
0N/A
0N/A DirectoryEntry de = directories.get(path);
0N/A com.sun.tools.javac.util.List<String> ret = de == null ? null : de.getFiles();
0N/A
0N/A if (ret == null) {
0N/A return com.sun.tools.javac.util.List.<String>nil();
0N/A }
0N/A return ret;
0N/A }
0N/A catch (IOException e) {
0N/A return com.sun.tools.javac.util.List.<String>nil();
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public List<String> getDirectories(RelativeDirectory path) {
0N/A lock.lock();
0N/A try {
0N/A checkIndex();
0N/A
0N/A DirectoryEntry de = directories.get(path);
0N/A com.sun.tools.javac.util.List<String> ret = de == null ? null : de.getDirectories();
0N/A
0N/A if (ret == null) {
0N/A return com.sun.tools.javac.util.List.<String>nil();
0N/A }
0N/A
0N/A return ret;
0N/A }
0N/A catch (IOException e) {
0N/A return com.sun.tools.javac.util.List.<String>nil();
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public Set<RelativeDirectory> getAllDirectories() {
0N/A lock.lock();
0N/A try {
0N/A checkIndex();
0N/A if (allDirs == Collections.EMPTY_SET) {
102N/A allDirs = new HashSet<RelativeDirectory>(directories.keySet());
0N/A }
0N/A
0N/A return allDirs;
0N/A }
0N/A catch (IOException e) {
102N/A return Collections.<RelativeDirectory>emptySet();
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Tests if a specific path exists in the zip. This method will return true
0N/A * for file entries and directories.
0N/A *
0N/A * @param path A path within the zip.
0N/A * @return True if the path is a file or dir, false otherwise.
0N/A */
102N/A public boolean contains(RelativePath path) {
0N/A lock.lock();
0N/A try {
0N/A checkIndex();
0N/A return getZipIndexEntry(path) != null;
0N/A }
0N/A catch (IOException e) {
0N/A return false;
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public boolean isDirectory(RelativePath path) throws IOException {
0N/A lock.lock();
0N/A try {
0N/A // The top level in a zip file is always a directory.
102N/A if (path.getPath().length() == 0) {
0N/A lastReferenceTimeStamp = System.currentTimeMillis();
0N/A return true;
0N/A }
0N/A
0N/A checkIndex();
0N/A return directories.get(path) != null;
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public long getLastModified(RelativeFile path) throws IOException {
0N/A lock.lock();
0N/A try {
56N/A Entry entry = getZipIndexEntry(path);
0N/A if (entry == null)
0N/A throw new FileNotFoundException();
0N/A return entry.getLastModified();
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public int length(RelativeFile path) throws IOException {
0N/A lock.lock();
0N/A try {
56N/A Entry entry = getZipIndexEntry(path);
0N/A if (entry == null)
0N/A throw new FileNotFoundException();
0N/A
0N/A if (entry.isDir) {
0N/A return 0;
0N/A }
0N/A
0N/A byte[] header = getHeader(entry);
0N/A // entry is not compressed?
0N/A if (get2ByteLittleEndian(header, 8) == 0) {
0N/A return entry.compressedSize;
0N/A } else {
0N/A return entry.size;
0N/A }
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public byte[] read(RelativeFile path) throws IOException {
0N/A lock.lock();
0N/A try {
56N/A Entry entry = getZipIndexEntry(path);
0N/A if (entry == null)
102N/A throw new FileNotFoundException("Path not found in ZIP: " + path.path);
0N/A return read(entry);
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
56N/A byte[] read(Entry entry) throws IOException {
0N/A lock.lock();
0N/A try {
0N/A openFile();
0N/A byte[] result = readBytes(entry);
0N/A closeFile();
0N/A return result;
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
102N/A public int read(RelativeFile path, byte[] buffer) throws IOException {
0N/A lock.lock();
0N/A try {
56N/A Entry entry = getZipIndexEntry(path);
0N/A if (entry == null)
0N/A throw new FileNotFoundException();
0N/A return read(entry, buffer);
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
56N/A int read(Entry entry, byte[] buffer)
0N/A throws IOException {
0N/A lock.lock();
0N/A try {
0N/A int result = readBytes(entry, buffer);
0N/A return result;
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
56N/A private byte[] readBytes(Entry entry) throws IOException {
0N/A byte[] header = getHeader(entry);
0N/A int csize = entry.compressedSize;
0N/A byte[] cbuf = new byte[csize];
0N/A zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
0N/A zipRandomFile.readFully(cbuf, 0, csize);
0N/A
0N/A // is this compressed - offset 8 in the ZipEntry header
0N/A if (get2ByteLittleEndian(header, 8) == 0)
0N/A return cbuf;
0N/A
0N/A int size = entry.size;
0N/A byte[] buf = new byte[size];
0N/A if (inflate(cbuf, buf) != size)
0N/A throw new ZipException("corrupted zip file");
0N/A
0N/A return buf;
0N/A }
0N/A
0N/A /**
0N/A *
0N/A */
56N/A private int readBytes(Entry entry, byte[] buffer) throws IOException {
0N/A byte[] header = getHeader(entry);
0N/A
0N/A // entry is not compressed?
0N/A if (get2ByteLittleEndian(header, 8) == 0) {
0N/A zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
0N/A int offset = 0;
0N/A int size = buffer.length;
0N/A while (offset < size) {
0N/A int count = zipRandomFile.read(buffer, offset, size - offset);
0N/A if (count == -1)
0N/A break;
0N/A offset += count;
0N/A }
0N/A return entry.size;
0N/A }
0N/A
0N/A int csize = entry.compressedSize;
0N/A byte[] cbuf = new byte[csize];
0N/A zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
0N/A zipRandomFile.readFully(cbuf, 0, csize);
0N/A
0N/A int count = inflate(cbuf, buffer);
0N/A if (count == -1)
0N/A throw new ZipException("corrupted zip file");
0N/A
0N/A return entry.size;
0N/A }
0N/A
0N/A //----------------------------------------------------------------------------
0N/A // Zip utilities
0N/A //----------------------------------------------------------------------------
0N/A
56N/A private byte[] getHeader(Entry entry) throws IOException {
0N/A zipRandomFile.seek(entry.offset);
0N/A byte[] header = new byte[30];
0N/A zipRandomFile.readFully(header);
0N/A if (get4ByteLittleEndian(header, 0) != 0x04034b50)
0N/A throw new ZipException("corrupted zip file");
0N/A if ((get2ByteLittleEndian(header, 6) & 1) != 0)
0N/A throw new ZipException("encrypted zip file"); // offset 6 in the header of the ZipFileEntry
0N/A return header;
0N/A }
0N/A
0N/A /*
0N/A * Inflate using the java.util.zip.Inflater class
0N/A */
0N/A private static Inflater inflater;
0N/A private int inflate(byte[] src, byte[] dest) {
0N/A
0N/A // construct the inflater object or reuse an existing one
0N/A if (inflater == null)
0N/A inflater = new Inflater(true);
0N/A
0N/A synchronized (inflater) {
0N/A inflater.reset();
0N/A inflater.setInput(src);
0N/A try {
0N/A return inflater.inflate(dest);
0N/A } catch (DataFormatException ex) {
0N/A return -1;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * return the two bytes buf[pos], buf[pos+1] as an unsigned integer in little
0N/A * endian format.
0N/A */
0N/A private static int get2ByteLittleEndian(byte[] buf, int pos) {
0N/A return (buf[pos] & 0xFF) + ((buf[pos+1] & 0xFF) << 8);
0N/A }
0N/A
0N/A /**
0N/A * return the 4 bytes buf[i..i+3] as an integer in little endian format.
0N/A */
0N/A private static int get4ByteLittleEndian(byte[] buf, int pos) {
0N/A return (buf[pos] & 0xFF) + ((buf[pos + 1] & 0xFF) << 8) +
0N/A ((buf[pos + 2] & 0xFF) << 16) + ((buf[pos + 3] & 0xFF) << 24);
0N/A }
0N/A
0N/A /* ----------------------------------------------------------------------------
0N/A * ZipDirectory
0N/A * ----------------------------------------------------------------------------*/
0N/A
0N/A private class ZipDirectory {
102N/A private RelativeDirectory lastDir;
0N/A private int lastStart;
0N/A private int lastLen;
0N/A
0N/A byte[] zipDir;
0N/A RandomAccessFile zipRandomFile = null;
0N/A ZipFileIndex zipFileIndex = null;
0N/A
0N/A public ZipDirectory(RandomAccessFile zipRandomFile, long start, long end, ZipFileIndex index) throws IOException {
0N/A this.zipRandomFile = zipRandomFile;
0N/A this.zipFileIndex = index;
0N/A
0N/A findCENRecord(start, end);
0N/A }
0N/A
0N/A /*
0N/A * Reads zip file central directory.
0N/A * For more details see readCEN in zip_util.c from the JDK sources.
0N/A * This is a Java port of that function.
0N/A */
0N/A private void findCENRecord(long start, long end) throws IOException {
0N/A long totalLength = end - start;
0N/A int endbuflen = 1024;
0N/A byte[] endbuf = new byte[endbuflen];
0N/A long endbufend = end - start;
0N/A
0N/A // There is a variable-length field after the dir offset record. We need to do consequential search.
0N/A while (endbufend >= 22) {
0N/A if (endbufend < endbuflen)
0N/A endbuflen = (int)endbufend;
0N/A long endbufpos = endbufend - endbuflen;
0N/A zipRandomFile.seek(start + endbufpos);
0N/A zipRandomFile.readFully(endbuf, 0, endbuflen);
0N/A int i = endbuflen - 22;
0N/A while (i >= 0 &&
0N/A !(endbuf[i] == 0x50 &&
0N/A endbuf[i + 1] == 0x4b &&
0N/A endbuf[i + 2] == 0x05 &&
0N/A endbuf[i + 3] == 0x06 &&
0N/A endbufpos + i + 22 +
0N/A get2ByteLittleEndian(endbuf, i + 20) == totalLength)) {
0N/A i--;
0N/A }
0N/A
0N/A if (i >= 0) {
0N/A zipDir = new byte[get4ByteLittleEndian(endbuf, i + 12) + 2];
0N/A zipDir[0] = endbuf[i + 10];
0N/A zipDir[1] = endbuf[i + 11];
0N/A zipRandomFile.seek(start + get4ByteLittleEndian(endbuf, i + 16));
0N/A zipRandomFile.readFully(zipDir, 2, zipDir.length - 2);
0N/A return;
0N/A } else {
0N/A endbufend = endbufpos + 21;
0N/A }
0N/A }
0N/A throw new ZipException("cannot read zip file");
0N/A }
102N/A
0N/A private void buildIndex() throws IOException {
0N/A int entryCount = get2ByteLittleEndian(zipDir, 0);
0N/A
0N/A // Add each of the files
0N/A if (entryCount > 0) {
102N/A directories = new HashMap<RelativeDirectory, DirectoryEntry>();
56N/A ArrayList<Entry> entryList = new ArrayList<Entry>();
0N/A int pos = 2;
0N/A for (int i = 0; i < entryCount; i++) {
0N/A pos = readEntry(pos, entryList, directories);
0N/A }
0N/A
0N/A // Add the accumulated dirs into the same list
102N/A for (RelativeDirectory d: directories.keySet()) {
102N/A // use shared RelativeDirectory objects for parent dirs
102N/A RelativeDirectory parent = getRelativeDirectory(d.dirname().getPath());
102N/A String file = d.basename();
102N/A Entry zipFileIndexEntry = new Entry(parent, file);
0N/A zipFileIndexEntry.isDir = true;
0N/A entryList.add(zipFileIndexEntry);
0N/A }
0N/A
56N/A entries = entryList.toArray(new Entry[entryList.size()]);
0N/A Arrays.sort(entries);
0N/A } else {
0N/A cleanupState();
0N/A }
0N/A }
0N/A
56N/A private int readEntry(int pos, List<Entry> entryList,
102N/A Map<RelativeDirectory, DirectoryEntry> directories) throws IOException {
0N/A if (get4ByteLittleEndian(zipDir, pos) != 0x02014b50) {
0N/A throw new ZipException("cannot read zip file entry");
0N/A }
0N/A
0N/A int dirStart = pos + 46;
0N/A int fileStart = dirStart;
0N/A int fileEnd = fileStart + get2ByteLittleEndian(zipDir, pos + 28);
0N/A
0N/A if (zipFileIndex.symbolFilePrefixLength != 0 &&
0N/A ((fileEnd - fileStart) >= symbolFilePrefixLength)) {
0N/A dirStart += zipFileIndex.symbolFilePrefixLength;
0N/A fileStart += zipFileIndex.symbolFilePrefixLength;
0N/A }
102N/A // Force any '\' to '/'. Keep the position of the last separator.
0N/A for (int index = fileStart; index < fileEnd; index++) {
0N/A byte nextByte = zipDir[index];
102N/A if (nextByte == (byte)'\\') {
102N/A zipDir[index] = (byte)'/';
102N/A fileStart = index + 1;
102N/A } else if (nextByte == (byte)'/') {
0N/A fileStart = index + 1;
0N/A }
0N/A }
0N/A
102N/A RelativeDirectory directory = null;
0N/A if (fileStart == dirStart)
102N/A directory = getRelativeDirectory("");
0N/A else if (lastDir != null && lastLen == fileStart - dirStart - 1) {
0N/A int index = lastLen - 1;
0N/A while (zipDir[lastStart + index] == zipDir[dirStart + index]) {
0N/A if (index == 0) {
0N/A directory = lastDir;
0N/A break;
0N/A }
0N/A index--;
0N/A }
0N/A }
0N/A
0N/A // Sub directories
0N/A if (directory == null) {
0N/A lastStart = dirStart;
0N/A lastLen = fileStart - dirStart - 1;
0N/A
102N/A directory = getRelativeDirectory(new String(zipDir, dirStart, lastLen, "UTF-8"));
0N/A lastDir = directory;
0N/A
0N/A // Enter also all the parent directories
102N/A RelativeDirectory tempDirectory = directory;
0N/A
0N/A while (directories.get(tempDirectory) == null) {
0N/A directories.put(tempDirectory, new DirectoryEntry(tempDirectory, zipFileIndex));
102N/A if (tempDirectory.path.indexOf("/") == tempDirectory.path.length() - 1)
0N/A break;
102N/A else {
102N/A // use shared RelativeDirectory objects for parent dirs
102N/A tempDirectory = getRelativeDirectory(tempDirectory.dirname().getPath());
102N/A }
0N/A }
0N/A }
0N/A else {
0N/A if (directories.get(directory) == null) {
0N/A directories.put(directory, new DirectoryEntry(directory, zipFileIndex));
0N/A }
0N/A }
0N/A
0N/A // For each dir create also a file
0N/A if (fileStart != fileEnd) {
56N/A Entry entry = new Entry(directory,
0N/A new String(zipDir, fileStart, fileEnd - fileStart, "UTF-8"));
0N/A
0N/A entry.setNativeTime(get4ByteLittleEndian(zipDir, pos + 12));
0N/A entry.compressedSize = get4ByteLittleEndian(zipDir, pos + 20);
0N/A entry.size = get4ByteLittleEndian(zipDir, pos + 24);
0N/A entry.offset = get4ByteLittleEndian(zipDir, pos + 42);
0N/A entryList.add(entry);
0N/A }
0N/A
0N/A return pos + 46 +
0N/A get2ByteLittleEndian(zipDir, pos + 28) +
0N/A get2ByteLittleEndian(zipDir, pos + 30) +
0N/A get2ByteLittleEndian(zipDir, pos + 32);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the last modified timestamp of a zip file.
0N/A * @return long
0N/A */
0N/A public long getZipFileLastModified() throws IOException {
0N/A lock.lock();
0N/A try {
0N/A checkIndex();
0N/A return zipFileLastModified;
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A /** ------------------------------------------------------------------------
0N/A * DirectoryEntry class
0N/A * -------------------------------------------------------------------------*/
56N/A
0N/A static class DirectoryEntry {
0N/A private boolean filesInited;
0N/A private boolean directoriesInited;
0N/A private boolean zipFileEntriesInited;
0N/A private boolean entriesInited;
0N/A
0N/A private long writtenOffsetOffset = 0;
0N/A
102N/A private RelativeDirectory dirName;
0N/A
0N/A private com.sun.tools.javac.util.List<String> zipFileEntriesFiles = com.sun.tools.javac.util.List.<String>nil();
0N/A private com.sun.tools.javac.util.List<String> zipFileEntriesDirectories = com.sun.tools.javac.util.List.<String>nil();
56N/A private com.sun.tools.javac.util.List<Entry> zipFileEntries = com.sun.tools.javac.util.List.<Entry>nil();
0N/A
56N/A private List<Entry> entries = new ArrayList<Entry>();
0N/A
0N/A private ZipFileIndex zipFileIndex;
0N/A
0N/A private int numEntries;
0N/A
102N/A DirectoryEntry(RelativeDirectory dirName, ZipFileIndex index) {
102N/A filesInited = false;
0N/A directoriesInited = false;
0N/A entriesInited = false;
0N/A
102N/A this.dirName = dirName;
0N/A this.zipFileIndex = index;
0N/A }
0N/A
0N/A private com.sun.tools.javac.util.List<String> getFiles() {
102N/A if (!filesInited) {
102N/A initEntries();
102N/A for (Entry e : entries) {
102N/A if (!e.isDir) {
102N/A zipFileEntriesFiles = zipFileEntriesFiles.append(e.name);
102N/A }
102N/A }
102N/A filesInited = true;
0N/A }
0N/A return zipFileEntriesFiles;
0N/A }
0N/A
0N/A private com.sun.tools.javac.util.List<String> getDirectories() {
102N/A if (!directoriesInited) {
102N/A initEntries();
102N/A for (Entry e : entries) {
102N/A if (e.isDir) {
102N/A zipFileEntriesDirectories = zipFileEntriesDirectories.append(e.name);
102N/A }
102N/A }
102N/A directoriesInited = true;
0N/A }
0N/A return zipFileEntriesDirectories;
0N/A }
0N/A
56N/A private com.sun.tools.javac.util.List<Entry> getEntries() {
102N/A if (!zipFileEntriesInited) {
102N/A initEntries();
102N/A zipFileEntries = com.sun.tools.javac.util.List.nil();
102N/A for (Entry zfie : entries) {
102N/A zipFileEntries = zipFileEntries.append(zfie);
102N/A }
102N/A zipFileEntriesInited = true;
0N/A }
0N/A return zipFileEntries;
0N/A }
0N/A
56N/A private Entry getEntry(String rootName) {
0N/A initEntries();
56N/A int index = Collections.binarySearch(entries, new Entry(dirName, rootName));
0N/A if (index < 0) {
0N/A return null;
0N/A }
0N/A
0N/A return entries.get(index);
0N/A }
0N/A
0N/A private void initEntries() {
0N/A if (entriesInited) {
0N/A return;
0N/A }
0N/A
0N/A if (!zipFileIndex.readFromIndex) {
0N/A int from = -Arrays.binarySearch(zipFileIndex.entries,
56N/A new Entry(dirName, ZipFileIndex.MIN_CHAR)) - 1;
0N/A int to = -Arrays.binarySearch(zipFileIndex.entries,
56N/A new Entry(dirName, MAX_CHAR)) - 1;
0N/A
0N/A for (int i = from; i < to; i++) {
0N/A entries.add(zipFileIndex.entries[i]);
0N/A }
0N/A } else {
0N/A File indexFile = zipFileIndex.getIndexFile();
0N/A if (indexFile != null) {
0N/A RandomAccessFile raf = null;
0N/A try {
0N/A raf = new RandomAccessFile(indexFile, "r");
0N/A raf.seek(writtenOffsetOffset);
0N/A
0N/A for (int nFiles = 0; nFiles < numEntries; nFiles++) {
0N/A // Read the name bytes
0N/A int zfieNameBytesLen = raf.readInt();
0N/A byte [] zfieNameBytes = new byte[zfieNameBytesLen];
0N/A raf.read(zfieNameBytes);
0N/A String eName = new String(zfieNameBytes, "UTF-8");
0N/A
0N/A // Read isDir
0N/A boolean eIsDir = raf.readByte() == (byte)0 ? false : true;
0N/A
0N/A // Read offset of bytes in the real Jar/Zip file
0N/A int eOffset = raf.readInt();
0N/A
0N/A // Read size of the file in the real Jar/Zip file
0N/A int eSize = raf.readInt();
0N/A
0N/A // Read compressed size of the file in the real Jar/Zip file
0N/A int eCsize = raf.readInt();
0N/A
0N/A // Read java time stamp of the file in the real Jar/Zip file
0N/A long eJavaTimestamp = raf.readLong();
0N/A
56N/A Entry rfie = new Entry(dirName, eName);
0N/A rfie.isDir = eIsDir;
0N/A rfie.offset = eOffset;
0N/A rfie.size = eSize;
0N/A rfie.compressedSize = eCsize;
0N/A rfie.javatime = eJavaTimestamp;
0N/A entries.add(rfie);
0N/A }
0N/A } catch (Throwable t) {
0N/A // Do nothing
0N/A } finally {
0N/A try {
0N/A if (raf == null) {
0N/A raf.close();
0N/A }
0N/A } catch (Throwable t) {
0N/A // Do nothing
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A entriesInited = true;
0N/A }
0N/A
56N/A List<Entry> getEntriesAsCollection() {
0N/A initEntries();
0N/A
0N/A return entries;
0N/A }
0N/A }
0N/A
0N/A private boolean readIndex() {
0N/A if (triedToReadIndex || !usePreindexedCache) {
0N/A return false;
0N/A }
0N/A
0N/A boolean ret = false;
0N/A lock.lock();
0N/A try {
0N/A triedToReadIndex = true;
0N/A RandomAccessFile raf = null;
0N/A try {
0N/A File indexFileName = getIndexFile();
0N/A raf = new RandomAccessFile(indexFileName, "r");
0N/A
0N/A long fileStamp = raf.readLong();
0N/A if (zipFile.lastModified() != fileStamp) {
0N/A ret = false;
0N/A } else {
102N/A directories = new HashMap<RelativeDirectory, DirectoryEntry>();
0N/A int numDirs = raf.readInt();
0N/A for (int nDirs = 0; nDirs < numDirs; nDirs++) {
0N/A int dirNameBytesLen = raf.readInt();
0N/A byte [] dirNameBytes = new byte[dirNameBytesLen];
0N/A raf.read(dirNameBytes);
0N/A
102N/A RelativeDirectory dirNameStr = getRelativeDirectory(new String(dirNameBytes, "UTF-8"));
0N/A DirectoryEntry de = new DirectoryEntry(dirNameStr, this);
0N/A de.numEntries = raf.readInt();
0N/A de.writtenOffsetOffset = raf.readLong();
0N/A directories.put(dirNameStr, de);
0N/A }
0N/A ret = true;
0N/A zipFileLastModified = fileStamp;
0N/A }
0N/A } catch (Throwable t) {
0N/A // Do nothing
0N/A } finally {
0N/A if (raf != null) {
0N/A try {
0N/A raf.close();
0N/A } catch (Throwable tt) {
0N/A // Do nothing
0N/A }
0N/A }
0N/A }
0N/A if (ret == true) {
0N/A readFromIndex = true;
0N/A }
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A
0N/A return ret;
0N/A }
0N/A
0N/A private boolean writeIndex() {
0N/A boolean ret = false;
0N/A if (readFromIndex || !usePreindexedCache) {
0N/A return true;
0N/A }
0N/A
0N/A if (!writeIndex) {
0N/A return true;
0N/A }
0N/A
0N/A File indexFile = getIndexFile();
0N/A if (indexFile == null) {
0N/A return false;
0N/A }
0N/A
0N/A RandomAccessFile raf = null;
0N/A long writtenSoFar = 0;
0N/A try {
0N/A raf = new RandomAccessFile(indexFile, "rw");
0N/A
0N/A raf.writeLong(zipFileLastModified);
0N/A writtenSoFar += 8;
0N/A
0N/A List<DirectoryEntry> directoriesToWrite = new ArrayList<DirectoryEntry>();
102N/A Map<RelativeDirectory, Long> offsets = new HashMap<RelativeDirectory, Long>();
0N/A raf.writeInt(directories.keySet().size());
0N/A writtenSoFar += 4;
0N/A
102N/A for (RelativeDirectory dirName: directories.keySet()) {
0N/A DirectoryEntry dirEntry = directories.get(dirName);
0N/A
0N/A directoriesToWrite.add(dirEntry);
0N/A
0N/A // Write the dir name bytes
102N/A byte [] dirNameBytes = dirName.getPath().getBytes("UTF-8");
0N/A int dirNameBytesLen = dirNameBytes.length;
0N/A raf.writeInt(dirNameBytesLen);
0N/A writtenSoFar += 4;
0N/A
0N/A raf.write(dirNameBytes);
0N/A writtenSoFar += dirNameBytesLen;
0N/A
0N/A // Write the number of files in the dir
183N/A List<Entry> dirEntries = dirEntry.getEntriesAsCollection();
0N/A raf.writeInt(dirEntries.size());
0N/A writtenSoFar += 4;
0N/A
0N/A offsets.put(dirName, new Long(writtenSoFar));
0N/A
0N/A // Write the offset of the file's data in the dir
0N/A dirEntry.writtenOffsetOffset = 0L;
0N/A raf.writeLong(0L);
0N/A writtenSoFar += 8;
0N/A }
0N/A
0N/A for (DirectoryEntry de : directoriesToWrite) {
0N/A // Fix up the offset in the directory table
0N/A long currFP = raf.getFilePointer();
0N/A
0N/A long offsetOffset = offsets.get(de.dirName).longValue();
0N/A raf.seek(offsetOffset);
0N/A raf.writeLong(writtenSoFar);
0N/A
0N/A raf.seek(currFP);
0N/A
0N/A // Now write each of the files in the DirectoryEntry
56N/A List<Entry> entries = de.getEntriesAsCollection();
56N/A for (Entry zfie : entries) {
0N/A // Write the name bytes
0N/A byte [] zfieNameBytes = zfie.name.getBytes("UTF-8");
0N/A int zfieNameBytesLen = zfieNameBytes.length;
0N/A raf.writeInt(zfieNameBytesLen);
0N/A writtenSoFar += 4;
0N/A raf.write(zfieNameBytes);
0N/A writtenSoFar += zfieNameBytesLen;
0N/A
0N/A // Write isDir
0N/A raf.writeByte(zfie.isDir ? (byte)1 : (byte)0);
0N/A writtenSoFar += 1;
0N/A
0N/A // Write offset of bytes in the real Jar/Zip file
0N/A raf.writeInt(zfie.offset);
0N/A writtenSoFar += 4;
0N/A
0N/A // Write size of the file in the real Jar/Zip file
0N/A raf.writeInt(zfie.size);
0N/A writtenSoFar += 4;
0N/A
0N/A // Write compressed size of the file in the real Jar/Zip file
0N/A raf.writeInt(zfie.compressedSize);
0N/A writtenSoFar += 4;
0N/A
0N/A // Write java time stamp of the file in the real Jar/Zip file
0N/A raf.writeLong(zfie.getLastModified());
0N/A writtenSoFar += 8;
0N/A }
0N/A }
0N/A } catch (Throwable t) {
0N/A // Do nothing
0N/A } finally {
0N/A try {
0N/A if (raf != null) {
0N/A raf.close();
0N/A }
0N/A } catch(IOException ioe) {
0N/A // Do nothing
0N/A }
0N/A }
0N/A
0N/A return ret;
0N/A }
0N/A
0N/A public boolean writeZipIndex() {
0N/A lock.lock();
0N/A try {
0N/A return writeIndex();
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A }
0N/A
0N/A private File getIndexFile() {
0N/A if (zipIndexFile == null) {
0N/A if (zipFile == null) {
0N/A return null;
0N/A }
0N/A
0N/A zipIndexFile = new File((preindexedCacheLocation == null ? "" : preindexedCacheLocation) +
0N/A zipFile.getName() + ".index");
0N/A }
0N/A
0N/A return zipIndexFile;
0N/A }
0N/A
0N/A public File getZipFile() {
0N/A return zipFile;
0N/A }
56N/A
102N/A private RelativeDirectory getRelativeDirectory(String path) {
102N/A RelativeDirectory rd;
102N/A SoftReference<RelativeDirectory> ref = relativeDirectoryCache.get(path);
102N/A if (ref != null) {
102N/A rd = ref.get();
102N/A if (rd != null)
102N/A return rd;
102N/A }
102N/A rd = new RelativeDirectory(path);
102N/A relativeDirectoryCache.put(path, new SoftReference<RelativeDirectory>(rd));
102N/A return rd;
102N/A }
56N/A
56N/A static class Entry implements Comparable<Entry> {
56N/A public static final Entry[] EMPTY_ARRAY = {};
56N/A
56N/A // Directory related
102N/A RelativeDirectory dir;
56N/A boolean isDir;
56N/A
56N/A // File related
56N/A String name;
56N/A
56N/A int offset;
56N/A int size;
56N/A int compressedSize;
56N/A long javatime;
56N/A
56N/A private int nativetime;
56N/A
102N/A public Entry(RelativePath path) {
102N/A this(path.dirname(), path.basename());
56N/A }
56N/A
102N/A public Entry(RelativeDirectory directory, String name) {
102N/A this.dir = directory;
56N/A this.name = name;
56N/A }
56N/A
56N/A public String getName() {
102N/A return new RelativeFile(dir, name).getPath();
56N/A }
56N/A
56N/A public String getFileName() {
56N/A return name;
56N/A }
56N/A
56N/A public long getLastModified() {
56N/A if (javatime == 0) {
56N/A javatime = dosToJavaTime(nativetime);
56N/A }
56N/A return javatime;
56N/A }
56N/A
70N/A // based on dosToJavaTime in java.util.Zip, but avoiding the
70N/A // use of deprecated Date constructor
70N/A private static long dosToJavaTime(int dtime) {
70N/A Calendar c = Calendar.getInstance();
70N/A c.set(Calendar.YEAR, ((dtime >> 25) & 0x7f) + 1980);
70N/A c.set(Calendar.MONTH, ((dtime >> 21) & 0x0f) - 1);
70N/A c.set(Calendar.DATE, ((dtime >> 16) & 0x1f));
70N/A c.set(Calendar.HOUR_OF_DAY, ((dtime >> 11) & 0x1f));
70N/A c.set(Calendar.MINUTE, ((dtime >> 5) & 0x3f));
70N/A c.set(Calendar.SECOND, ((dtime << 1) & 0x3e));
70N/A c.set(Calendar.MILLISECOND, 0);
70N/A return c.getTimeInMillis();
56N/A }
56N/A
56N/A void setNativeTime(int natTime) {
56N/A nativetime = natTime;
56N/A }
56N/A
56N/A public boolean isDirectory() {
56N/A return isDir;
56N/A }
56N/A
56N/A public int compareTo(Entry other) {
102N/A RelativeDirectory otherD = other.dir;
56N/A if (dir != otherD) {
56N/A int c = dir.compareTo(otherD);
56N/A if (c != 0)
56N/A return c;
56N/A }
56N/A return name.compareTo(other.name);
56N/A }
56N/A
102N/A @Override
102N/A public boolean equals(Object o) {
102N/A if (!(o instanceof Entry))
102N/A return false;
102N/A Entry other = (Entry) o;
102N/A return dir.equals(other.dir) && name.equals(other.name);
102N/A }
102N/A
102N/A @Override
102N/A public int hashCode() {
102N/A int hash = 7;
102N/A hash = 97 * hash + (this.dir != null ? this.dir.hashCode() : 0);
102N/A hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
102N/A return hash;
102N/A }
102N/A
56N/A
56N/A public String toString() {
56N/A return isDir ? ("Dir:" + dir + " : " + name) :
56N/A (dir + ":" + name);
56N/A }
56N/A }
56N/A
0N/A}