35N/A/*
1231N/A * Copyright (c) 2007, 2012, Oracle and/or its affiliates. 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
553N/A * published by the Free Software Foundation. Oracle designates this
35N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle 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 *
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.
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;
423N/Aimport java.lang.ref.Reference;
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;
1231N/Aimport java.util.LinkedHashMap;
49N/Aimport java.util.HashMap;
49N/Aimport java.util.HashSet;
0N/Aimport java.util.List;
49N/Aimport java.util.Map;
49N/Aimport java.util.Set;
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
838N/A/**
838N/A * This class implements the building of index of a zip archive and access to
838N/A * its context. It also uses a prebuilt index if available.
838N/A * It supports invocations where it will serialize an optimized zip index file
838N/A * to disk.
0N/A *
838N/A * In order to use a secondary index file, set "usezipindex" in the Options
838N/A * object when JavacFileManager is invoked. (You can pass "-XDusezipindex" on
838N/A * the command line.)
0N/A *
838N/A * Location where to look for/generate optimized zip index files can be
838N/A * provided using "-XDcachezipindexdir=<directory>". If this flag is not
838N/A * provided, the default location is the value of the "java.io.tmpdir" system
838N/A * property.
0N/A *
838N/A * If "-XDwritezipindexfiles" is specified, there will be new optimized index
838N/A * file created for each archive, used by the compiler for compilation, at the
838N/A * location specified by the "cachezipindexdir" option.
0N/A *
838N/A * If system property nonBatchMode option is specified the compiler will use
838N/A * timestamp checking to reindex the zip files if it is needed. In batch mode
838N/A * the timestamps are not checked and the compiler uses the cached indexes.
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>
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
0N/A private static boolean NON_BATCH_MODE = System.getProperty("nonBatchMode") != null;// TODO: Use -XD compiler switch for this.
0N/A
838N/A private Map<RelativeDirectory, DirectoryEntry> directories =
838N/A Collections.<RelativeDirectory, DirectoryEntry>emptyMap();
838N/A private Set<RelativeDirectory> allDirs =
838N/A Collections.<RelativeDirectory>emptySet();
0N/A
0N/A // ZipFileIndex data entries
838N/A final File zipFile;
423N/A private Reference<File> absFileRef;
838N/A 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;
838N/A private final int symbolFilePrefixLength;
0N/A private boolean hasPopulatedData = false;
838N/A long lastReferenceTimeStamp = NOT_MODIFIED;
0N/A
838N/A private final boolean usePreindexedCache;
838N/A private final String preindexedCacheLocation;
0N/A
0N/A private boolean writeIndex = false;
0N/A
838N/A private Map<String, SoftReference<RelativeDirectory>> relativeDirectoryCache =
102N/A new HashMap<String, SoftReference<RelativeDirectory>>();
102N/A
0N/A
838N/A public synchronized boolean isOpen() {
838N/A return (zipRandomFile != null);
0N/A }
0N/A
838N/A 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
838N/A @Override
0N/A public String toString() {
102N/A return "ZipFileIndex[" + zipFile + "]";
0N/A }
0N/A
0N/A // Just in case...
838N/A @Override
838N/A protected void finalize() throws Throwable {
0N/A closeFile();
838N/A super.finalize();
0N/A }
0N/A
0N/A private boolean isUpToDate() {
838N/A if (zipFile != null
838N/A && ((!NON_BATCH_MODE) || zipFileLastModified == zipFile.lastModified())
838N/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
838N/A public synchronized void close() {
838N/A writeIndex();
838N/A closeFile();
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 /**
838N/A * Returns the ZipFileIndexEntry for a path, if there is one.
0N/A */
838N/A synchronized Entry getZipIndexEntry(RelativePath path) {
0N/A try {
0N/A checkIndex();
102N/A DirectoryEntry de = directories.get(path.dirname());
102N/A String lookFor = path.basename();
838N/A return (de == null) ? null : de.getEntry(lookFor);
0N/A }
0N/A catch (IOException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
838N/A * Returns a javac List of filenames within a directory in the ZipFileIndex.
0N/A */
838N/A public synchronized com.sun.tools.javac.util.List<String> getFiles(RelativeDirectory path) {
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 }
0N/A
838N/A public synchronized List<String> getDirectories(RelativeDirectory path) {
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 }
0N/A
838N/A public synchronized Set<RelativeDirectory> getAllDirectories() {
0N/A try {
0N/A checkIndex();
0N/A if (allDirs == Collections.EMPTY_SET) {
1231N/A allDirs = new java.util.LinkedHashSet<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 }
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 */
838N/A public synchronized boolean contains(RelativePath path) {
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 }
838N/A }
838N/A
838N/A public synchronized boolean isDirectory(RelativePath path) throws IOException {
838N/A // The top level in a zip file is always a directory.
838N/A if (path.getPath().length() == 0) {
838N/A lastReferenceTimeStamp = System.currentTimeMillis();
838N/A return true;
0N/A }
838N/A
838N/A checkIndex();
838N/A return directories.get(path) != null;
0N/A }
0N/A
838N/A public synchronized long getLastModified(RelativeFile path) throws IOException {
838N/A Entry entry = getZipIndexEntry(path);
838N/A if (entry == null)
838N/A throw new FileNotFoundException();
838N/A return entry.getLastModified();
0N/A }
0N/A
838N/A public synchronized int length(RelativeFile path) throws IOException {
838N/A Entry entry = getZipIndexEntry(path);
838N/A if (entry == null)
838N/A throw new FileNotFoundException();
838N/A
838N/A if (entry.isDir) {
838N/A return 0;
0N/A }
838N/A
838N/A byte[] header = getHeader(entry);
838N/A // entry is not compressed?
838N/A if (get2ByteLittleEndian(header, 8) == 0) {
838N/A return entry.compressedSize;
838N/A } else {
838N/A return entry.size;
0N/A }
0N/A }
0N/A
838N/A public synchronized byte[] read(RelativeFile path) throws IOException {
838N/A Entry entry = getZipIndexEntry(path);
838N/A if (entry == null)
838N/A throw new FileNotFoundException("Path not found in ZIP: " + path.path);
838N/A return read(entry);
0N/A }
0N/A
838N/A synchronized byte[] read(Entry entry) throws IOException {
838N/A openFile();
838N/A byte[] result = readBytes(entry);
838N/A closeFile();
838N/A return result;
0N/A }
0N/A
838N/A public synchronized int read(RelativeFile path, byte[] buffer) throws IOException {
838N/A Entry entry = getZipIndexEntry(path);
838N/A if (entry == null)
838N/A throw new FileNotFoundException();
838N/A return read(entry, buffer);
0N/A }
0N/A
838N/A synchronized int read(Entry entry, byte[] buffer)
0N/A throws IOException {
838N/A int result = readBytes(entry, buffer);
838N/A return result;
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 */
838N/A private SoftReference<Inflater> inflaterRef;
0N/A private int inflate(byte[] src, byte[] dest) {
838N/A Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());
0N/A
0N/A // construct the inflater object or reuse an existing one
0N/A if (inflater == null)
838N/A inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));
0N/A
838N/A inflater.reset();
838N/A inflater.setInput(src);
838N/A try {
838N/A return inflater.inflate(dest);
838N/A } catch (DataFormatException ex) {
838N/A return -1;
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;
922N/A hasValidHeader();
922N/A findCENRecord(start, end);
922N/A }
0N/A
922N/A /*
922N/A * the zip entry signature should be at offset 0, otherwise allow the
922N/A * calling logic to take evasive action by throwing ZipFormatException.
922N/A */
922N/A private boolean hasValidHeader() throws IOException {
922N/A final long pos = zipRandomFile.getFilePointer();
922N/A try {
922N/A if (zipRandomFile.read() == 'P') {
922N/A if (zipRandomFile.read() == 'K') {
922N/A if (zipRandomFile.read() == 0x03) {
922N/A if (zipRandomFile.read() == 0x04) {
922N/A return true;
922N/A }
922N/A }
922N/A }
922N/A }
922N/A } finally {
922N/A zipRandomFile.seek(pos);
922N/A }
922N/A throw new ZipFormatException("invalid zip magic");
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];
922N/A int sz = get4ByteLittleEndian(endbuf, i + 16);
922N/A // a negative offset or the entries field indicates a
922N/A // potential zip64 archive
922N/A if (sz < 0 || get2ByteLittleEndian(zipDir, 0) == 0xffff) {
922N/A throw new ZipFormatException("detected a zip64 archive");
922N/A }
922N/A zipRandomFile.seek(start + sz);
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) {
1231N/A directories = new LinkedHashMap<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 {
838N/A synchronized (this) {
0N/A checkIndex();
0N/A return zipFileLastModified;
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 {
443N/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;
838N/A synchronized (this) {
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 {
1231N/A directories = new LinkedHashMap<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
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
838N/A List<Entry> list = de.getEntriesAsCollection();
838N/A for (Entry zfie : list) {
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() {
838N/A synchronized (this) {
0N/A return writeIndex();
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
423N/A File getAbsoluteFile() {
423N/A File absFile = (absFileRef == null ? null : absFileRef.get());
423N/A if (absFile == null) {
423N/A absFile = zipFile.getAbsoluteFile();
423N/A absFileRef = new SoftReference<File>(absFile);
423N/A }
423N/A return absFile;
423N/A }
423N/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
838N/A @Override
56N/A public String toString() {
56N/A return isDir ? ("Dir:" + dir + " : " + name) :
56N/A (dir + ":" + name);
56N/A }
56N/A }
56N/A
922N/A /*
922N/A * Exception primarily used to implement a failover, used exclusively here.
922N/A */
922N/A
922N/A static final class ZipFormatException extends IOException {
922N/A private static final long serialVersionUID = 8000196834066748623L;
922N/A protected ZipFormatException(String message) {
922N/A super(message);
922N/A }
922N/A
922N/A protected ZipFormatException(String message, Throwable cause) {
922N/A super(message, cause);
922N/A }
922N/A }
0N/A}