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

Mon, 07 Dec 2009 14:35:07 -0800

author
jjg
date
Mon, 07 Dec 2009 14:35:07 -0800
changeset 444
ea89c5d4af08
parent 424
86b773b7cb40
child 554
9d9f26857129
permissions
-rw-r--r--

6907660: stupid typo in ZipFileIndex guarantees NPE
Reviewed-by: darcy

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

mercurial