src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java

Mon, 10 Dec 2012 16:21:26 +0000

author
vromero
date
Mon, 10 Dec 2012 16:21:26 +0000
changeset 1442
fcf89720ae71
parent 1358
fc123bdeddb8
child 1467
189b26e3818f
permissions
-rw-r--r--

8003967: detect and remove all mutable implicit static enum fields in langtools
Reviewed-by: jjg

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

mercurial