author | ksrini |
Fri, 08 Jul 2011 10:25:57 -0700 | |
changeset 10115 | eb08d08c7ef7 |
parent 7795 | 98021fc612af |
child 12857 | 0a5f341c2a28 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10115 | 2 |
* Copyright (c) 2003, 2011, 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 |
*/ |
|
10115 | 72 |
public SortedMap<String, String> properties() { |
6313 | 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 |
// The packer worker. |
|
159 |
private class DoPack { |
|
6313 | 160 |
final int verbose = props.getInteger(Utils.DEBUG_VERBOSE); |
2 | 161 |
|
162 |
{ |
|
6313 | 163 |
props.setInteger(Pack200.Packer.PROGRESS, 0); |
164 |
if (verbose > 0) Utils.log.info(props.toString()); |
|
2 | 165 |
} |
166 |
||
167 |
// Here's where the bits are collected before getting packed: |
|
168 |
final Package pkg = new Package(); |
|
169 |
||
170 |
final String unknownAttrCommand; |
|
171 |
{ |
|
6313 | 172 |
String uaMode = props.getProperty(Pack200.Packer.UNKNOWN_ATTRIBUTE, Pack200.Packer.PASS); |
2 | 173 |
if (!(Pack200.Packer.STRIP.equals(uaMode) || |
174 |
Pack200.Packer.PASS.equals(uaMode) || |
|
175 |
Pack200.Packer.ERROR.equals(uaMode))) { |
|
176 |
throw new RuntimeException("Bad option: " + Pack200.Packer.UNKNOWN_ATTRIBUTE + " = " + uaMode); |
|
177 |
} |
|
178 |
unknownAttrCommand = uaMode.intern(); |
|
179 |
} |
|
180 |
||
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
181 |
final Map<Attribute.Layout, Attribute> attrDefs; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
182 |
final Map<Attribute.Layout, String> attrCommands; |
2 | 183 |
{ |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
184 |
Map<Attribute.Layout, Attribute> lattrDefs = new HashMap<>(); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
185 |
Map<Attribute.Layout, String> lattrCommands = new HashMap<>(); |
2 | 186 |
String[] keys = { |
187 |
Pack200.Packer.CLASS_ATTRIBUTE_PFX, |
|
188 |
Pack200.Packer.FIELD_ATTRIBUTE_PFX, |
|
189 |
Pack200.Packer.METHOD_ATTRIBUTE_PFX, |
|
190 |
Pack200.Packer.CODE_ATTRIBUTE_PFX |
|
191 |
}; |
|
192 |
int[] ctypes = { |
|
193 |
Constants.ATTR_CONTEXT_CLASS, |
|
194 |
Constants.ATTR_CONTEXT_FIELD, |
|
195 |
Constants.ATTR_CONTEXT_METHOD, |
|
196 |
Constants.ATTR_CONTEXT_CODE |
|
197 |
}; |
|
198 |
for (int i = 0; i < ctypes.length; i++) { |
|
199 |
String pfx = keys[i]; |
|
10115 | 200 |
Map<String, String> map = props.prefixMap(pfx); |
201 |
for (String key : map.keySet()) { |
|
2 | 202 |
assert(key.startsWith(pfx)); |
203 |
String name = key.substring(pfx.length()); |
|
6313 | 204 |
String layout = props.getProperty(key); |
205 |
Layout lkey = Attribute.keyForLookup(ctypes[i], name); |
|
2 | 206 |
if (Pack200.Packer.STRIP.equals(layout) || |
207 |
Pack200.Packer.PASS.equals(layout) || |
|
208 |
Pack200.Packer.ERROR.equals(layout)) { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
209 |
lattrCommands.put(lkey, layout.intern()); |
2 | 210 |
} else { |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
211 |
Attribute.define(lattrDefs, ctypes[i], name, layout); |
2 | 212 |
if (verbose > 1) { |
213 |
Utils.log.fine("Added layout for "+Constants.ATTR_CONTEXT_NAME[i]+" attribute "+name+" = "+layout); |
|
214 |
} |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
215 |
assert(lattrDefs.containsKey(lkey)); |
2 | 216 |
} |
217 |
} |
|
218 |
} |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
219 |
this.attrDefs = (lattrDefs.isEmpty()) ? null : lattrDefs; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
220 |
this.attrCommands = (lattrCommands.isEmpty()) ? null : lattrCommands; |
2 | 221 |
} |
222 |
||
223 |
final boolean keepFileOrder |
|
6313 | 224 |
= props.getBoolean(Pack200.Packer.KEEP_FILE_ORDER); |
2 | 225 |
final boolean keepClassOrder |
6313 | 226 |
= props.getBoolean(Utils.PACK_KEEP_CLASS_ORDER); |
2 | 227 |
|
228 |
final boolean keepModtime |
|
6313 | 229 |
= Pack200.Packer.KEEP.equals(props.getProperty(Pack200.Packer.MODIFICATION_TIME)); |
2 | 230 |
final boolean latestModtime |
6313 | 231 |
= Pack200.Packer.LATEST.equals(props.getProperty(Pack200.Packer.MODIFICATION_TIME)); |
2 | 232 |
final boolean keepDeflateHint |
6313 | 233 |
= Pack200.Packer.KEEP.equals(props.getProperty(Pack200.Packer.DEFLATE_HINT)); |
2 | 234 |
{ |
235 |
if (!keepModtime && !latestModtime) { |
|
6313 | 236 |
int modtime = props.getTime(Pack200.Packer.MODIFICATION_TIME); |
2 | 237 |
if (modtime != Constants.NO_MODTIME) { |
238 |
pkg.default_modtime = modtime; |
|
239 |
} |
|
240 |
} |
|
241 |
if (!keepDeflateHint) { |
|
6313 | 242 |
boolean deflate_hint = props.getBoolean(Pack200.Packer.DEFLATE_HINT); |
2 | 243 |
if (deflate_hint) { |
244 |
pkg.default_options |= Constants.AO_DEFLATE_HINT; |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
long totalOutputSize = 0; |
|
250 |
int segmentCount = 0; |
|
251 |
long segmentTotalSize = 0; |
|
252 |
long segmentSize = 0; // running counter |
|
253 |
final long segmentLimit; |
|
254 |
{ |
|
255 |
long limit; |
|
6313 | 256 |
if (props.getProperty(Pack200.Packer.SEGMENT_LIMIT, "").equals("")) |
2 | 257 |
limit = -1; |
258 |
else |
|
6313 | 259 |
limit = props.getLong(Pack200.Packer.SEGMENT_LIMIT); |
2 | 260 |
limit = Math.min(Integer.MAX_VALUE, limit); |
261 |
limit = Math.max(-1, limit); |
|
262 |
if (limit == -1) |
|
263 |
limit = Long.MAX_VALUE; |
|
264 |
segmentLimit = limit; |
|
265 |
} |
|
266 |
||
6313 | 267 |
final List<String> passFiles; // parsed pack.pass.file options |
2 | 268 |
{ |
269 |
// Which class files will be passed through? |
|
6313 | 270 |
passFiles = props.getProperties(Pack200.Packer.PASS_FILE_PFX); |
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
271 |
for (ListIterator<String> i = passFiles.listIterator(); i.hasNext(); ) { |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
272 |
String file = i.next(); |
2 | 273 |
if (file == null) { i.remove(); continue; } |
274 |
file = Utils.getJarEntryName(file); // normalize '\\' to '/' |
|
275 |
if (file.endsWith("/")) |
|
276 |
file = file.substring(0, file.length()-1); |
|
277 |
i.set(file); |
|
278 |
} |
|
279 |
if (verbose > 0) Utils.log.info("passFiles = " + passFiles); |
|
280 |
} |
|
281 |
||
282 |
{ |
|
283 |
// Fill in permitted range of major/minor version numbers. |
|
284 |
int ver; |
|
6313 | 285 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"min.class.majver")) != 0) |
2 | 286 |
pkg.min_class_majver = (short) ver; |
6313 | 287 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"min.class.minver")) != 0) |
2 | 288 |
pkg.min_class_minver = (short) ver; |
6313 | 289 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"max.class.majver")) != 0) |
2 | 290 |
pkg.max_class_majver = (short) ver; |
6313 | 291 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"max.class.minver")) != 0) |
2 | 292 |
pkg.max_class_minver = (short) ver; |
6313 | 293 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"package.minver")) != 0) |
2 | 294 |
pkg.package_minver = (short) ver; |
6313 | 295 |
if ((ver = props.getInteger(Utils.COM_PREFIX+"package.majver")) != 0) |
2 | 296 |
pkg.package_majver = (short) ver; |
297 |
} |
|
298 |
||
299 |
{ |
|
300 |
// Hook for testing: Forces use of special archive modes. |
|
6313 | 301 |
int opt = props.getInteger(Utils.COM_PREFIX+"archive.options"); |
2 | 302 |
if (opt != 0) |
303 |
pkg.default_options |= opt; |
|
304 |
} |
|
305 |
||
6313 | 306 |
// (Done collecting options from props.) |
2 | 307 |
|
308 |
boolean isClassFile(String name) { |
|
309 |
if (!name.endsWith(".class")) return false; |
|
310 |
for (String prefix = name; ; ) { |
|
311 |
if (passFiles.contains(prefix)) return false; |
|
312 |
int chop = prefix.lastIndexOf('/'); |
|
313 |
if (chop < 0) break; |
|
314 |
prefix = prefix.substring(0, chop); |
|
315 |
} |
|
316 |
return true; |
|
317 |
} |
|
318 |
||
319 |
boolean isMetaInfFile(String name) { |
|
320 |
return name.startsWith("/" + Utils.METAINF) || |
|
321 |
name.startsWith(Utils.METAINF); |
|
322 |
} |
|
323 |
||
324 |
// Get a new package, based on the old one. |
|
325 |
private void makeNextPackage() { |
|
326 |
pkg.reset(); |
|
327 |
} |
|
328 |
||
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
329 |
final class InFile { |
2 | 330 |
final String name; |
331 |
final JarFile jf; |
|
332 |
final JarEntry je; |
|
333 |
final File f; |
|
334 |
int modtime = Constants.NO_MODTIME; |
|
335 |
int options; |
|
336 |
InFile(String name) { |
|
337 |
this.name = Utils.getJarEntryName(name); |
|
338 |
this.f = new File(name); |
|
339 |
this.jf = null; |
|
340 |
this.je = null; |
|
341 |
int timeSecs = getModtime(f.lastModified()); |
|
342 |
if (keepModtime && timeSecs != Constants.NO_MODTIME) { |
|
343 |
this.modtime = timeSecs; |
|
344 |
} else if (latestModtime && timeSecs > pkg.default_modtime) { |
|
345 |
pkg.default_modtime = timeSecs; |
|
346 |
} |
|
347 |
} |
|
348 |
InFile(JarFile jf, JarEntry je) { |
|
349 |
this.name = Utils.getJarEntryName(je.getName()); |
|
350 |
this.f = null; |
|
351 |
this.jf = jf; |
|
352 |
this.je = je; |
|
353 |
int timeSecs = getModtime(je.getTime()); |
|
354 |
if (keepModtime && timeSecs != Constants.NO_MODTIME) { |
|
355 |
this.modtime = timeSecs; |
|
356 |
} else if (latestModtime && timeSecs > pkg.default_modtime) { |
|
357 |
pkg.default_modtime = timeSecs; |
|
358 |
} |
|
359 |
if (keepDeflateHint && je.getMethod() == JarEntry.DEFLATED) { |
|
360 |
options |= Constants.FO_DEFLATE_HINT; |
|
361 |
} |
|
362 |
} |
|
363 |
InFile(JarEntry je) { |
|
364 |
this(null, je); |
|
365 |
} |
|
366 |
long getInputLength() { |
|
367 |
long len = (je != null)? je.getSize(): f.length(); |
|
368 |
assert(len >= 0) : this+".len="+len; |
|
369 |
// Bump size by pathname length and modtime/def-hint bytes. |
|
370 |
return Math.max(0, len) + name.length() + 5; |
|
371 |
} |
|
372 |
int getModtime(long timeMillis) { |
|
373 |
// Convert milliseconds to seconds. |
|
374 |
long seconds = (timeMillis+500) / 1000; |
|
375 |
if ((int)seconds == seconds) { |
|
376 |
return (int)seconds; |
|
377 |
} else { |
|
378 |
Utils.log.warning("overflow in modtime for "+f); |
|
379 |
return Constants.NO_MODTIME; |
|
380 |
} |
|
381 |
} |
|
382 |
void copyTo(Package.File file) { |
|
383 |
if (modtime != Constants.NO_MODTIME) |
|
384 |
file.modtime = modtime; |
|
385 |
file.options |= options; |
|
386 |
} |
|
387 |
InputStream getInputStream() throws IOException { |
|
388 |
if (jf != null) |
|
389 |
return jf.getInputStream(je); |
|
390 |
else |
|
391 |
return new FileInputStream(f); |
|
392 |
} |
|
393 |
||
394 |
public String toString() { |
|
395 |
return name; |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
private int nread = 0; // used only if (verbose > 0) |
|
400 |
private void noteRead(InFile f) { |
|
401 |
nread++; |
|
402 |
if (verbose > 2) |
|
403 |
Utils.log.fine("...read "+f.name); |
|
404 |
if (verbose > 0 && (nread % 1000) == 0) |
|
405 |
Utils.log.info("Have read "+nread+" files..."); |
|
406 |
} |
|
407 |
||
408 |
void run(JarInputStream in, OutputStream out) throws IOException { |
|
409 |
// First thing we do is get the manifest, as JIS does |
|
410 |
// not provide the Manifest as an entry. |
|
411 |
if (in.getManifest() != null) { |
|
412 |
ByteArrayOutputStream tmp = new ByteArrayOutputStream(); |
|
413 |
in.getManifest().write(tmp); |
|
414 |
InputStream tmpIn = new ByteArrayInputStream(tmp.toByteArray()); |
|
415 |
pkg.addFile(readFile(JarFile.MANIFEST_NAME, tmpIn)); |
|
416 |
} |
|
417 |
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) { |
|
418 |
InFile inFile = new InFile(je); |
|
419 |
||
420 |
String name = inFile.name; |
|
421 |
Package.File bits = readFile(name, in); |
|
422 |
Package.File file = null; |
|
423 |
// (5078608) : discount the resource files in META-INF |
|
424 |
// from segment computation. |
|
6313 | 425 |
long inflen = (isMetaInfFile(name)) |
426 |
? 0L |
|
427 |
: inFile.getInputLength(); |
|
2 | 428 |
|
429 |
if ((segmentSize += inflen) > segmentLimit) { |
|
430 |
segmentSize -= inflen; |
|
431 |
int nextCount = -1; // don't know; it's a stream |
|
432 |
flushPartial(out, nextCount); |
|
433 |
} |
|
6313 | 434 |
if (verbose > 1) { |
2 | 435 |
Utils.log.fine("Reading " + name); |
6313 | 436 |
} |
2 | 437 |
|
438 |
assert(je.isDirectory() == name.endsWith("/")); |
|
439 |
||
440 |
if (isClassFile(name)) { |
|
441 |
file = readClass(name, bits.getInputStream()); |
|
442 |
} |
|
443 |
if (file == null) { |
|
444 |
file = bits; |
|
445 |
pkg.addFile(file); |
|
446 |
} |
|
447 |
inFile.copyTo(file); |
|
448 |
noteRead(inFile); |
|
449 |
} |
|
450 |
flushAll(out); |
|
451 |
} |
|
452 |
||
453 |
void run(JarFile in, OutputStream out) throws IOException { |
|
6313 | 454 |
List<InFile> inFiles = scanJar(in); |
2 | 455 |
|
456 |
if (verbose > 0) |
|
457 |
Utils.log.info("Reading " + inFiles.size() + " files..."); |
|
458 |
||
459 |
int numDone = 0; |
|
6313 | 460 |
for (InFile inFile : inFiles) { |
2 | 461 |
String name = inFile.name; |
462 |
// (5078608) : discount the resource files completely from segmenting |
|
6313 | 463 |
long inflen = (isMetaInfFile(name)) |
464 |
? 0L |
|
465 |
: inFile.getInputLength() ; |
|
2 | 466 |
if ((segmentSize += inflen) > segmentLimit) { |
467 |
segmentSize -= inflen; |
|
468 |
// Estimate number of remaining segments: |
|
469 |
float filesDone = numDone+1; |
|
470 |
float segsDone = segmentCount+1; |
|
471 |
float filesToDo = inFiles.size() - filesDone; |
|
472 |
float segsToDo = filesToDo * (segsDone/filesDone); |
|
473 |
if (verbose > 1) |
|
474 |
Utils.log.fine("Estimated segments to do: "+segsToDo); |
|
475 |
flushPartial(out, (int) Math.ceil(segsToDo)); |
|
476 |
} |
|
477 |
InputStream strm = inFile.getInputStream(); |
|
478 |
if (verbose > 1) |
|
479 |
Utils.log.fine("Reading " + name); |
|
480 |
Package.File file = null; |
|
481 |
if (isClassFile(name)) { |
|
482 |
file = readClass(name, strm); |
|
483 |
if (file == null) { |
|
484 |
strm.close(); |
|
485 |
strm = inFile.getInputStream(); |
|
486 |
} |
|
487 |
} |
|
488 |
if (file == null) { |
|
489 |
file = readFile(name, strm); |
|
490 |
pkg.addFile(file); |
|
491 |
} |
|
492 |
inFile.copyTo(file); |
|
493 |
strm.close(); // tidy up |
|
494 |
noteRead(inFile); |
|
495 |
numDone += 1; |
|
496 |
} |
|
497 |
flushAll(out); |
|
498 |
} |
|
499 |
||
500 |
Package.File readClass(String fname, InputStream in) throws IOException { |
|
501 |
Package.Class cls = pkg.new Class(fname); |
|
502 |
in = new BufferedInputStream(in); |
|
503 |
ClassReader reader = new ClassReader(cls, in); |
|
504 |
reader.setAttrDefs(attrDefs); |
|
505 |
reader.setAttrCommands(attrCommands); |
|
506 |
reader.unknownAttrCommand = unknownAttrCommand; |
|
507 |
try { |
|
508 |
reader.read(); |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
509 |
} catch (IOException ioe) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
510 |
String message = "Passing class file uncompressed due to"; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
511 |
if (ioe instanceof Attribute.FormatException) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
512 |
Attribute.FormatException ee = (Attribute.FormatException) ioe; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
513 |
// He passed up the category to us in layout. |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
514 |
if (ee.layout.equals(Pack200.Packer.PASS)) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
515 |
Utils.log.info(ee.toString()); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
516 |
Utils.log.warning(message + " unrecognized attribute: " + |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
517 |
fname); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
518 |
return null; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
519 |
} |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
520 |
} else if (ioe instanceof ClassReader.ClassFormatException) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
521 |
ClassReader.ClassFormatException ce = (ClassReader.ClassFormatException) ioe; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
522 |
// %% 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
|
523 |
if (unknownAttrCommand.equals(Pack200.Packer.PASS)) { |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
524 |
Utils.log.info(ce.toString()); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
525 |
Utils.log.warning(message + " unknown class format: " + |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
526 |
fname); |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
527 |
return null; |
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
528 |
} |
2 | 529 |
} |
530 |
// Otherwise, it must be an error. |
|
6900
a3ca67586333
6982312: (pack200) pack200 fails with the jdk7 class files
ksrini
parents:
6313
diff
changeset
|
531 |
throw ioe; |
2 | 532 |
} |
533 |
pkg.addClass(cls); |
|
534 |
return cls.file; |
|
535 |
} |
|
536 |
||
537 |
// Read raw data. |
|
538 |
Package.File readFile(String fname, InputStream in) throws IOException { |
|
539 |
||
540 |
Package.File file = pkg.new File(fname); |
|
541 |
file.readFrom(in); |
|
542 |
if (file.isDirectory() && file.getFileLength() != 0) |
|
543 |
throw new IllegalArgumentException("Non-empty directory: "+file.getFileName()); |
|
544 |
return file; |
|
545 |
} |
|
546 |
||
547 |
void flushPartial(OutputStream out, int nextCount) throws IOException { |
|
6313 | 548 |
if (pkg.files.isEmpty() && pkg.classes.isEmpty()) { |
2 | 549 |
return; // do not flush an empty segment |
550 |
} |
|
551 |
flushPackage(out, Math.max(1, nextCount)); |
|
6313 | 552 |
props.setInteger(Pack200.Packer.PROGRESS, 25); |
2 | 553 |
// In case there will be another segment: |
554 |
makeNextPackage(); |
|
555 |
segmentCount += 1; |
|
556 |
segmentTotalSize += segmentSize; |
|
557 |
segmentSize = 0; |
|
558 |
} |
|
559 |
||
560 |
void flushAll(OutputStream out) throws IOException { |
|
6313 | 561 |
props.setInteger(Pack200.Packer.PROGRESS, 50); |
2 | 562 |
flushPackage(out, 0); |
563 |
out.flush(); |
|
6313 | 564 |
props.setInteger(Pack200.Packer.PROGRESS, 100); |
2 | 565 |
segmentCount += 1; |
566 |
segmentTotalSize += segmentSize; |
|
567 |
segmentSize = 0; |
|
568 |
if (verbose > 0 && segmentCount > 1) { |
|
569 |
Utils.log.info("Transmitted " |
|
570 |
+segmentTotalSize+" input bytes in " |
|
571 |
+segmentCount+" segments totaling " |
|
572 |
+totalOutputSize+" bytes"); |
|
573 |
} |
|
574 |
} |
|
575 |
||
576 |
||
577 |
/** Write all information in the current package segment |
|
578 |
* to the output stream. |
|
579 |
*/ |
|
580 |
void flushPackage(OutputStream out, int nextCount) throws IOException { |
|
581 |
int nfiles = pkg.files.size(); |
|
582 |
if (!keepFileOrder) { |
|
583 |
// Keeping the order of classes costs about 1% |
|
584 |
// Keeping the order of all files costs something more. |
|
585 |
if (verbose > 1) Utils.log.fine("Reordering files."); |
|
586 |
boolean stripDirectories = true; |
|
587 |
pkg.reorderFiles(keepClassOrder, stripDirectories); |
|
588 |
} else { |
|
589 |
// Package builder must have created a stub for each class. |
|
590 |
assert(pkg.files.containsAll(pkg.getClassStubs())); |
|
591 |
// Order of stubs in file list must agree with classes. |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
592 |
List<Package.File> res = pkg.files; |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
593 |
assert((res = new ArrayList<>(pkg.files)) |
2 | 594 |
.retainAll(pkg.getClassStubs()) || true); |
595 |
assert(res.equals(pkg.getClassStubs())); |
|
596 |
} |
|
597 |
pkg.trimStubs(); |
|
598 |
||
599 |
// Do some stripping, maybe. |
|
6313 | 600 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.debug")) pkg.stripAttributeKind("Debug"); |
601 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.compile")) pkg.stripAttributeKind("Compile"); |
|
602 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.constants")) pkg.stripAttributeKind("Constant"); |
|
603 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.exceptions")) pkg.stripAttributeKind("Exceptions"); |
|
604 |
if (props.getBoolean(Utils.COM_PREFIX+"strip.innerclasses")) pkg.stripAttributeKind("InnerClasses"); |
|
2 | 605 |
|
606 |
// Must choose an archive version; PackageWriter does not. |
|
607 |
if (pkg.package_majver <= 0) pkg.choosePackageVersion(); |
|
608 |
||
609 |
PackageWriter pw = new PackageWriter(pkg, out); |
|
610 |
pw.archiveNextCount = nextCount; |
|
611 |
pw.write(); |
|
612 |
out.flush(); |
|
613 |
if (verbose > 0) { |
|
614 |
long outSize = pw.archiveSize0+pw.archiveSize1; |
|
615 |
totalOutputSize += outSize; |
|
616 |
long inSize = segmentSize; |
|
617 |
Utils.log.info("Transmitted " |
|
618 |
+nfiles+" files of " |
|
619 |
+inSize+" input bytes in a segment of " |
|
620 |
+outSize+" bytes"); |
|
621 |
} |
|
622 |
} |
|
623 |
||
6313 | 624 |
List<InFile> scanJar(JarFile jf) throws IOException { |
2 | 625 |
// Collect jar entries, preserving order. |
6313 | 626 |
List<InFile> inFiles = new ArrayList<>(); |
7171
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
627 |
try { |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
628 |
for (JarEntry je : Collections.list(jf.entries())) { |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
629 |
InFile inFile = new InFile(jf, je); |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
630 |
assert(je.isDirectory() == inFile.name.endsWith("/")); |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
631 |
inFiles.add(inFile); |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
632 |
} |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
633 |
} catch (IllegalStateException ise) { |
ee97f78e7482
6985763: Pack200.Packer.pack(...) and Pack200.Unpacker.unpack(...) throw unspecified exceptions
ksrini
parents:
6900
diff
changeset
|
634 |
throw new IOException(ise.getLocalizedMessage(), ise); |
2 | 635 |
} |
636 |
return inFiles; |
|
637 |
} |
|
638 |
} |
|
639 |
} |