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