blob: d9dda7378b49d0cccad492cfb713d31ce09af6d3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/*******************************************************************************
* Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.internal;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
/**
* Internal wrapper for the weird Pack200 Java API to allow usage with streams.
*/
public final class Pack200Streams {
/**
* Unpack a stream in Pack200 format into a stream in JAR/ZIP format.
*
* @param input
* stream in Pack200 format
* @return stream in JAR/ZIP format
* @throws IOException
* in case of errors with the streams
*/
public static InputStream unpack(final InputStream input)
throws IOException {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final JarOutputStream jar = new JarOutputStream(buffer);
Pack200.newUnpacker().unpack(new NoCloseInput(input), jar);
jar.finish();
return new ByteArrayInputStream(buffer.toByteArray());
}
/**
* Packs a buffer in JAR/ZIP format into a stream in Pack200 format.
*
* @param source
* source in JAR/ZIP format
* @param output
* stream in Pack200 format
* @throws IOException
* in case of errors with the streams
*/
public static void pack(final byte[] source, final OutputStream output)
throws IOException {
final JarInputStream jar = new JarInputStream(
new ByteArrayInputStream(source));
Pack200.newPacker().pack(jar, output);
}
private static class NoCloseInput extends FilterInputStream {
protected NoCloseInput(final InputStream in) {
super(in);
}
@Override
public void close() throws IOException {
// do not close the underlying stream
}
}
private Pack200Streams() {
}
}
|