1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.compressors.pack200;
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Map;
25 import java.util.jar.JarInputStream;
26
27 import org.apache.commons.compress.compressors.CompressorOutputStream;
28 import org.apache.commons.compress.java.util.jar.Pack200;
29
30
31
32
33
34
35
36 public class Pack200CompressorOutputStream extends CompressorOutputStream<OutputStream> {
37 private boolean finished;
38 private final AbstractStreamBridge abstractStreamBridge;
39 private final Map<String, String> properties;
40
41
42
43
44
45
46
47 public Pack200CompressorOutputStream(final OutputStream out) throws IOException {
48 this(out, Pack200Strategy.IN_MEMORY);
49 }
50
51
52
53
54
55
56
57
58 public Pack200CompressorOutputStream(final OutputStream out, final Map<String, String> props) throws IOException {
59 this(out, Pack200Strategy.IN_MEMORY, props);
60 }
61
62
63
64
65
66
67
68
69 public Pack200CompressorOutputStream(final OutputStream out, final Pack200Strategy mode) throws IOException {
70 this(out, mode, null);
71 }
72
73
74
75
76
77
78
79
80
81 public Pack200CompressorOutputStream(final OutputStream out, final Pack200Strategy mode, final Map<String, String> props) throws IOException {
82 super(out);
83 abstractStreamBridge = mode.newStreamBridge();
84 properties = props;
85 }
86
87 @Override
88 public void close() throws IOException {
89 try {
90 finish();
91 } finally {
92 try {
93 abstractStreamBridge.stop();
94 } finally {
95 out.close();
96 }
97 }
98 }
99
100 public void finish() throws IOException {
101 if (!finished) {
102 finished = true;
103 final Pack200.Packer p = Pack200.newPacker();
104 if (properties != null) {
105 p.properties().putAll(properties);
106 }
107 try (JarInputStream ji = new JarInputStream(abstractStreamBridge.getInputStream())) {
108 p.pack(ji, out);
109 }
110 }
111 }
112
113 @Override
114 public void write(final byte[] b) throws IOException {
115 abstractStreamBridge.write(b);
116 }
117
118 @Override
119 public void write(final byte[] b, final int from, final int length) throws IOException {
120 abstractStreamBridge.write(b, from, length);
121 }
122
123 @Override
124 public void write(final int b) throws IOException {
125 abstractStreamBridge.write(b);
126 }
127 }