1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.harmony.pack200;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.jar.JarFile;
24 import java.util.jar.JarInputStream;
25
26 import org.apache.commons.compress.java.util.jar.Pack200.Packer;
27 import org.apache.commons.compress.utils.ParsingUtils;
28
29
30
31
32
33 public class Pack200PackerAdapter extends Pack200Adapter implements Packer {
34
35 private final PackingOptions options = new PackingOptions();
36
37 @Override
38 protected void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) throws IOException {
39 super.firePropertyChange(propertyName, oldValue, newValue);
40 if (newValue != null && !newValue.equals(oldValue)) {
41 if (propertyName.startsWith(CLASS_ATTRIBUTE_PFX)) {
42 final String attributeName = propertyName.substring(CLASS_ATTRIBUTE_PFX.length());
43 options.addClassAttributeAction(attributeName, (String) newValue);
44 } else if (propertyName.startsWith(CODE_ATTRIBUTE_PFX)) {
45 final String attributeName = propertyName.substring(CODE_ATTRIBUTE_PFX.length());
46 options.addCodeAttributeAction(attributeName, (String) newValue);
47 } else if (propertyName.equals(DEFLATE_HINT)) {
48 options.setDeflateHint((String) newValue);
49 } else if (propertyName.equals(EFFORT)) {
50 options.setEffort(ParsingUtils.parseIntValue((String) newValue));
51 } else if (propertyName.startsWith(FIELD_ATTRIBUTE_PFX)) {
52 final String attributeName = propertyName.substring(FIELD_ATTRIBUTE_PFX.length());
53 options.addFieldAttributeAction(attributeName, (String) newValue);
54 } else if (propertyName.equals(KEEP_FILE_ORDER)) {
55 options.setKeepFileOrder(Boolean.parseBoolean((String) newValue));
56 } else if (propertyName.startsWith(METHOD_ATTRIBUTE_PFX)) {
57 final String attributeName = propertyName.substring(METHOD_ATTRIBUTE_PFX.length());
58 options.addMethodAttributeAction(attributeName, (String) newValue);
59 } else if (propertyName.equals(MODIFICATION_TIME)) {
60 options.setModificationTime((String) newValue);
61 } else if (propertyName.startsWith(PASS_FILE_PFX)) {
62 if (oldValue != null && !oldValue.equals("")) {
63 options.removePassFile((String) oldValue);
64 }
65 options.addPassFile((String) newValue);
66 } else if (propertyName.equals(SEGMENT_LIMIT)) {
67 options.setSegmentLimit(ParsingUtils.parseLongValue((String) newValue));
68 } else if (propertyName.equals(UNKNOWN_ATTRIBUTE)) {
69 options.setUnknownAttributeAction((String) newValue);
70 }
71 }
72 }
73
74 @Override
75 public void pack(final JarFile file, final OutputStream out) throws IOException {
76 if (file == null || out == null) {
77 throw new IllegalArgumentException("Must specify both input and output streams");
78 }
79 completed(0);
80 try {
81 new Archive(file, out, options).pack();
82 } catch (final Pack200Exception e) {
83 throw new IOException("Failed to pack Jar:" + e);
84 }
85 completed(1);
86 }
87
88 @Override
89 public void pack(final JarInputStream in, final OutputStream out) throws IOException {
90 if (in == null || out == null) {
91 throw new IllegalArgumentException("Must specify both input and output streams");
92 }
93 completed(0);
94 final PackingOptions options = new PackingOptions();
95
96 try {
97 new Archive(in, out, options).pack();
98 } catch (final Pack200Exception e) {
99 throw new IOException("Failed to pack Jar:" + e);
100 }
101 completed(1);
102 in.close();
103 }
104
105 }