author | ksrini |
Thu, 14 Oct 2010 09:36:05 -0700 | |
changeset 6891 | f8a528363fa5 |
parent 6313 | 470912c9e214 |
child 6900 | a3ca67586333 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
6313 | 2 |
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.java.util.jar.pack; |
|
27 |
||
6313 | 28 |
import com.sun.java.util.jar.pack.Attribute.Layout; |
29 |
import com.sun.java.util.jar.pack.ConstantPool.ClassEntry; |
|
30 |
import com.sun.java.util.jar.pack.ConstantPool.DescriptorEntry; |
|
31 |
import com.sun.java.util.jar.pack.ConstantPool.LiteralEntry; |
|
32 |
import com.sun.java.util.jar.pack.ConstantPool.MemberEntry; |
|
33 |
import com.sun.java.util.jar.pack.ConstantPool.SignatureEntry; |
|
34 |
import com.sun.java.util.jar.pack.ConstantPool.Utf8Entry; |
|
2 | 35 |
import java.util.*; |
36 |
import java.util.jar.*; |
|
37 |
import java.util.zip.*; |
|
38 |
import java.io.*; |
|
39 |
||
4919 | 40 |
import sun.util.logging.PlatformLogger; |
41 |
||
2 | 42 |
class Utils { |
43 |
static final String COM_PREFIX = "com.sun.java.util.jar.pack."; |
|
44 |
static final String METAINF = "META-INF"; |
|
45 |
||
46 |
/* |
|
47 |
* Outputs various diagnostic support information. |
|
48 |
* If >0, print summary comments (e.g., constant pool info). |
|
49 |
* If >1, print unit comments (e.g., processing of classes). |
|
50 |
* If >2, print many comments (e.g., processing of members). |
|
51 |
* If >3, print tons of comments (e.g., processing of references). |
|
52 |
* (installer only) |
|
53 |
*/ |
|
54 |
static final String DEBUG_VERBOSE = Utils.COM_PREFIX+"verbose"; |
|
55 |
||
56 |
/* |
|
57 |
* Disables use of native code, prefers the Java-coded implementation. |
|
58 |
* (installer only) |
|
59 |
*/ |
|
60 |
static final String DEBUG_DISABLE_NATIVE = COM_PREFIX+"disable.native"; |
|
61 |
||
62 |
/* |
|
63 |
* Use the default working TimeZone instead of UTC. |
|
64 |
* Note: This has installer unpacker implications. |
|
65 |
* see: zip.cpp which uses gmtime vs. localtime. |
|
66 |
*/ |
|
67 |
static final String PACK_DEFAULT_TIMEZONE = COM_PREFIX+"default.timezone"; |
|
68 |
||
69 |
/* |
|
70 |
* Property indicating that the unpacker should |
|
71 |
* ignore the transmitted PACK_MODIFICATION_TIME, |
|
72 |
* replacing it by the given value. The value can |
|
73 |
* be a numeric string, representing the number of |
|
74 |
* mSecs since the epoch (UTC), or the special string |
|
75 |
* {@link #NOW}, meaning the current time (UTC). |
|
76 |
* The default value is the special string {@link #KEEP}, |
|
77 |
* which asks the unpacker to preserve all transmitted |
|
78 |
* modification time information. |
|
79 |
* (installer only) |
|
80 |
*/ |
|
81 |
static final String UNPACK_MODIFICATION_TIME = COM_PREFIX+"unpack.modification.time"; |
|
82 |
||
83 |
/* |
|
84 |
* Property indicating that the unpacker strip the |
|
85 |
* Debug Attributes, if they are present, in the pack stream. |
|
86 |
* The default value is false. |
|
87 |
* (installer only) |
|
88 |
*/ |
|
89 |
static final String UNPACK_STRIP_DEBUG = COM_PREFIX+"unpack.strip.debug"; |
|
90 |
||
91 |
/* |
|
92 |
* Remove the input file after unpacking. |
|
93 |
* (installer only) |
|
94 |
*/ |
|
95 |
static final String UNPACK_REMOVE_PACKFILE = COM_PREFIX+"unpack.remove.packfile"; |
|
96 |
||
97 |
/* |
|
98 |
* A possible value for MODIFICATION_TIME |
|
99 |
*/ |
|
100 |
static final String NOW = "now"; |
|
101 |
// Other debug options: |
|
102 |
// com...debug.bands=false add band IDs to pack file, to verify sync |
|
103 |
// com...dump.bands=false dump band contents to local disk |
|
104 |
// com...no.vary.codings=false turn off coding variation heuristics |
|
105 |
// com...no.big.strings=false turn off "big string" feature |
|
106 |
||
107 |
/* |
|
108 |
* If this property is set to {@link #TRUE}, the packer will preserve |
|
109 |
* the ordering of class files of the original jar in the output archive. |
|
110 |
* The ordering is preserved only for class-files; resource files |
|
111 |
* may be reordered. |
|
112 |
* <p> |
|
113 |
* If the packer is allowed to reorder class files, it can marginally |
|
114 |
* decrease the transmitted size of the archive. |
|
115 |
*/ |
|
116 |
static final String PACK_KEEP_CLASS_ORDER = COM_PREFIX+"keep.class.order"; |
|
117 |
/* |
|
118 |
* This string PACK200 is given as a zip comment on all JAR files |
|
119 |
* produced by this utility. |
|
120 |
*/ |
|
121 |
static final String PACK_ZIP_ARCHIVE_MARKER_COMMENT = "PACK200"; |
|
122 |
||
6313 | 123 |
// Keep a TLS point to the global data and environment. |
124 |
// This makes it simpler to supply environmental options |
|
2 | 125 |
// to the engine code, especially the native code. |
6313 | 126 |
static final ThreadLocal<TLGlobals> currentInstance = new ThreadLocal<>(); |
127 |
||
128 |
// convenience methods to access the TL globals |
|
129 |
static TLGlobals getTLGlobals() { |
|
130 |
return currentInstance.get(); |
|
131 |
} |
|
132 |
||
133 |
static Map<String, Utf8Entry> getUtf8Entries() { |
|
134 |
return getTLGlobals().getUtf8Entries(); |
|
135 |
} |
|
136 |
||
137 |
static Map<String, ClassEntry> getClassEntries() { |
|
138 |
return getTLGlobals().getClassEntries(); |
|
139 |
} |
|
140 |
||
141 |
static Map<Object, LiteralEntry> getLiteralEntries() { |
|
142 |
return getTLGlobals().getLiteralEntries(); |
|
143 |
} |
|
144 |
||
145 |
static Map<String, DescriptorEntry> getDescriptorEntries() { |
|
146 |
return getTLGlobals().getDescriptorEntries(); |
|
147 |
} |
|
148 |
||
149 |
static Map<String, SignatureEntry> getSignatureEntries() { |
|
150 |
return getTLGlobals().getSignatureEntries(); |
|
151 |
} |
|
152 |
||
153 |
static Map<String, MemberEntry> getMemberEntries() { |
|
154 |
return getTLGlobals().getMemberEntries(); |
|
155 |
} |
|
2 | 156 |
|
157 |
static PropMap currentPropMap() { |
|
158 |
Object obj = currentInstance.get(); |
|
159 |
if (obj instanceof PackerImpl) |
|
6313 | 160 |
return ((PackerImpl)obj).props; |
2 | 161 |
if (obj instanceof UnpackerImpl) |
6313 | 162 |
return ((UnpackerImpl)obj).props; |
2 | 163 |
return null; |
164 |
} |
|
165 |
||
166 |
static final boolean nolog |
|
167 |
= Boolean.getBoolean(Utils.COM_PREFIX+"nolog"); |
|
168 |
||
169 |
||
4919 | 170 |
static class Pack200Logger { |
171 |
private final String name; |
|
172 |
private PlatformLogger log; |
|
173 |
Pack200Logger(String name) { |
|
174 |
this.name = name; |
|
175 |
} |
|
176 |
||
177 |
private synchronized PlatformLogger getLogger() { |
|
178 |
if (log == null) { |
|
179 |
log = PlatformLogger.getLogger(name); |
|
180 |
} |
|
181 |
return log; |
|
182 |
} |
|
183 |
||
184 |
public void warning(String msg, Object param) { |
|
185 |
int verbose = currentPropMap().getInteger(DEBUG_VERBOSE); |
|
186 |
if (verbose > 0) { |
|
187 |
getLogger().warning(msg, param); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
public void warning(String msg) { |
|
192 |
warning(msg, null); |
|
193 |
} |
|
194 |
||
195 |
public void info(String msg) { |
|
196 |
int verbose = currentPropMap().getInteger(DEBUG_VERBOSE); |
|
197 |
if (verbose > 0) { |
|
198 |
if (nolog) { |
|
199 |
System.out.println(msg); |
|
200 |
} else { |
|
201 |
getLogger().info(msg); |
|
2 | 202 |
} |
203 |
} |
|
4919 | 204 |
} |
2 | 205 |
|
4919 | 206 |
public void fine(String msg) { |
207 |
int verbose = currentPropMap().getInteger(DEBUG_VERBOSE); |
|
208 |
if (verbose > 0) { |
|
209 |
System.out.println(msg); |
|
2 | 210 |
} |
4919 | 211 |
} |
2 | 212 |
} |
213 |
||
4919 | 214 |
static final Pack200Logger log |
215 |
= new Pack200Logger("java.util.jar.Pack200"); |
|
216 |
||
2 | 217 |
// Returns the Max Version String of this implementation |
218 |
static String getVersionString() { |
|
6891
f8a528363fa5
6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents:
6313
diff
changeset
|
219 |
return "Pack200, Vendor: " + |
f8a528363fa5
6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents:
6313
diff
changeset
|
220 |
System.getProperty("java.vendor") + |
f8a528363fa5
6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents:
6313
diff
changeset
|
221 |
", Version: " + |
2 | 222 |
Constants.JAVA6_PACKAGE_MAJOR_VERSION + "." + |
223 |
Constants.JAVA6_PACKAGE_MINOR_VERSION; |
|
224 |
} |
|
225 |
||
226 |
static void markJarFile(JarOutputStream out) throws IOException { |
|
227 |
out.setComment(PACK_ZIP_ARCHIVE_MARKER_COMMENT); |
|
228 |
} |
|
229 |
||
230 |
// -0 mode helper |
|
231 |
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException { |
|
232 |
if (in.getManifest() != null) { |
|
233 |
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME); |
|
234 |
out.putNextEntry(me); |
|
235 |
in.getManifest().write(out); |
|
236 |
out.closeEntry(); |
|
237 |
} |
|
238 |
byte[] buffer = new byte[1 << 14]; |
|
239 |
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) { |
|
240 |
out.putNextEntry(je); |
|
241 |
for (int nr; 0 < (nr = in.read(buffer)); ) { |
|
242 |
out.write(buffer, 0, nr); |
|
243 |
} |
|
244 |
} |
|
245 |
in.close(); |
|
246 |
markJarFile(out); // add PACK200 comment |
|
247 |
} |
|
248 |
static void copyJarFile(JarFile in, JarOutputStream out) throws IOException { |
|
249 |
byte[] buffer = new byte[1 << 14]; |
|
250 |
for (Enumeration e = in.entries(); e.hasMoreElements(); ) { |
|
251 |
JarEntry je = (JarEntry) e.nextElement(); |
|
252 |
out.putNextEntry(je); |
|
253 |
InputStream ein = in.getInputStream(je); |
|
254 |
for (int nr; 0 < (nr = ein.read(buffer)); ) { |
|
255 |
out.write(buffer, 0, nr); |
|
256 |
} |
|
257 |
} |
|
258 |
in.close(); |
|
259 |
markJarFile(out); // add PACK200 comment |
|
260 |
} |
|
261 |
static void copyJarFile(JarInputStream in, OutputStream out) throws IOException { |
|
262 |
// 4947205 : Peformance is slow when using pack-effort=0 |
|
263 |
out = new BufferedOutputStream(out); |
|
264 |
out = new NonCloser(out); // protect from JarOutputStream.close() |
|
265 |
JarOutputStream jout = new JarOutputStream(out); |
|
266 |
copyJarFile(in, jout); |
|
267 |
jout.close(); |
|
268 |
} |
|
269 |
static void copyJarFile(JarFile in, OutputStream out) throws IOException { |
|
270 |
||
271 |
// 4947205 : Peformance is slow when using pack-effort=0 |
|
272 |
out = new BufferedOutputStream(out); |
|
273 |
out = new NonCloser(out); // protect from JarOutputStream.close() |
|
274 |
JarOutputStream jout = new JarOutputStream(out); |
|
275 |
copyJarFile(in, jout); |
|
276 |
jout.close(); |
|
277 |
} |
|
278 |
// Wrapper to prevent closing of client-supplied stream. |
|
279 |
static private |
|
280 |
class NonCloser extends FilterOutputStream { |
|
281 |
NonCloser(OutputStream out) { super(out); } |
|
282 |
public void close() throws IOException { flush(); } |
|
283 |
} |
|
284 |
static String getJarEntryName(String name) { |
|
285 |
if (name == null) return null; |
|
286 |
return name.replace(File.separatorChar, '/'); |
|
287 |
} |
|
288 |
||
289 |
static String zeString(ZipEntry ze) { |
|
290 |
int store = (ze.getCompressedSize() > 0) ? |
|
291 |
(int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 ) |
|
292 |
: 0 ; |
|
293 |
// Follow unzip -lv output |
|
294 |
return (long)ze.getSize() + "\t" + ze.getMethod() |
|
295 |
+ "\t" + ze.getCompressedSize() + "\t" |
|
296 |
+ store + "%\t" |
|
297 |
+ new Date(ze.getTime()) + "\t" |
|
298 |
+ Long.toHexString(ze.getCrc()) + "\t" |
|
299 |
+ ze.getName() ; |
|
300 |
} |
|
301 |
||
302 |
||
303 |
||
304 |
static byte[] readMagic(BufferedInputStream in) throws IOException { |
|
305 |
in.mark(4); |
|
306 |
byte[] magic = new byte[4]; |
|
307 |
for (int i = 0; i < magic.length; i++) { |
|
308 |
// read 1 byte at a time, so we always get 4 |
|
309 |
if (1 != in.read(magic, i, 1)) |
|
310 |
break; |
|
311 |
} |
|
312 |
in.reset(); |
|
313 |
return magic; |
|
314 |
} |
|
315 |
||
316 |
// magic number recognizers |
|
317 |
static boolean isJarMagic(byte[] magic) { |
|
318 |
return (magic[0] == (byte)'P' && |
|
319 |
magic[1] == (byte)'K' && |
|
320 |
magic[2] >= 1 && |
|
321 |
magic[2] < 8 && |
|
322 |
magic[3] == magic[2] + 1); |
|
323 |
} |
|
324 |
static boolean isPackMagic(byte[] magic) { |
|
325 |
return (magic[0] == (byte)0xCA && |
|
326 |
magic[1] == (byte)0xFE && |
|
327 |
magic[2] == (byte)0xD0 && |
|
328 |
magic[3] == (byte)0x0D); |
|
329 |
} |
|
330 |
static boolean isGZIPMagic(byte[] magic) { |
|
331 |
return (magic[0] == (byte)0x1F && |
|
332 |
magic[1] == (byte)0x8B && |
|
333 |
magic[2] == (byte)0x08); |
|
334 |
// fourth byte is variable "flg" field |
|
335 |
} |
|
336 |
||
337 |
private Utils() { } // do not instantiate |
|
338 |
} |