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.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.UncheckedIOException;
26 import java.util.Map;
27 import java.util.jar.JarOutputStream;
28
29 import org.apache.commons.compress.compressors.CompressorInputStream;
30 import org.apache.commons.compress.java.util.jar.Pack200;
31
32
33
34
35
36
37
38
39
40
41
42 public class Pack200CompressorInputStream extends CompressorInputStream {
43
44 private static final byte[] CAFE_DOOD = { (byte) 0xCA, (byte) 0xFE, (byte) 0xD0, (byte) 0x0D };
45 private static final int SIG_LENGTH = CAFE_DOOD.length;
46
47
48
49
50
51
52
53
54 public static boolean matches(final byte[] signature, final int length) {
55 if (length < SIG_LENGTH) {
56 return false;
57 }
58
59 for (int i = 0; i < SIG_LENGTH; i++) {
60 if (signature[i] != CAFE_DOOD[i]) {
61 return false;
62 }
63 }
64
65 return true;
66 }
67
68 private final InputStream originalInputStream;
69
70 private final AbstractStreamBridge abstractStreamBridge;
71
72
73
74
75
76
77
78 public Pack200CompressorInputStream(final File file) throws IOException {
79 this(file, Pack200Strategy.IN_MEMORY);
80 }
81
82
83
84
85
86
87
88
89 public Pack200CompressorInputStream(final File file, final Map<String, String> properties) throws IOException {
90 this(file, Pack200Strategy.IN_MEMORY, properties);
91 }
92
93
94
95
96
97
98
99
100 public Pack200CompressorInputStream(final File file, final Pack200Strategy mode) throws IOException {
101 this(null, file, mode, null);
102 }
103
104
105
106
107
108
109
110
111
112 public Pack200CompressorInputStream(final File file, final Pack200Strategy mode, final Map<String, String> properties) throws IOException {
113 this(null, file, mode, properties);
114 }
115
116
117
118
119
120
121
122
123
124
125
126 public Pack200CompressorInputStream(final InputStream inputStream) throws IOException {
127 this(inputStream, Pack200Strategy.IN_MEMORY);
128 }
129
130 private Pack200CompressorInputStream(final InputStream inputStream, final File file, final Pack200Strategy mode, final Map<String, String> properties)
131 throws IOException {
132 this.originalInputStream = inputStream;
133 this.abstractStreamBridge = mode.newStreamBridge();
134 try (JarOutputStream jarOut = new JarOutputStream(abstractStreamBridge)) {
135 final Pack200.Unpacker unpacker = Pack200.newUnpacker();
136 if (properties != null) {
137 unpacker.properties().putAll(properties);
138 }
139 if (file == null) {
140 unpacker.unpack(inputStream, jarOut);
141 } else {
142 unpacker.unpack(file, jarOut);
143 }
144 }
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 public Pack200CompressorInputStream(final InputStream inputStream, final Map<String, String> properties) throws IOException {
159 this(inputStream, Pack200Strategy.IN_MEMORY, properties);
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173 public Pack200CompressorInputStream(final InputStream inputStream, final Pack200Strategy mode) throws IOException {
174 this(inputStream, null, mode, null);
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189 public Pack200CompressorInputStream(final InputStream inputStream, final Pack200Strategy mode, final Map<String, String> properties) throws IOException {
190 this(inputStream, null, mode, properties);
191 }
192
193 @SuppressWarnings("resource")
194 @Override
195 public int available() throws IOException {
196 return getInputStream().available();
197 }
198
199 @Override
200 public void close() throws IOException {
201 try {
202 abstractStreamBridge.stop();
203 } finally {
204 if (originalInputStream != null) {
205 originalInputStream.close();
206 }
207 }
208 }
209
210 private InputStream getInputStream() throws IOException {
211 return abstractStreamBridge.getInputStream();
212 }
213
214 @SuppressWarnings("resource")
215 @Override
216 public synchronized void mark(final int limit) {
217 try {
218 getInputStream().mark(limit);
219 } catch (final IOException ex) {
220 throw new UncheckedIOException(ex);
221 }
222 }
223
224 @SuppressWarnings("resource")
225 @Override
226 public boolean markSupported() {
227 try {
228 return getInputStream().markSupported();
229 } catch (final IOException ex) {
230 return false;
231 }
232 }
233
234 @SuppressWarnings("resource")
235 @Override
236 public int read() throws IOException {
237 return getInputStream().read();
238 }
239
240 @SuppressWarnings("resource")
241 @Override
242 public int read(final byte[] b) throws IOException {
243 return getInputStream().read(b);
244 }
245
246 @SuppressWarnings("resource")
247 @Override
248 public int read(final byte[] b, final int off, final int count) throws IOException {
249 return getInputStream().read(b, off, count);
250 }
251
252 @SuppressWarnings("resource")
253 @Override
254 public synchronized void reset() throws IOException {
255 getInputStream().reset();
256 }
257
258 @SuppressWarnings("resource")
259 @Override
260 public long skip(final long count) throws IOException {
261 return org.apache.commons.io.IOUtils.skip(getInputStream(), count);
262 }
263 }