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