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