author | ksrini |
Mon, 29 Nov 2010 13:38:12 -0800 | |
changeset 7192 | 445c518364c4 |
parent 7171 | ee97f78e7482 |
child 7795 | 98021fc612af |
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; |
2 | 29 |
import java.beans.PropertyChangeListener; |
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
30 |
import java.io.BufferedInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
31 |
import java.io.ByteArrayInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
32 |
import java.io.ByteArrayOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
33 |
import java.io.File; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
34 |
import java.io.FileInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
35 |
import java.io.IOException; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
36 |
import java.io.InputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
37 |
import java.io.OutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
38 |
import java.util.ArrayList; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
39 |
import java.util.Collections; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
40 |
import java.util.HashMap; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
41 |
import java.util.List; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
42 |
import java.util.ListIterator; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
43 |
import java.util.Map; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
44 |
import java.util.SortedMap; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
45 |
import java.util.TimeZone; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
46 |
import java.util.jar.JarEntry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
47 |
import java.util.jar.JarFile; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
48 |
import java.util.jar.JarInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
7171
diff
changeset
|
49 |
import java.util.jar.Pack200; |
2 | 50 |
|
51 |
||
52 |
/* |
|
53 |
* Implementation of the Pack provider. |
|
54 |
* </pre></blockquote> |
|
55 |
* @author John Rose |
|
56 |
* @author Kumar Srinivasan |
|
57 |
*/ |
|
58 |
||
59 |
||
6313 | 60 |
public class PackerImpl extends TLGlobals implements Pack200.Packer { |
2 | 61 |
|
62 |
/** |
|
63 |
* Constructs a Packer object and sets the initial state of |
|
64 |
* the packer engines. |
|
65 |
*/ |
|
6313 | 66 |
public PackerImpl() {} |
2 | 67 |
|
68 |
/** |
|
69 |
* Get the set of options for the pack and unpack engines. |
|
70 |
* @return A sorted association of option key strings to option values. |
|
71 |
*/ |
|
6313 | 72 |
public SortedMap<String, String> properties() { |
73 |
return props; |
|
2 | 74 |
} |
75 |
||
76 |
//Driver routines |
|
77 |
||
78 |
/** |
|
79 |
* Takes a JarFile and converts into a pack-stream. |
|
80 |
* <p> |
|
81 |
* Closes its input but not its output. (Pack200 archives are appendable.) |
|
82 |
* @param in a JarFile |
|
83 |
* @param out an OutputStream |
|
84 |
* @exception IOException if an error is encountered. |
|
85 |
*/ |
|
86 |
public void pack(JarFile in, OutputStream out) throws IOException { |
|
87 |
assert(Utils.currentInstance.get() == null); |
|
6313 | 88 |
TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE)) |
89 |
? null |
|
90 |
: TimeZone.getDefault(); |
|
2 | 91 |
try { |
92 |
Utils.currentInstance.set(this); |
|
93 |
if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC")); |
|
94 |
||
6313 | 95 |
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) { |
2 | 96 |
Utils.copyJarFile(in, out); |
97 |
} else { |
|
98 |
(new DoPack()).run(in, out); |
|
99 |
} |
|
100 |
} finally { |
|
101 |
Utils.currentInstance.set(null); |
|
102 |
if (tz != null) TimeZone.setDefault(tz); |
|
6313 | 103 |
in.close(); |
2 | 104 |
} |
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Takes a JarInputStream and converts into a pack-stream. |
|
109 |
* <p> |
|
110 |
* Closes its input but not its output. (Pack200 archives are appendable.) |
|
111 |
* <p> |
|
112 |
* The modification time and deflation hint attributes are not available, |
|
113 |
* for the jar-manifest file and the directory containing the file. |
|
114 |
* |
|
115 |
* @see #MODIFICATION_TIME |
|
116 |
* @see #DEFLATION_HINT |
|
117 |
* @param in a JarInputStream |
|
118 |
* @param out an OutputStream |
|
119 |
* @exception IOException if an error is encountered. |
|
120 |
*/ |
|
121 |
public void pack(JarInputStream in, OutputStream out) throws IOException { |
|
122 |
assert(Utils.currentInstance.get() == null); |
|
6313 | 123 |
TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE)) ? null : |
2 | 124 |
TimeZone.getDefault(); |
125 |
try { |
|
126 |
Utils.currentInstance.set(this); |
|
127 |
if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC")); |
|
6313 | 128 |
if ("0".equals(props.getProperty(Pack200.Packer.EFFORT))) { |
2 | 129 |
Utils.copyJarFile(in, out); |
130 |
} else { |
|
131 |
(new DoPack()).run(in, out); |
|
132 |
} |
|
133 |
} finally { |
|
134 |
Utils.currentInstance.set(null); |
|
135 |
if (tz != null) TimeZone.setDefault(tz); |
|
6313 | 136 |
in.close(); |
2 | 137 |
} |
138 |
} |
|
139 |
/** |
|
140 |
* Register a listener for changes to options. |
|
141 |
* @param listener An object to be invoked when a property is changed. |
|
142 |
*/ |
|
143 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
6313 | 144 |
props.addListener(listener); |
2 | 145 |
} |
146 |
||
147 |
/** |
|
148 |
* Remove a listener for the PropertyChange event. |
|
149 |
* @param listener The PropertyChange listener to be removed. |
|
150 |
*/ |
|
151 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
|
6313 | 152 |
props.removeListener(listener); |
2 | 153 |
} |
154 |
||
155 |
||
156 |
||
157 |
// All the worker bees..... |
|
158 |
||
159 |
// The packer worker. |
|
160 |
private class DoPack { |
|
6313 | 161 |
final int verbose = props.getInteger(Utils.DEBUG_VERBOSE); |
2 | 162 |
|
163 |
{ |
|
6313 | 164 |
props.setInteger(Pack200.Packer.PROGRESS, 0); |
165 |
if (verbose > 0) Utils.log.info(props.toString()); |
|
2 | 166 |
} |
167 |
||
168 |
// Here's where the bits are collected before getting packed: |
|
169 |
final Package pkg = new Package(); |
|
170 |
||
171 |
final String unknownAttrCommand; |
|
172 |
{ |
|
6313 | 173 |
String uaMode = props.getProperty(Pack200.Packer.UNKNOWN_ATTRIBUTE, Pack200.Packer.PASS); |
2 | 174 |
if (!(Pack200.Packer.STRIP.equals(uaMode) || |
175 |
Pack200.Packer.PASS.equals(uaMode) || |
|
176 |
Pack200.Packer.ERROR.equals(uaMode))) { |
|
177 |
throw new RuntimeException("Bad option: " + Pack200.Packer.UNKNOWN_ATTRIBUTE + " = " + uaMode); |
|
178 |
} |
|
179 |
unknownAttrCommand = uaMode.intern(); |
|
180 |
} |
|
181 |
||
182 |
final HashMap attrDefs; |
|
183 |
final HashMap attrCommands; |
|
184 |
{ |
|
185 |
HashMap attrDefs = new HashMap(); |
|
186 |
HashMap attrCommands = new HashMap(); |
|
187 |
String[] keys = { |
|
188 |
Pack200.Packer.CLASS_ATTRIBUTE_PFX, |
|
189 |
Pack200.Packer.FIELD_ATTRIBUTE_PFX, |
|
190 |
Pack200.Packer.METHOD_ATTRIBUTE_PFX, |
|
191 |
Pack200.Packer.CODE_ATTRIBUTE_PFX |
|
192 |
}; |
|
193 |
int[] ctypes = { |
|
194 |
Constants.ATTR_CONTEXT_CLASS, |
|
195 |
Constants.ATTR_CONTEXT_FIELD, |
|
196 |
Constants.ATTR_CONTEXT_METHOD, |
|
197 |
Constants.ATTR_CONTEXT_CODE |
|
198 |
}; |
|
199 |
for (int i = 0; i < ctypes.length; i++) { |
|
200 |
String pfx = keys[i]; |
|
6313 | 201 |
Map<String, String> map = props.prefixMap(pfx); |
202 |
for (String key : map.keySet()) { |
|
2 | 203 |
assert(key.startsWith(pfx)); |
204 |
String name = key.substring(pfx.length()); |
|
6313 | 205 |
String layout = props.getProperty(key); |
206 |
Layout lkey = Attribute.keyForLookup(ctypes[i], name); |
|
2 | 207 |
if (Pack200.Packer.STRIP.equals(layout) || |
208 |
Pack200.Packer.PASS.equals(layout) || |
|
209 |
Pack200.Packer.ERROR.equals(layout)) { |
|
210 |
attrCommands.put(lkey, layout.intern()); |
|
211 |
} else { |
|
212 |
Attribute.define(attrDefs, ctypes[i], name, layout); |
|
213 |
if (verbose > 1) { |
|
214 |
Utils.log.fine("Added layout for "+Constants.ATTR_CONTEXT_NAME[i]+" attribute "+name+" = "+layout); |
|
215 |
} |
|
216 |
assert(attrDefs.containsKey(lkey)); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
if (attrDefs.size() > 0) |
|
221 |
this.attrDefs = attrDefs; |
|
222 |
else |
|
223 |
this.attrDefs = null; |
|
224 |
if (attrCommands.size() > 0) |
|
225 |
this.attrCommands = attrCommands; |
|
226 |
else |
|
227 |
this.attrCommands = null; |
|
228 |
} |
|
229 |
||
230 |
final boolean keepFileOrder |
|
6313 | 231 |
= props.getBoolean(Pack200.Packer.KEEP_FILE_ORDER); |
2 | 232 |
final boolean keepClassOrder |
6313 | 233 |
= props.getBoolean(Utils.PACK_KEEP_CLASS_ORDER); |
2 | 234 |
|
235 |
final boolean keepModtime |
|
6313 | 236 |
= Pack200.Packer.KEEP.equals(props.getProperty(Pack200.Packer.MODIFICATION_TIME)); |
2 | 237 |
final boolean latestModtime |
6313 | 238 |
= Pack200.Packer.LATEST.equals(props.getProperty(Pack200.Packer.MODIFICATION_TIME)); |
2 | 239 |
final boolean keepDeflateHint |
6313 | 240 |
= Pack200.Packer.KEEP.equals(props.getProperty(Pack200.Packer.DEFLATE_HINT)); |
2 | 241 |
{ |
242 |
if (!keepModtime && !latestModtime) { |
|
6313 | 243 |
int modtime = props.getTime(Pack200.Packer.MODIFICATION_TIME); |
2 | 244 |
if (modtime != Constants.NO_MODTIME) { |
245 |
pkg.default_modtime = modtime; |
|
246 |
} |
|
247 |
} |
|
248 |
if (!keepDeflateHint) { |
|
6313 | 249 |
boolean deflate_hint = props.getBoolean(Pack200.Packer.DEFLATE_HINT); |
2 | 250 |
if (deflate_hint) { |
251 |
pkg.default_options |= Constants.AO_DEFLATE_HINT; |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
long totalOutputSize = 0; |
|
257 |
int segmentCount = 0; |
|
258 |
long segmentTotalSize = 0; |
|
259 |
long segmentSize = 0; // running counter |
|
260 |
final long segmentLimit; |
|
261 |
{ |
|
262 |
long limit; |
|
6313 | 263 |
if (props.getProperty(Pack200.Packer.SEGMENT_LIMIT, "").equals("")) |
2 | 264 |
limit = -1; |
265 |
else |
|
6313 | 266 |
limit = props.getLong(Pack200.Packer.SEGMENT_LIMIT); |
2 | 267 |
limit = Math.min(Integer.MAX_VALUE, limit); |
268 |
limit = Math.max(-1, limit); |
|
269 |
if (limit == -1) |
|
270 |
limit = Long.MAX_VALUE; |
|
271 |
segmentLimit = limit; |
|
272 |
} |
|
273 |
||
6313 | 274 |
final List<String> passFiles; // parsed pack.pass.file options |
2 | 275 |
{ |
276 |
// Which class files will be passed through? |
|
6313 | 277 |
passFiles = props.getProperties(Pack200.Packer.PASS_FILE_PFX); |
2 | 278 |
for (ListIterator i = passFiles.listIterator(); i.hasNext(); ) { |
279 |
String file = (String) i.next(); |
|
280 |
if (file == null) { i.remove(); continue; } |
|
281 |
file = Utils.getJarEntryName(file); // normalize '\\' to '/' |
|
282 |
if (file.endsWith("/")) |
|
283 |
file = file.substring(0, file.length()-1); |
|
284 |
i.set(file); |
|
285 |
} |
|
286 |
if (verbose > 0) Utils.log.info("passFiles = " + passFiles); |
|
287 |
} |
|
288 |
||
289 |
{ |
|
290 |
// Fill in permitted range of major/minor version numbers. |
|
291 |
int ver; |
|
6313 | 292 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"min.class.majver")) != 0) |
2 | 293 |
pkg.min_class_majver = (short) ver; |
6313 | 294 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"min.class.minver")) != 0) |
2 | 295 |
pkg.min_class_minver = (short) ver; |
6313 | 296 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"max.class.majver")) != 0) |
2 | 297 |
pkg.max_class_majver = (short) ver; |
6313 | 298 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"max.class.minver")) != 0) |
2 | 299 |
pkg.max_class_minver = (short) ver; |
6313 | 300 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"package.minver")) != 0) |
2 | 301 |
pkg.package_minver = (short) ver; |
6313 | 302 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"package.majver")) != 0) |
2 | 303 |
pkg.package_majver = (short) ver; |
304 |
} |
|
305 |
||
306 |
{ |
|
307 |
// Hook for testing: Forces use of special archive modes. |
|
6313 | 308 |
int opt = props.getInteger(Utils.COM_PREFIX+"archive.options"); |
2 | 309 |
if (opt != 0) |
310 |
pkg.default_options |= opt; |
|
311 |
} |
|
312 |
||
6313 | 313 |
// (Done collecting options from props.) |
2 | 314 |
|
315 |
boolean isClassFile(String name) { |
|
316 |
if (!name.endsWith(".class")) return false; |
|
317 |
for (String prefix = name; ; ) { |
|
318 |
if (passFiles.contains(prefix)) return false; |
|
319 |
int chop = prefix.lastIndexOf('/'); |
|
320 |
if (chop < 0) break; |
|
321 |
prefix = prefix.substring(0, chop); |
|
322 |
} |
|
323 |
return true; |
|
324 |
} |
|
325 |
||
326 |
boolean isMetaInfFile(String name) { |
|
327 |
return name.startsWith("/" + Utils.METAINF) || |
|
328 |
name.startsWith(Utils.METAINF); |
|
329 |
} |
|
330 |
||
331 |
// Get a new package, based on the old one. |
|
332 |
private void makeNextPackage() { |
|
333 |
pkg.reset(); |
|
334 |
} |
|
335 |
||
336 |
class InFile { |
|
337 |
final String name; |
|
338 |
final JarFile jf; |
|
339 |
final JarEntry je; |
|
340 |
final File f; |
|
341 |
int modtime = Constants.NO_MODTIME; |
|
342 |
int options; |
|
343 |
InFile(String name) { |
|
344 |
this.name = Utils.getJarEntryName(name); |
|
345 |
this.f = new File(name); |
|
346 |
this.jf = null; |
|
347 |
this.je = null; |
|
348 |
int timeSecs = getModtime(f.lastModified()); |
|
349 |
if (keepModtime && timeSecs != Constants.NO_MODTIME) { |
|
350 |
this.modtime = timeSecs; |
|
351 |
} else if (latestModtime && timeSecs > pkg.default_modtime) { |
|
352 |
pkg.default_modtime = timeSecs; |
|
353 |
} |
|
354 |
} |
|
355 |
InFile(JarFile jf, JarEntry je) { |
|
356 |
this.name = Utils.getJarEntryName(je.getName()); |
|
357 |
this.f = null; |
|
358 |
this.jf = jf; |
|
359 |
this.je = je; |
|
360 |
int timeSecs = getModtime(je.getTime()); |
|
361 |
if (keepModtime && timeSecs != Constants.NO_MODTIME) { |
|
362 |
this.modtime = timeSecs; |
|
363 |
} else if (latestModtime && timeSecs > pkg.default_modtime) { |
|
364 |
pkg.default_modtime = timeSecs; |
|
365 |
} |
|
366 |
if (keepDeflateHint && je.getMethod() == JarEntry.DEFLATED) { |
|
367 |
options |= Constants.FO_DEFLATE_HINT; |
|
368 |
} |
|
369 |
} |
|
370 |
InFile(JarEntry je) { |
|
371 |
this(null, je); |
|
372 |
} |
|
373 |
long getInputLength() { |
|
374 |
long len = (je != null)? je.getSize(): f.length(); |
|
375 |
assert(len >= 0) : this+".len="+len; |
|
376 |
// Bump size by pathname length and modtime/def-hint bytes. |
|
377 |
return Math.max(0, len) + name.length() + 5; |
|
378 |
} |
|
379 |
int getModtime(long timeMillis) { |
|
380 |
// Convert milliseconds to seconds. |
|
381 |
long seconds = (timeMillis+500) / 1000; |
|
382 |
if ((int)seconds == seconds) { |
|
383 |
return (int)seconds; |
|
384 |
} else { |
|
385 |
Utils.log.warning("overflow in modtime for "+f); |
|
386 |
return Constants.NO_MODTIME; |
|
387 |
} |
|
388 |
} |
|
389 |
void copyTo(Package.File file) { |
|
390 |
if (modtime != Constants.NO_MODTIME) |
|
391 |
file.modtime = modtime; |
|
392 |
file.options |= options; |
|
393 |
} |
|
394 |
InputStream getInputStream() throws IOException { |
|
395 |
if (jf != null) |
|
396 |
return jf.getInputStream(je); |
|
397 |
else |
|
398 |
return new FileInputStream(f); |
|
399 |
} |
|
400 |
||
401 |
public String toString() { |
|
402 |
return name; |
|
403 |
} |
|
404 |
} |
|
405 |
||
406 |
private int nread = 0; // used only if (verbose > 0) |
|
407 |
private void noteRead(InFile f) { |
|
408 |
nread++; |
|
409 |
if (verbose > 2) |
|
410 |
Utils.log.fine("...read "+f.name); |
|
411 |
if (verbose > 0 && (nread % 1000) == 0) |
|
412 |
Utils.log.info("Have read "+nread+" files..."); |
|
413 |
} |
|
414 |
||
415 |
void run(JarInputStream in, OutputStream out) throws IOException { |
|
416 |
// First thing we do is get the manifest, as JIS does |
|
417 |
// not provide the Manifest as an entry. |
|
418 |
if (in.getManifest() != null) { |
|
419 |
ByteArrayOutputStream tmp = new ByteArrayOutputStream(); |
|
420 |
in.getManifest().write(tmp); |
|
421 |
InputStream tmpIn = new ByteArrayInputStream(tmp.toByteArray()); |
|
422 |
pkg.addFile(readFile(JarFile.MANIFEST_NAME, tmpIn)); |
|
423 |
} |
|
424 |
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) { |
|
425 |
InFile inFile = new InFile(je); |
|
426 |
||
427 |
String name = inFile.name; |
|
428 |
Package.File bits = readFile(name, in); |
|
429 |
Package.File file = null; |
|
430 |
// (5078608) : discount the resource files in META-INF |
|
431 |
// from segment computation. |
|
6313 | 432 |
long inflen = (isMetaInfFile(name)) |
433 |
? 0L |
|
434 |
: inFile.getInputLength(); |
|
2 | 435 |
|
436 |
if ((segmentSize += inflen) > segmentLimit) { |
|
437 |
segmentSize -= inflen; |
|
438 |
int nextCount = -1; // don't know; it's a stream |
|
439 |
flushPartial(out, nextCount); |
|
440 |
} |
|
6313 | 441 |
if (verbose > 1) { |
2 | 442 |
Utils.log.fine("Reading " + name); |
6313 | 443 |
} |
2 | 444 |
|
445 |
assert(je.isDirectory() == name.endsWith("/")); |
|
446 |
||
447 |
if (isClassFile(name)) { |
|
448 |
file = readClass(name, bits.getInputStream()); |
|
449 |
} |
|
450 |
if (file == null) { |
|
451 |
file = bits; |
|
452 |
pkg.addFile(file); |
|
453 |
} |
|
454 |
inFile.copyTo(file); |
|
455 |
noteRead(inFile); |
|
456 |
} |
|
457 |
flushAll(out); |
|
458 |
} |
|
459 |
||
460 |
void run(JarFile in, OutputStream out) throws IOException { |
|
6313 | 461 |
List<InFile> inFiles = scanJar(in); |
2 | 462 |
|
463 |
if (verbose > 0) |
|
464 |
Utils.log.info("Reading " + inFiles.size() + " files..."); |
|
465 |
||
466 |
int numDone = 0; |
|
6313 | 467 |
for (InFile inFile : inFiles) { |
2 | 468 |
String name = inFile.name; |
469 |
// (5078608) : discount the resource files completely from segmenting |
|
6313 | 470 |
long inflen = (isMetaInfFile(name)) |
471 |
? 0L |
|
472 |
: inFile.getInputLength() ; |
|
2 | 473 |
if ((segmentSize += inflen) > segmentLimit) { |
474 |
segmentSize -= inflen; |
|
475 |
// Estimate number of remaining segments: |
|
476 |
float filesDone = numDone+1; |
|
477 |
float segsDone = segmentCount+1; |
|
478 |
float filesToDo = inFiles.size() - filesDone; |
|
479 |
float segsToDo = filesToDo * (segsDone/filesDone); |
|
480 |
if (verbose > 1) |
|
481 |
Utils.log.fine("Estimated segments to do: "+segsToDo); |
|
482 |
flushPartial(out, (int) Math.ceil(segsToDo)); |
|
483 |
} |
|
484 |
InputStream strm = inFile.getInputStream(); |
|
485 |
if (verbose > 1) |
|
486 |
Utils.log.fine("Reading " + name); |
|
487 |
Package.File file = null; |
|
488 |
if (isClassFile(name)) { |
|
489 |
file = readClass(name, strm); |
|
490 |
if (file == null) { |
|
491 |
strm.close(); |
|
492 |
strm = inFile.getInputStream(); |
|
493 |
} |
|
494 |
} |
|
495 |
if (file == null) { |
|
496 |
file = readFile(name, strm); |
|
497 |
pkg.addFile(file); |
|
498 |
} |
|
499 |
inFile.copyTo(file); |
|
500 |
strm.close(); // tidy up |
|
501 |
noteRead(inFile); |
|
502 |
numDone += 1; |
|
503 |
} |
|
504 |
flushAll(out); |
|
505 |
} |
|
506 |
||
507 |
Package.File readClass(String fname, InputStream in) throws IOException { |
|
508 |
Package.Class cls = pkg.new Class(fname); |
|
509 |
in = new BufferedInputStream(in); |
|
510 |
ClassReader reader = new ClassReader(cls, in); |
|
511 |
reader.setAttrDefs(attrDefs); |
|
512 |
reader.setAttrCommands(attrCommands); |
|
513 |
reader.unknownAttrCommand = unknownAttrCommand; |
|
514 |
try { |
|
515 |
reader.read(); |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
516 |
} catch (IOException ioe) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
517 |
String message = "Passing class file uncompressed due to"; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
518 |
if (ioe instanceof Attribute.FormatException) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
519 |
Attribute.FormatException ee = (Attribute.FormatException) ioe; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
520 |
// He passed up the category to us in layout. |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
521 |
if (ee.layout.equals(Pack200.Packer.PASS)) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
522 |
Utils.log.info(ee.toString()); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
523 |
Utils.log.warning(message + " unrecognized attribute: " + |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
524 |
fname); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
525 |
return null; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
526 |
} |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
527 |
} else if (ioe instanceof ClassReader.ClassFormatException) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
528 |
ClassReader.ClassFormatException ce = (ClassReader.ClassFormatException) ioe; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
529 |
// %% TODO: Do we invent a new property for this or reuse %% |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
530 |
if (unknownAttrCommand.equals(Pack200.Packer.PASS)) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
531 |
Utils.log.info(ce.toString()); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
532 |
Utils.log.warning(message + " unknown class format: " + |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
533 |
fname); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
534 |
return null; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
535 |
} |
2 | 536 |
} |
537 |
// Otherwise, it must be an error. |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
538 |
throw ioe; |
2 | 539 |
} |
540 |
pkg.addClass(cls); |
|
541 |
return cls.file; |
|
542 |
} |
|
543 |
||
544 |
// Read raw data. |
|
545 |
Package.File readFile(String fname, InputStream in) throws IOException { |
|
546 |
||
547 |
Package.File file = pkg.new File(fname); |
|
548 |
file.readFrom(in); |
|
549 |
if (file.isDirectory() && file.getFileLength() != 0) |
|
550 |
throw new IllegalArgumentException("Non-empty directory: "+file.getFileName()); |
|
551 |
return file; |
|
552 |
} |
|
553 |
||
554 |
void flushPartial(OutputStream out, int nextCount) throws IOException { |
|
6313 | 555 |
if (pkg.files.isEmpty() && pkg.classes.isEmpty()) { |
2 | 556 |
return; // do not flush an empty segment |
557 |
} |
|
558 |
flushPackage(out, Math.max(1, nextCount)); |
|
6313 | 559 |
props.setInteger(Pack200.Packer.PROGRESS, 25); |
2 | 560 |
// In case there will be another segment: |
561 |
makeNextPackage(); |
|
562 |
segmentCount += 1; |
|
563 |
segmentTotalSize += segmentSize; |
|
564 |
segmentSize = 0; |
|
565 |
} |
|
566 |
||
567 |
void flushAll(OutputStream out) throws IOException { |
|
6313 | 568 |
props.setInteger(Pack200.Packer.PROGRESS, 50); |
2 | 569 |
flushPackage(out, 0); |
570 |
out.flush(); |
|
6313 | 571 |
props.setInteger(Pack200.Packer.PROGRESS, 100); |
2 | 572 |
segmentCount += 1; |
573 |
segmentTotalSize += segmentSize; |
|
574 |
segmentSize = 0; |
|
575 |
if (verbose > 0 && segmentCount > 1) { |
|
576 |
Utils.log.info("Transmitted " |
|
577 |
+segmentTotalSize+" input bytes in " |
|
578 |
+segmentCount+" segments totaling " |
|
579 |
+totalOutputSize+" bytes"); |
|
580 |
} |
|
581 |
} |
|
582 |
||
583 |
||
584 |
/** Write all information in the current package segment |
|
585 |
* to the output stream. |
|
586 |
*/ |
|
587 |
void flushPackage(OutputStream out, int nextCount) throws IOException { |
|
588 |
int nfiles = pkg.files.size(); |
|
589 |
if (!keepFileOrder) { |
|
590 |
// Keeping the order of classes costs about 1% |
|
591 |
// Keeping the order of all files costs something more. |
|
592 |
if (verbose > 1) Utils.log.fine("Reordering files."); |
|
593 |
boolean stripDirectories = true; |
|
594 |
pkg.reorderFiles(keepClassOrder, stripDirectories); |
|
595 |
} else { |
|
596 |
// Package builder must have created a stub for each class. |
|
597 |
assert(pkg.files.containsAll(pkg.getClassStubs())); |
|
598 |
// Order of stubs in file list must agree with classes. |
|
599 |
List res = pkg.files; |
|
600 |
assert((res = new ArrayList(pkg.files)) |
|
601 |
.retainAll(pkg.getClassStubs()) || true); |
|
602 |
assert(res.equals(pkg.getClassStubs())); |
|
603 |
} |
|
604 |
pkg.trimStubs(); |
|
605 |
||
606 |
// Do some stripping, maybe. |
|
6313 | 607 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.debug")) pkg.stripAttributeKind("Debug"); |
608 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.compile")) pkg.stripAttributeKind("Compile"); |
|
609 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.constants")) pkg.stripAttributeKind("Constant"); |
|
610 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.exceptions")) pkg.stripAttributeKind("Exceptions"); |
|
611 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.innerclasses")) pkg.stripAttributeKind("InnerClasses"); |
|
2 | 612 |
|
613 |
// Must choose an archive version; PackageWriter does not. |
|
614 |
if (pkg.package_majver <= 0) pkg.choosePackageVersion(); |
|
615 |
||
616 |
PackageWriter pw = new PackageWriter(pkg, out); |
|
617 |
pw.archiveNextCount = nextCount; |
|
618 |
pw.write(); |
|
619 |
out.flush(); |
|
620 |
if (verbose > 0) { |
|
621 |
long outSize = pw.archiveSize0+pw.archiveSize1; |
|
622 |
totalOutputSize += outSize; |
|
623 |
long inSize = segmentSize; |
|
624 |
Utils.log.info("Transmitted " |
|
625 |
+nfiles+" files of " |
|
626 |
+inSize+" input bytes in a segment of " |
|
627 |
+outSize+" bytes"); |
|
628 |
} |
|
629 |
} |
|
630 |
||
6313 | 631 |
List<InFile> scanJar(JarFile jf) throws IOException { |
2 | 632 |
// Collect jar entries, preserving order. |
6313 | 633 |
List<InFile> inFiles = new ArrayList<>(); |
7171
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
634 |
try { |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
635 |
for (JarEntry je : Collections.list(jf.entries())) { |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
636 |
InFile inFile = new InFile(jf, je); |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
637 |
assert(je.isDirectory() == inFile.name.endsWith("/")); |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
638 |
inFiles.add(inFile); |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
639 |
} |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
640 |
} catch (IllegalStateException ise) { |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
641 |
throw new IOException(ise.getLocalizedMessage(), ise); |
2 | 642 |
} |
643 |
return inFiles; |
|
644 |
} |
|
645 |
} |
|
646 |
} |