aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core/src/org/jacoco/core
diff options
context:
space:
mode:
authorEvgeny Mandrikov <138671+Godin@users.noreply.github.com>2019-12-18 01:42:43 +0100
committerMarc R. Hoffmann <hoffmann@mountainminds.com>2019-12-18 01:42:43 +0100
commit60cf0dda12b30d4206f6d2787aebb35a70b26db9 (patch)
tree46df40cf3b3ef499c0431bd1905d9fc701f4a956 /org.jacoco.core/src/org/jacoco/core
parent7f5655075cadfd7c656c20d671ab9e183668280a (diff)
downloadjacoco-60cf0dda12b30d4206f6d2787aebb35a70b26db9.tar.gz
Absence of Pack200 in JDK 14 should not cause failure of JaCoCo build (#984)
Diffstat (limited to 'org.jacoco.core/src/org/jacoco/core')
-rw-r--r--org.jacoco.core/src/org/jacoco/core/internal/Pack200Streams.java43
1 files changed, 40 insertions, 3 deletions
diff --git a/org.jacoco.core/src/org/jacoco/core/internal/Pack200Streams.java b/org.jacoco.core/src/org/jacoco/core/internal/Pack200Streams.java
index 96b05960..57505706 100644
--- a/org.jacoco.core/src/org/jacoco/core/internal/Pack200Streams.java
+++ b/org.jacoco.core/src/org/jacoco/core/internal/Pack200Streams.java
@@ -18,9 +18,9 @@ import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.reflect.InvocationTargetException;
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.
@@ -36,11 +36,27 @@ public final class Pack200Streams {
* @throws IOException
* in case of errors with the streams
*/
+ @SuppressWarnings("resource")
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);
+ try {
+ final Object unpacker = Class.forName("java.util.jar.Pack200")
+ .getMethod("newUnpacker").invoke(null);
+ Class.forName("java.util.jar.Pack200$Unpacker")
+ .getMethod("unpack", InputStream.class,
+ JarOutputStream.class)
+ .invoke(unpacker, new NoCloseInput(input), jar);
+ } catch (ClassNotFoundException e) {
+ throw newIOException(e);
+ } catch (NoSuchMethodException e) {
+ throw newIOException(e);
+ } catch (IllegalAccessException e) {
+ throw newIOException(e);
+ } catch (InvocationTargetException e) {
+ throw newIOException(e.getCause());
+ }
jar.finish();
return new ByteArrayInputStream(buffer.toByteArray());
}
@@ -55,11 +71,32 @@ public final class Pack200Streams {
* @throws IOException
* in case of errors with the streams
*/
+ @SuppressWarnings("resource")
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);
+ try {
+ final Object packer = Class.forName("java.util.jar.Pack200")
+ .getMethod("newPacker").invoke(null);
+ Class.forName("java.util.jar.Pack200$Packer")
+ .getMethod("pack", JarInputStream.class, OutputStream.class)
+ .invoke(packer, jar, output);
+ } catch (ClassNotFoundException e) {
+ throw newIOException(e);
+ } catch (NoSuchMethodException e) {
+ throw newIOException(e);
+ } catch (IllegalAccessException e) {
+ throw newIOException(e);
+ } catch (InvocationTargetException e) {
+ throw newIOException(e.getCause());
+ }
+ }
+
+ private static IOException newIOException(final Throwable cause) {
+ final IOException exception = new IOException();
+ exception.initCause(cause);
+ return exception;
}
private static class NoCloseInput extends FilterInputStream {