langtools/src/share/classes/com/sun/tools/javac/zip/ZipFileIndex.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 655 1ebc7ce89018
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
package com.sun.tools.javac.zip;
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
import java.text.MessageFormat;
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
import java.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
import java.util.concurrent.locks.ReentrantLock;
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
import java.util.zip.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
/** This class implements building of index of a zip archive and access to it's context.
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 *  It also uses prebuild index if available. It supports invocations where it will
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 *  serialize an optimized zip index file to disk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *  In oreder to use secondary index file make sure the option "usezipindex" is in the Options object,
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 *  when JavacFileManager is invoked. (You can pass "-XDusezipindex" on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 *  Location where to look for/generate optimized zip index files can be provided using
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *  "-XDcachezipindexdir=<directory>". If this flag is not provided, the dfault location is
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 *  the value of the "java.io.tmpdir" system property.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 *  If key "-XDwritezipindexfiles" is specified, there will be new optimized index file
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 *  created for each archive, used by the compiler for compilation, at location,
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 *  specified by "cachezipindexdir" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * If nonBatchMode option is specified (-XDnonBatchMode) the compiler will use timestamp
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * checking to reindex the zip files if it is needed. In batch mode the timestamps are not checked
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * and the compiler uses the cached indexes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
public class ZipFileIndex {
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
    private static final String MIN_CHAR = String.valueOf(Character.MIN_VALUE);
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
    private static final String MAX_CHAR = String.valueOf(Character.MAX_VALUE);
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
    public final static long NOT_MODIFIED = Long.MIN_VALUE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
    private static Map<File, ZipFileIndex> zipFileIndexCache = new HashMap<File, ZipFileIndex>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
    private static ReentrantLock lock = new ReentrantLock();
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
    private static boolean NON_BATCH_MODE = System.getProperty("nonBatchMode") != null;// TODO: Use -XD compiler switch for this.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    private Map<String, DirectoryEntry> directories = Collections.<String, DirectoryEntry>emptyMap();
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    private Set<String> allDirs = Collections.<String>emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    // ZipFileIndex data entries
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    private File zipFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    private long zipFileLastModified = NOT_MODIFIED;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    private RandomAccessFile zipRandomFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private ZipFileIndexEntry[] entries;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private boolean readFromIndex = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private File zipIndexFile = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    private boolean triedToReadIndex = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    private int symbolFilePrefixLength = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private boolean hasPopulatedData = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private long lastReferenceTimeStamp = NOT_MODIFIED;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private boolean usePreindexedCache = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    private String preindexedCacheLocation = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    private boolean writeIndex = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * Returns a list of all ZipFileIndex entries
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * @return A list of ZipFileIndex entries, or an empty list
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public static List<ZipFileIndex> getZipFileIndexes() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        return getZipFileIndexes(false);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Returns a list of all ZipFileIndex entries
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * @param openedOnly If true it returns a list of only opened ZipFileIndex entries, otherwise
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     *                   all ZipFileEntry(s) are included into the list.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * @return A list of ZipFileIndex entries, or an empty list
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public static List<ZipFileIndex> getZipFileIndexes(boolean openedOnly) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        List<ZipFileIndex> zipFileIndexes = new ArrayList<ZipFileIndex>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            zipFileIndexes.addAll(zipFileIndexCache.values());
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            if (openedOnly) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                for(ZipFileIndex elem : zipFileIndexes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                    if (!elem.isOpen()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                        zipFileIndexes.remove(elem);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        return zipFileIndexes;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public boolean isOpen() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            return zipRandomFile != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    public static ZipFileIndex getZipFileIndex(File zipFile, int symbolFilePrefixLen, boolean useCache, String cacheLocation, boolean writeIndex) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        ZipFileIndex zi = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            zi = getExistingZipIndex(zipFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                zi = new ZipFileIndex(zipFile, symbolFilePrefixLen, writeIndex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                        useCache, cacheLocation);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                zipFileIndexCache.put(zipFile, zi);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        return zi;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    public static ZipFileIndex getExistingZipIndex(File zipFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            return zipFileIndexCache.get(zipFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    public static void clearCache() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            zipFileIndexCache.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    public static void clearCache(long timeNotUsed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            Iterator<File> cachedFileIterator = zipFileIndexCache.keySet().iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            while (cachedFileIterator.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                File cachedFile = cachedFileIterator.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                ZipFileIndex cachedZipIndex = zipFileIndexCache.get(cachedFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                if (cachedZipIndex != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                    long timeToTest = cachedZipIndex.lastReferenceTimeStamp + timeNotUsed;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                    if (timeToTest < cachedZipIndex.lastReferenceTimeStamp || // Overflow...
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                            System.currentTimeMillis() > timeToTest) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                        zipFileIndexCache.remove(cachedFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    public static void removeFromCache(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            zipFileIndexCache.remove(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    /** Sets already opened list of ZipFileIndexes from an outside client
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
      * of the compiler. This functionality should be used in a non-batch clients of the compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
      */
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    public static void setOpenedIndexes(List<ZipFileIndex>indexes) throws IllegalStateException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            if (zipFileIndexCache.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                throw new IllegalStateException("Setting opened indexes should be called only when the ZipFileCache is empty. Call JavacFileManager.flush() before calling this method.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            for (ZipFileIndex zfi : indexes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                zipFileIndexCache.put(zfi.zipFile, zfi);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    private ZipFileIndex(File zipFile, int symbolFilePrefixLen, boolean writeIndex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            boolean useCache, String cacheLocation) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        this.zipFile = zipFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        this.symbolFilePrefixLength = symbolFilePrefixLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        this.writeIndex = writeIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        this.usePreindexedCache = useCache;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        this.preindexedCacheLocation = cacheLocation;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        if (zipFile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            this.zipFileLastModified = zipFile.lastModified();
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        // Validate integrity of the zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        return "ZipFileIndex of file:(" + zipFile + ")";
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    // Just in case...
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    protected void finalize() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        closeFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    private boolean isUpToDate() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        if (zipFile != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                ((!NON_BATCH_MODE) || zipFileLastModified == zipFile.lastModified()) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                hasPopulatedData) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     * Here we need to make sure that the ZipFileIndex is valid. Check the timestamp of the file and
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     * if its the same as the one at the time the index was build we don't need to reopen anything.
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    private void checkIndex() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        boolean isUpToDate = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        if (!isUpToDate()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            closeFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            isUpToDate = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        if (zipRandomFile != null || isUpToDate) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            lastReferenceTimeStamp = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        hasPopulatedData = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        if (readIndex()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            lastReferenceTimeStamp = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        directories = Collections.<String, DirectoryEntry>emptyMap();
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        allDirs = Collections.<String>emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            openFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            long totalLength = zipRandomFile.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            ZipDirectory directory = new ZipDirectory(zipRandomFile, 0L, totalLength, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            directory.buildIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            if (zipRandomFile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                closeFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        lastReferenceTimeStamp = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    private void openFile() throws FileNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        if (zipRandomFile == null && zipFile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            zipRandomFile = new RandomAccessFile(zipFile, "r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    private void cleanupState() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        // Make sure there is a valid but empty index if the file doesn't exist
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        entries = ZipFileIndexEntry.EMPTY_ARRAY;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        directories = Collections.<String, DirectoryEntry>emptyMap();
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        zipFileLastModified = NOT_MODIFIED;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        allDirs = Collections.<String>emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            writeIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            closeFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    private void closeFile() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        if (zipRandomFile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                zipRandomFile.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            zipRandomFile = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
     * Returns the ZipFileIndexEntry for an absolute path, if there is one.
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    public ZipFileIndexEntry getZipIndexEntry(String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        if (File.separatorChar != '/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            path = path.replace('/', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            String lookFor = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            int lastSepIndex = path.lastIndexOf(File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            boolean noSeparator = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            if (lastSepIndex == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                noSeparator = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            DirectoryEntry de = directories.get(noSeparator ? "" : path.substring(0, lastSepIndex));
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            lookFor = path.substring(noSeparator ? 0 : lastSepIndex + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            return de == null ? null : de.getEntry(lookFor);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
     * Returns a javac List of filenames within an absolute path in the ZipFileIndex.
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    public com.sun.tools.javac.util.List<String> getFiles(String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        if (File.separatorChar != '/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            path = path.replace('/', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            DirectoryEntry de = directories.get(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            com.sun.tools.javac.util.List<String> ret = de == null ? null : de.getFiles();
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            if (ret == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                return com.sun.tools.javac.util.List.<String>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
            return ret;
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            return com.sun.tools.javac.util.List.<String>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    public List<String> getAllDirectories(String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        if (File.separatorChar != '/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            path = path.replace('/', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            path = path.intern();
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
            DirectoryEntry de = directories.get(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            com.sun.tools.javac.util.List<String> ret = de == null ? null : de.getDirectories();
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
            if (ret == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                return com.sun.tools.javac.util.List.<String>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
            return ret;
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            return com.sun.tools.javac.util.List.<String>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    public Set<String> getAllDirectories() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            if (allDirs == Collections.EMPTY_SET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                Set<String> alldirs = new HashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                Iterator<String> dirsIter = directories.keySet().iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                while (dirsIter.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                    alldirs.add(new String(dirsIter.next()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                allDirs = alldirs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
            return allDirs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            return Collections.<String>emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
     * Tests if a specific path exists in the zip.  This method will return true
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
     * for file entries and directories.
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
     * @param path A path within the zip.
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
     * @return True if the path is a file or dir, false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    public boolean contains(String path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            return getZipIndexEntry(path) != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
        catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    public boolean isDirectory(String path) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            // The top level in a zip file is always a directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            if (path.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                lastReferenceTimeStamp = System.currentTimeMillis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            if (File.separatorChar != '/')
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                path = path.replace('/', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            return directories.get(path) != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    public long getLastModified(String path) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
            ZipFileIndexEntry entry = getZipIndexEntry(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
            if (entry == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                throw new FileNotFoundException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
            return entry.getLastModified();
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    public int length(String path) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
            ZipFileIndexEntry entry = getZipIndexEntry(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            if (entry == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
                throw new FileNotFoundException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            if (entry.isDir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
            byte[] header = getHeader(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            // entry is not compressed?
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            if (get2ByteLittleEndian(header, 8) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                return entry.compressedSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                return entry.size;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
    public byte[] read(String path) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
            ZipFileIndexEntry entry = getZipIndexEntry(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            if (entry == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                throw new FileNotFoundException(MessageFormat.format("Path not found in ZIP: {0}", path));
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
            return read(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
    public byte[] read(ZipFileIndexEntry entry) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            openFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            byte[] result = readBytes(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
            closeFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
    public int read(String path, byte[] buffer) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            ZipFileIndexEntry entry = getZipIndexEntry(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
            if (entry == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                throw new FileNotFoundException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            return read(entry, buffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
    public int read(ZipFileIndexEntry entry, byte[] buffer)
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            int result = readBytes(entry, buffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
    private byte[] readBytes(ZipFileIndexEntry entry) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        byte[] header = getHeader(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
        int csize = entry.compressedSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
        byte[] cbuf = new byte[csize];
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        zipRandomFile.readFully(cbuf, 0, csize);
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        // is this compressed - offset 8 in the ZipEntry header
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        if (get2ByteLittleEndian(header, 8) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
            return cbuf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        int size = entry.size;
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
        byte[] buf = new byte[size];
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        if (inflate(cbuf, buf) != size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
            throw new ZipException("corrupted zip file");
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        return buf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
    private int readBytes(ZipFileIndexEntry entry, byte[] buffer) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        byte[] header = getHeader(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
        // entry is not compressed?
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        if (get2ByteLittleEndian(header, 8) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
            zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
            int offset = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
            int size = buffer.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            while (offset < size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                int count = zipRandomFile.read(buffer, offset, size - offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                if (count == -1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                offset += count;
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
            return entry.size;
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        int csize = entry.compressedSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        byte[] cbuf = new byte[csize];
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        zipRandomFile.readFully(cbuf, 0, csize);
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
        int count = inflate(cbuf, buffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        if (count == -1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            throw new ZipException("corrupted zip file");
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        return entry.size;
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
    //----------------------------------------------------------------------------
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    // Zip utilities
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
    //----------------------------------------------------------------------------
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
    private byte[] getHeader(ZipFileIndexEntry entry) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        zipRandomFile.seek(entry.offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
        byte[] header = new byte[30];
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
        zipRandomFile.readFully(header);
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        if (get4ByteLittleEndian(header, 0) != 0x04034b50)
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            throw new ZipException("corrupted zip file");
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
        if ((get2ByteLittleEndian(header, 6) & 1) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            throw new ZipException("encrypted zip file"); // offset 6 in the header of the ZipFileEntry
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        return header;
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
  /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
   * Inflate using the java.util.zip.Inflater class
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
   */
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
    private static Inflater inflater;
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
    private int inflate(byte[] src, byte[] dest) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        // construct the inflater object or reuse an existing one
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        if (inflater == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            inflater = new Inflater(true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
        synchronized (inflater) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            inflater.reset();
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            inflater.setInput(src);
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                return inflater.inflate(dest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            } catch (DataFormatException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
     * return the two bytes buf[pos], buf[pos+1] as an unsigned integer in little
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
     * endian format.
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
    private static int get2ByteLittleEndian(byte[] buf, int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
        return (buf[pos] & 0xFF) + ((buf[pos+1] & 0xFF) << 8);
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
     * return the 4 bytes buf[i..i+3] as an integer in little endian format.
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
    private static int get4ByteLittleEndian(byte[] buf, int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        return (buf[pos] & 0xFF) + ((buf[pos + 1] & 0xFF) << 8) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
                ((buf[pos + 2] & 0xFF) << 16) + ((buf[pos + 3] & 0xFF) << 24);
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
    /* ----------------------------------------------------------------------------
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
     * ZipDirectory
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
     * ----------------------------------------------------------------------------*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
    private class ZipDirectory {
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
        private String lastDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        private int lastStart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
        private int lastLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
        byte[] zipDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
        RandomAccessFile zipRandomFile = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        ZipFileIndex zipFileIndex = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
        public ZipDirectory(RandomAccessFile zipRandomFile, long start, long end, ZipFileIndex index) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
            this.zipRandomFile = zipRandomFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
            this.zipFileIndex = index;
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
            findCENRecord(start, end);
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
         * Reads zip file central directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
         * For more details see readCEN in zip_util.c from the JDK sources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
         * This is a Java port of that function.
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
        private void findCENRecord(long start, long end) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
            long totalLength = end - start;
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
            int endbuflen = 1024;
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
            byte[] endbuf = new byte[endbuflen];
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
            long endbufend = end - start;
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
            // There is a variable-length field after the dir offset record. We need to do consequential search.
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
            while (endbufend >= 22) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
                if (endbufend < endbuflen)
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
                    endbuflen = (int)endbufend;
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
                long endbufpos = endbufend - endbuflen;
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
                zipRandomFile.seek(start + endbufpos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
                zipRandomFile.readFully(endbuf, 0, endbuflen);
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
                int i = endbuflen - 22;
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
                while (i >= 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
                        !(endbuf[i] == 0x50 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
                        endbuf[i + 1] == 0x4b &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
                        endbuf[i + 2] == 0x05 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
                        endbuf[i + 3] == 0x06 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
                        endbufpos + i + 22 +
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
                        get2ByteLittleEndian(endbuf, i + 20) == totalLength)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
                    i--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
                if (i >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
                    zipDir = new byte[get4ByteLittleEndian(endbuf, i + 12) + 2];
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
                    zipDir[0] = endbuf[i + 10];
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
                    zipDir[1] = endbuf[i + 11];
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
                    zipRandomFile.seek(start + get4ByteLittleEndian(endbuf, i + 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
                    zipRandomFile.readFully(zipDir, 2, zipDir.length - 2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                    endbufend = endbufpos + 21;
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
            throw new ZipException("cannot read zip file");
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
        private void buildIndex() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            int entryCount = get2ByteLittleEndian(zipDir, 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
            entries = new ZipFileIndexEntry[entryCount];
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            // Add each of the files
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            if (entryCount > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
                directories = new HashMap<String, DirectoryEntry>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                ArrayList<ZipFileIndexEntry> entryList = new ArrayList<ZipFileIndexEntry>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                int pos = 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                for (int i = 0; i < entryCount; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                    pos = readEntry(pos, entryList, directories);
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                // Add the accumulated dirs into the same list
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                Iterator i = directories.keySet().iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
                while (i.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
                    ZipFileIndexEntry zipFileIndexEntry = new ZipFileIndexEntry( (String) i.next());
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
                    zipFileIndexEntry.isDir = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
                    entryList.add(zipFileIndexEntry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
                entries = entryList.toArray(new ZipFileIndexEntry[entryList.size()]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
                Arrays.sort(entries);
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
                cleanupState();
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        private int readEntry(int pos, List<ZipFileIndexEntry> entryList,
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
                Map<String, DirectoryEntry> directories) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
            if (get4ByteLittleEndian(zipDir, pos) != 0x02014b50) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
                throw new ZipException("cannot read zip file entry");
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
            int dirStart = pos + 46;
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
            int fileStart = dirStart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
            int fileEnd = fileStart + get2ByteLittleEndian(zipDir, pos + 28);
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
            if (zipFileIndex.symbolFilePrefixLength != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
                    ((fileEnd - fileStart) >= symbolFilePrefixLength)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
                dirStart += zipFileIndex.symbolFilePrefixLength;
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
               fileStart += zipFileIndex.symbolFilePrefixLength;
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
            // Use the OS's path separator. Keep the position of the last one.
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            for (int index = fileStart; index < fileEnd; index++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
                byte nextByte = zipDir[index];
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
                if (nextByte == (byte)'\\' || nextByte == (byte)'/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
                    zipDir[index] = (byte)File.separatorChar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
                    fileStart = index + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            String directory = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
            if (fileStart == dirStart)
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
                directory = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            else if (lastDir != null && lastLen == fileStart - dirStart - 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
                int index = lastLen - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
                while (zipDir[lastStart + index] == zipDir[dirStart + index]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
                    if (index == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
                        directory = lastDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
                    index--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
            // Sub directories
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
            if (directory == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
                lastStart = dirStart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
                lastLen = fileStart - dirStart - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
                directory = new String(zipDir, dirStart, lastLen, "UTF-8").intern();
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
                lastDir = directory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
                // Enter also all the parent directories
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
                String tempDirectory = directory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
                while (directories.get(tempDirectory) == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
                    directories.put(tempDirectory, new DirectoryEntry(tempDirectory, zipFileIndex));
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
                    int separator = tempDirectory.lastIndexOf(File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
                    if (separator == -1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
                    tempDirectory = tempDirectory.substring(0, separator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
                directory = directory.intern();
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                if (directories.get(directory) == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                    directories.put(directory, new DirectoryEntry(directory, zipFileIndex));
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
            // For each dir create also a file
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
            if (fileStart != fileEnd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
                ZipFileIndexEntry entry = new ZipFileIndexEntry(directory,
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                        new String(zipDir, fileStart, fileEnd - fileStart, "UTF-8"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                entry.setNativeTime(get4ByteLittleEndian(zipDir, pos + 12));
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                entry.compressedSize = get4ByteLittleEndian(zipDir, pos + 20);
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
                entry.size = get4ByteLittleEndian(zipDir, pos + 24);
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
                entry.offset = get4ByteLittleEndian(zipDir, pos + 42);
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
                entryList.add(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
            return pos + 46 +
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
                    get2ByteLittleEndian(zipDir, pos + 28) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
                    get2ByteLittleEndian(zipDir, pos + 30) +
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
                    get2ByteLittleEndian(zipDir, pos + 32);
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
     * Returns the last modified timestamp of a zip file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
     * @return long
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
    public long getZipFileLastModified() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
            checkIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
            return zipFileLastModified;
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
    /** ------------------------------------------------------------------------
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
     *  DirectoryEntry class
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
     * -------------------------------------------------------------------------*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
    static class DirectoryEntry {
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
        private boolean filesInited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
        private boolean directoriesInited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
        private boolean zipFileEntriesInited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
        private boolean entriesInited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
        private long writtenOffsetOffset = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
        private String dirName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
        private com.sun.tools.javac.util.List<String> zipFileEntriesFiles = com.sun.tools.javac.util.List.<String>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
        private com.sun.tools.javac.util.List<String> zipFileEntriesDirectories = com.sun.tools.javac.util.List.<String>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
        private com.sun.tools.javac.util.List<ZipFileIndexEntry>  zipFileEntries = com.sun.tools.javac.util.List.<ZipFileIndexEntry>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
        private List<ZipFileIndexEntry> entries = new ArrayList<ZipFileIndexEntry>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        private ZipFileIndex zipFileIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
        private int numEntries;
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
        DirectoryEntry(String dirName, ZipFileIndex index) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
        filesInited = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
            directoriesInited = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
            entriesInited = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
            if (File.separatorChar == '/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                dirName.replace('\\', '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                dirName.replace('/', '\\');
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            this.dirName = dirName.intern();
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
            this.zipFileIndex = index;
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
        private com.sun.tools.javac.util.List<String> getFiles() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
            if (filesInited) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
                return zipFileEntriesFiles;
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
            initEntries();
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
            for (ZipFileIndexEntry e : entries) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
                if (!e.isDir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
                    zipFileEntriesFiles = zipFileEntriesFiles.append(e.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
            filesInited = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
            return zipFileEntriesFiles;
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
        private com.sun.tools.javac.util.List<String> getDirectories() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
            if (directoriesInited) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
                return zipFileEntriesFiles;
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
            initEntries();
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
            for (ZipFileIndexEntry e : entries) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
                if (e.isDir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
                    zipFileEntriesDirectories = zipFileEntriesDirectories.append(e.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
            directoriesInited = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
            return zipFileEntriesDirectories;
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
        private com.sun.tools.javac.util.List<ZipFileIndexEntry> getEntries() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
            if (zipFileEntriesInited) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
                return zipFileEntries;
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
            initEntries();
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
            zipFileEntries = com.sun.tools.javac.util.List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
            for (ZipFileIndexEntry zfie : entries) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
                zipFileEntries = zipFileEntries.append(zfie);
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
            zipFileEntriesInited = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
            return zipFileEntries;
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
        private ZipFileIndexEntry getEntry(String rootName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
            initEntries();
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
            int index = Collections.binarySearch(entries, new ZipFileIndexEntry(dirName, rootName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
            if (index < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
            return entries.get(index);
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
        private void initEntries() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
            if (entriesInited) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
            if (!zipFileIndex.readFromIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
                int from = -Arrays.binarySearch(zipFileIndex.entries,
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                        new ZipFileIndexEntry(dirName, ZipFileIndex.MIN_CHAR)) - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                int to = -Arrays.binarySearch(zipFileIndex.entries,
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                        new ZipFileIndexEntry(dirName, MAX_CHAR)) - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
                boolean emptyList = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
                for (int i = from; i < to; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
                    entries.add(zipFileIndex.entries[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
                File indexFile = zipFileIndex.getIndexFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
                if (indexFile != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
                    RandomAccessFile raf = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
                        raf = new RandomAccessFile(indexFile, "r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
                        raf.seek(writtenOffsetOffset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
                        for (int nFiles = 0; nFiles < numEntries; nFiles++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                            // Read the name bytes
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                            int zfieNameBytesLen = raf.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                            byte [] zfieNameBytes = new byte[zfieNameBytesLen];
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
                            raf.read(zfieNameBytes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
                            String eName = new String(zfieNameBytes, "UTF-8");
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
                            // Read isDir
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
                            boolean eIsDir = raf.readByte() == (byte)0 ? false : true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                            // Read offset of bytes in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                            int eOffset = raf.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                            // Read size of the file in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                            int eSize = raf.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                            // Read compressed size of the file in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
                            int eCsize = raf.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
                            // Read java time stamp of the file in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
                            long eJavaTimestamp = raf.readLong();
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
                            ZipFileIndexEntry rfie = new ZipFileIndexEntry(dirName, eName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
                            rfie.isDir = eIsDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
                            rfie.offset = eOffset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
                            rfie.size = eSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
                            rfie.compressedSize = eCsize;
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
                            rfie.javatime = eJavaTimestamp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
                            entries.add(rfie);
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                    } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                        // Do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
                            if (raf == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                                raf.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
                        } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
                            // Do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
            entriesInited = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
        List<ZipFileIndexEntry> getEntriesAsCollection() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
            initEntries();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
            return entries;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
    private boolean readIndex() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
        if (triedToReadIndex || !usePreindexedCache) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
        boolean ret = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
            triedToReadIndex = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
            RandomAccessFile raf = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
                File indexFileName = getIndexFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
                raf = new RandomAccessFile(indexFileName, "r");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
                long fileStamp = raf.readLong();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
                if (zipFile.lastModified() != fileStamp) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
                    ret = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
                    directories = new HashMap<String, DirectoryEntry>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                    int numDirs = raf.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
                    for (int nDirs = 0; nDirs < numDirs; nDirs++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                        int dirNameBytesLen = raf.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                        byte [] dirNameBytes = new byte[dirNameBytesLen];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                        raf.read(dirNameBytes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
                        String dirNameStr = new String(dirNameBytes, "UTF-8");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                        DirectoryEntry de = new DirectoryEntry(dirNameStr, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
                        de.numEntries = raf.readInt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
                        de.writtenOffsetOffset = raf.readLong();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
                        directories.put(dirNameStr, de);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
                    ret = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
                    zipFileLastModified = fileStamp;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
                // Do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
                if (raf != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
                        raf.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                    } catch (Throwable tt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
                        // Do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            if (ret == true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
                readFromIndex = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
        return ret;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
    private boolean writeIndex() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
        boolean ret = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
        if (readFromIndex || !usePreindexedCache) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
        if (!writeIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
        File indexFile = getIndexFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
        if (indexFile == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
        RandomAccessFile raf = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
        long writtenSoFar = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
            raf = new RandomAccessFile(indexFile, "rw");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
            raf.writeLong(zipFileLastModified);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
            writtenSoFar += 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
            Iterator<String> iterDirName = directories.keySet().iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
            List<DirectoryEntry> directoriesToWrite = new ArrayList<DirectoryEntry>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
            Map<String, Long> offsets = new HashMap<String, Long>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
            raf.writeInt(directories.keySet().size());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
            writtenSoFar += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
            while(iterDirName.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
                String dirName = iterDirName.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
                DirectoryEntry dirEntry = directories.get(dirName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
                directoriesToWrite.add(dirEntry);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
                // Write the dir name bytes
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
                byte [] dirNameBytes = dirName.getBytes("UTF-8");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
                int dirNameBytesLen = dirNameBytes.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
                raf.writeInt(dirNameBytesLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
                writtenSoFar += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
                raf.write(dirNameBytes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                writtenSoFar += dirNameBytesLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
                // Write the number of files in the dir
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
                List dirEntries = dirEntry.getEntriesAsCollection();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
                raf.writeInt(dirEntries.size());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
                writtenSoFar += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
                offsets.put(dirName, new Long(writtenSoFar));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
                // Write the offset of the file's data in the dir
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                dirEntry.writtenOffsetOffset = 0L;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
                raf.writeLong(0L);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
                writtenSoFar += 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
            for (DirectoryEntry de : directoriesToWrite) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
                // Fix up the offset in the directory table
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
                long currFP = raf.getFilePointer();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
                long offsetOffset = offsets.get(de.dirName).longValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
                raf.seek(offsetOffset);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
                raf.writeLong(writtenSoFar);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
                raf.seek(currFP);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
                // Now write each of the files in the DirectoryEntry
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
                List<ZipFileIndexEntry> entries = de.getEntriesAsCollection();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
                for (ZipFileIndexEntry zfie : entries) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
                    // Write the name bytes
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
                    byte [] zfieNameBytes = zfie.name.getBytes("UTF-8");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
                    int zfieNameBytesLen = zfieNameBytes.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
                    raf.writeInt(zfieNameBytesLen);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
                    writtenSoFar += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
                    raf.write(zfieNameBytes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
                    writtenSoFar += zfieNameBytesLen;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
                    // Write isDir
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
                    raf.writeByte(zfie.isDir ? (byte)1 : (byte)0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
                    writtenSoFar += 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
                    // Write offset of bytes in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
                    raf.writeInt(zfie.offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
                    writtenSoFar += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
                    // Write size of the file in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
                    raf.writeInt(zfie.size);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
                    writtenSoFar += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
                    // Write compressed size of the file in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
                    raf.writeInt(zfie.compressedSize);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
                    writtenSoFar += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
                    // Write java time stamp of the file in the real Jar/Zip file
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
                    raf.writeLong(zfie.getLastModified());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
                    writtenSoFar += 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
        } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
            // Do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
                if (raf != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
                    raf.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
            } catch(IOException ioe) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
                // Do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
        return ret;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
    public boolean writeZipIndex() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
        lock.lock();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
            return writeIndex();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
            lock.unlock();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
    private File getIndexFile() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
        if (zipIndexFile == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
            if (zipFile == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
            zipIndexFile = new File((preindexedCacheLocation == null ? "" : preindexedCacheLocation) +
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
                    zipFile.getName() + ".index");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
        return zipIndexFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
    public File getZipFile() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
        return zipFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
}