You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2021/07/01 20:14:04 UTC
[commons-compress] 01/01: try to use java.util.jar.Packe200,
only fall back to Harmony
This is an automated email from the ASF dual-hosted git repository.
bodewig pushed a commit to branch pack200-harmony-as-fallback
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit d3ef88f6501b0c5703c85a7c2245f90773b3a277
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Thu Jul 1 22:12:51 2021 +0200
try to use java.util.jar.Packe200, only fall back to Harmony
---
.../compressors/pack200/HarmonyPack200.java | 133 ++++++++
.../compressors/pack200/JavaUtilPack200.java | 106 +++++++
.../pack200/Pack200CompressorInputStream.java | 3 +-
.../pack200/Pack200CompressorOutputStream.java | 3 +-
.../compressors/pack200/Pack200Facade.java | 295 ++++++++++++++++++
.../compress/compressors/pack200/Pack200Utils.java | 7 +-
.../compress/harmony/pack200/Pack200Adapter.java | 59 ----
.../harmony/pack200/Pack200PackerAdapter.java | 102 -------
.../harmony/unpack200/Pack200UnpackerAdapter.java | 74 -----
.../commons/compress/java/util/jar/Pack200.java | 339 ---------------------
.../compressors/pack200/Pack200FacadeTest.java | 50 +++
11 files changed, 589 insertions(+), 582 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/compressors/pack200/HarmonyPack200.java b/src/main/java/org/apache/commons/compress/compressors/pack200/HarmonyPack200.java
new file mode 100644
index 0000000..b52c4da
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/compressors/pack200/HarmonyPack200.java
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress.compressors.pack200;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+
+import org.apache.commons.compress.harmony.pack200.Pack200Exception;
+import org.apache.commons.compress.harmony.pack200.PackingOptions;
+import org.apache.commons.compress.harmony.unpack200.Archive;
+
+/**
+ * Class factory for {@link Pack200Facade.Packer} and {@link Pack200Facade.Unpacker} based on the {@code
+ * org.apache.commons.compress.harmony} package.
+ */
+public abstract class HarmonyPack200 {
+
+ /**
+ * Prevent this class from being instantiated.
+ */
+ private HarmonyPack200() {
+ // do nothing
+ }
+
+ /**
+ * Returns a new instance of a packer engine.
+ *
+ * @return an instance of {@code Pack200Facade.Packer}
+ */
+ public static Pack200Facade.Packer newPacker() {
+ return new Pack200PackerAdapter();
+ }
+
+ /**
+ * Returns a new instance of a unpacker engine.
+ *
+ * @return a instance of {@code Pack200Facade.Unpacker}.
+ */
+ public static Pack200Facade.Unpacker newUnpacker() {
+ return new Pack200UnpackerAdapter();
+ }
+
+ private static abstract class Pack200Adapter {
+
+ protected static final int DEFAULT_BUFFER_SIZE = 8192;
+
+ private final SortedMap<String, String> properties = new TreeMap<String, String>();
+
+ public SortedMap<String, String> properties() {
+ return properties;
+ }
+
+ }
+
+ static class Pack200UnpackerAdapter extends Pack200Adapter implements Pack200Facade.Unpacker {
+ @Override
+ public void unpack(InputStream in, JarOutputStream out) throws IOException {
+ if (in == null || out == null)
+ throw new IllegalArgumentException("Must specify both input and output streams");
+ try {
+ new Archive(in, out).unpack();
+ } catch (Pack200Exception e) {
+ throw new IOException("Failed to unpack Jar:" + String.valueOf(e));
+ }
+ in.close();
+ }
+
+ @Override
+ public void unpack(File file, JarOutputStream out) throws IOException {
+ if (file == null || out == null)
+ throw new IllegalArgumentException("Must specify both input and output streams");
+ int size = (int) file.length();
+ int bufferSize = (size > 0 && size < DEFAULT_BUFFER_SIZE ? size
+ : DEFAULT_BUFFER_SIZE);
+ InputStream in = new BufferedInputStream(new FileInputStream(file), bufferSize);
+ unpack(in, out);
+ }
+ }
+
+ static class Pack200PackerAdapter extends Pack200Adapter implements Pack200Facade.Packer {
+
+ private final PackingOptions options = new PackingOptions();
+
+ @Override
+ public void pack(JarFile file, OutputStream out) throws IOException {
+ if (file == null || out == null)
+ throw new IllegalArgumentException("Must specify both input and output streams");
+ try {
+ new org.apache.commons.compress.harmony.pack200.Archive(file, out, options).pack();
+ } catch (Pack200Exception e) {
+ throw new IOException("Failed to pack Jar:" + String.valueOf(e));
+ }
+ }
+
+ @Override
+ public void pack(JarInputStream in, OutputStream out) throws IOException {
+ if (in == null || out == null)
+ throw new IllegalArgumentException("Must specify both input and output streams");
+ PackingOptions options = new PackingOptions();
+
+ try {
+ new org.apache.commons.compress.harmony.pack200.Archive(in, out, options).pack();
+ } catch (Pack200Exception e) {
+ throw new IOException("Failed to pack Jar:" + String.valueOf(e));
+ }
+ in.close();
+ }
+
+ }
+}
diff --git a/src/main/java/org/apache/commons/compress/compressors/pack200/JavaUtilPack200.java b/src/main/java/org/apache/commons/compress/compressors/pack200/JavaUtilPack200.java
new file mode 100644
index 0000000..eb475ae
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/compressors/pack200/JavaUtilPack200.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.compress.compressors.pack200;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.SortedMap;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Pack200;
+
+/**
+ * Class factory for {@link Pack200Facade.Packer} and {@link Pack200Facade.Unpacker} based on the {@code
+ * org.apache.commons.compress.harmony} package.
+ */
+public abstract class JavaUtilPack200 {
+
+ /**
+ * Prevent this class from being instantiated.
+ */
+ private JavaUtilPack200() {
+ // do nothing
+ }
+
+ /**
+ * Returns a new instance of a packer engine.
+ *
+ * @return an instance of {@code Pack200Facade.Packer}
+ */
+ public static Pack200Facade.Packer newPacker() {
+ return new Pack200PackerAdapter(Pack200.newPacker());
+ }
+
+ /**
+ * Returns a new instance of a unpacker engine.
+ *
+ * @return a instance of {@code Pack200Facade.Unpacker}.
+ */
+ public static Pack200Facade.Unpacker newUnpacker() {
+ return new Pack200UnpackerAdapter(Pack200.newUnpacker());
+ }
+
+ private static class Pack200UnpackerAdapter implements Pack200Facade.Unpacker {
+ private final Pack200.Unpacker unpacker;
+
+ private Pack200UnpackerAdapter(final Pack200.Unpacker unpacker) {
+ this.unpacker = unpacker;
+ }
+
+ @Override
+ public void unpack(InputStream in, JarOutputStream out) throws IOException {
+ unpacker.unpack(in, out);
+ }
+
+ @Override
+ public void unpack(File file, JarOutputStream out) throws IOException {
+ unpacker.unpack(file, out);
+ }
+
+ @Override
+ public SortedMap<String, String> properties() {
+ return unpacker.properties();
+ }
+ }
+
+ private static class Pack200PackerAdapter implements Pack200Facade.Packer {
+ private final Pack200.Packer packer;
+
+ private Pack200PackerAdapter(final Pack200.Packer packer) {
+ this.packer = packer;
+ }
+
+ @Override
+ public void pack(JarFile file, OutputStream out) throws IOException {
+ packer.pack(file, out);
+ }
+
+ @Override
+ public void pack(JarInputStream in, OutputStream out) throws IOException {
+ packer.pack(in, out);
+ }
+
+ @Override
+ public SortedMap<String, String> properties() {
+ return packer.properties();
+ }
+ }
+}
diff --git a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorInputStream.java
index 0336fed..2d4d0d7 100644
--- a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorInputStream.java
@@ -24,7 +24,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.jar.JarOutputStream;
-import org.apache.commons.compress.java.util.jar.Pack200;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.utils.CloseShieldFilterInputStream;
@@ -173,7 +172,7 @@ public class Pack200CompressorInputStream extends CompressorInputStream {
originalInput = in;
streamBridge = mode.newStreamBridge();
try (final JarOutputStream jarOut = new JarOutputStream(streamBridge)) {
- final Pack200.Unpacker u = Pack200.newUnpacker();
+ final Pack200Facade.Unpacker u = Pack200Facade.newUnpacker();
if (props != null) {
u.properties().putAll(props);
}
diff --git a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorOutputStream.java
index fea31f4..cf1473e 100644
--- a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200CompressorOutputStream.java
@@ -23,7 +23,6 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.jar.JarInputStream;
-import org.apache.commons.compress.java.util.jar.Pack200;
import org.apache.commons.compress.compressors.CompressorOutputStream;
@@ -128,7 +127,7 @@ public class Pack200CompressorOutputStream extends CompressorOutputStream {
public void finish() throws IOException {
if (!finished) {
finished = true;
- final Pack200.Packer p = Pack200.newPacker();
+ final Pack200Facade.Packer p = Pack200Facade.newPacker();
if (properties != null) {
p.properties().putAll(properties);
}
diff --git a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Facade.java b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Facade.java
new file mode 100644
index 0000000..49dd738
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Facade.java
@@ -0,0 +1,295 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.commons.compress.compressors.pack200;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.SortedMap;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+
+/**
+ * Class factory for {@link Pack200Facade.Packer} and {@link Pack200Facade.Unpacker} that tries to use {@link
+ * JavaUtilPack200} and falls back to {@link HarmonyPack200}.
+ */
+public abstract class Pack200Facade {
+
+ private static final Method newPackerMethod;
+ private static final Method newUnpackerMethod;
+
+ static {
+ Method newPacker, newUnpacker;
+ newPacker = newUnpacker = null;
+ try {
+ Class p = Class.forName("org.apache.commons.compress.compressors.pack200.JavaUtilPack200");
+ newPacker = p.getMethod("newPacker");
+ newUnpacker = p.getMethod("newUnpacker");
+ } catch (Exception ex) {
+ newPacker = newUnpacker = null;
+ }
+ newPackerMethod = newPacker;
+ newUnpackerMethod = newUnpacker;
+ }
+
+ /**
+ * Prevent this class from being instantiated.
+ */
+ private Pack200Facade() {
+ // do nothing
+ }
+
+ /**
+ * Returns a new instance of a packer engine.
+ *
+ * @return an instance of {@code Packer}
+ */
+ public static Packer newPacker() {
+ if (newPackerMethod != null) {
+ try {
+ return (Packer) newPackerMethod.invoke(null);
+ } catch (IllegalAccessException | InvocationTargetException ex) {
+ }
+ }
+ return HarmonyPack200.newPacker();
+ }
+
+ /**
+ * Returns a new instance of a unpacker engine.
+ *
+ * @return a instance of {@code Unpacker}.
+ */
+ public static Unpacker newUnpacker() {
+ if (newUnpackerMethod != null) {
+ try {
+ return (Unpacker) newUnpackerMethod.invoke(null);
+ } catch (IllegalAccessException | InvocationTargetException ex) {
+ }
+ }
+ return HarmonyPack200.newUnpacker();
+ }
+
+ /**
+ * The interface defining the API for converting a JAR file to an output
+ * stream in the Pack200 format.
+ */
+ public static interface Packer {
+
+ /**
+ * the format of a class attribute name.
+ */
+ static final String CLASS_ATTRIBUTE_PFX = "pack.class.attribute.";
+
+ /**
+ * the format of a code attribute name.
+ */
+ static final String CODE_ATTRIBUTE_PFX = "pack.code.attribute.";
+
+ /**
+ * the deflation hint to set in the output archive.
+ */
+ static final String DEFLATE_HINT = "pack.deflate.hint";
+
+ /**
+ * the indicated amount of effort to use in compressing the archive.
+ */
+ static final String EFFORT = "pack.effort";
+
+ /**
+ * a String representation for {@code error}.
+ */
+ static final String ERROR = "error";
+
+ /**
+ * a String representation of {@code false}.
+ */
+ static final String FALSE = "false";
+
+ /**
+ * the format of a field attribute name.
+ */
+ static final String FIELD_ATTRIBUTE_PFX = "pack.field.attribute.";
+
+ /**
+ * a String representation for {@code keep}.
+ */
+ static final String KEEP = "keep";
+
+ /**
+ * decide if all elements shall transmit in their original order.
+ */
+ static final String KEEP_FILE_ORDER = "pack.keep.file.order";
+
+ /**
+ * a String representation for {@code latest}.
+ */
+ static final String LATEST = "latest";
+
+ /**
+ * the format of a method attribute name.
+ */
+ static final String METHOD_ATTRIBUTE_PFX = "pack.method.attribute.";
+
+ /**
+ * if it shall attempt to determine the latest modification time if this
+ * is set to {@code LATEST}.
+ */
+ static final String MODIFICATION_TIME = "pack.modification.time";
+
+ /**
+ * a String representation of {@code pass}.
+ */
+ static final String PASS = "pass";
+
+ /**
+ * the file that will not be compressed.
+ */
+ static final String PASS_FILE_PFX = "pack.pass.file.";
+
+ /**
+ * packer progress as a percentage.
+ */
+ static final String PROGRESS = "pack.progress";
+
+ /**
+ * The number of bytes of each archive segment.
+ */
+ static final String SEGMENT_LIMIT = "pack.segment.limit";
+
+ /**
+ * a String representation of {@code strip}.
+ */
+ static final String STRIP = "strip";
+
+ /**
+ * a String representation of {@code true}.
+ */
+ static final String TRUE = "true";
+
+ /**
+ * the action to take if an unknown attribute is encountered.
+ */
+ static final String UNKNOWN_ATTRIBUTE = "pack.unknown.attribute";
+
+ /**
+ * Returns a sorted map of the properties of this packer.
+ *
+ * @return the properties of the packer.
+ */
+ SortedMap<String, String> properties();
+
+ /**
+ * Pack the specified JAR file to the specified output stream.
+ *
+ * @param in
+ * JAR file to be compressed.
+ * @param out
+ * stream of compressed data.
+ * @throws IOException
+ * if I/O exception occurs.
+ */
+ void pack(JarFile in, OutputStream out) throws IOException;
+
+ /**
+ * Pack the data from the specified jar input stream to the specified
+ * output stream.
+ *
+ * @param in
+ * stream of uncompressed JAR data.
+ * @param out
+ * stream of compressed data.
+ * @throws IOException
+ * if I/O exception occurs.
+ */
+ void pack(JarInputStream in, OutputStream out) throws IOException;
+
+ }
+
+ /**
+ * The interface defining the API for converting a packed stream in the
+ * Pack200 format to a JAR file.
+ */
+ public static interface Unpacker {
+
+ /**
+ * The String indicating if the unpacker should ignore all transmitted
+ * values,can be replaced by either {@code true} or {@code false}.
+ */
+ static final String DEFLATE_HINT = "unpack.deflate.hint";
+
+ /**
+ * a String representation of {@code false}.
+ */
+ static final String FALSE = "false";
+
+ /**
+ * a String representation of {@code keep}.
+ */
+ static final String KEEP = "keep";
+
+ /**
+ * the progress as a {@code percentage}.
+ */
+ static final String PROGRESS = "unpack.progress";
+
+ /**
+ * a String representation of {@code true}.
+ */
+ static final String TRUE = "true";
+
+ /**
+ * Returns a sorted map of the properties of this unpacker.
+ *
+ * @return the properties of unpacker.
+ */
+ SortedMap<String, String> properties();
+
+ /**
+ * Unpack the specified stream to the specified JAR output stream.
+ *
+ * @param in
+ * stream to uncompressed.
+ * @param out
+ * JAR output stream of uncompressed data.
+ * @throws IOException
+ * if I/O exception occurs.
+ */
+ void unpack(InputStream in, JarOutputStream out) throws IOException;
+
+ /**
+ * Unpack the contents of the specified {@code File} to the specified
+ * JAR output stream.
+ *
+ * @param in
+ * file to be uncompressed.
+ * @param out
+ * JAR output stream of uncompressed data.
+ * @throws IOException
+ * if I/O exception occurs.
+ */
+ void unpack(File in, JarOutputStream out) throws IOException;
+
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
index c57bb1f..7f087e2 100644
--- a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
+++ b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
@@ -26,7 +26,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
-import org.apache.commons.compress.java.util.jar.Pack200;
/**
* Utility methods for Pack200.
@@ -129,16 +128,16 @@ public class Pack200Utils {
if (props == null) {
props = new HashMap<>();
}
- props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
+ props.put(Pack200Facade.Packer.SEGMENT_LIMIT, "-1");
final File tempFile = File.createTempFile("commons-compress", "pack200normalize");
try {
try (FileOutputStream fos = new FileOutputStream(tempFile);
JarFile jarFile = new JarFile(from)) {
- final Pack200.Packer packer = Pack200.newPacker();
+ final Pack200Facade.Packer packer = Pack200Facade.newPacker();
packer.properties().putAll(props);
packer.pack(jarFile, fos);
}
- final Pack200.Unpacker unpacker = Pack200.newUnpacker();
+ final Pack200Facade.Unpacker unpacker = Pack200Facade.newUnpacker();
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(to))) {
unpacker.unpack(tempFile, jos);
}
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200Adapter.java b/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200Adapter.java
deleted file mode 100644
index 8fc7784..0000000
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200Adapter.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.compress.harmony.pack200;
-
-import java.beans.PropertyChangeListener;
-import java.beans.PropertyChangeSupport;
-import java.util.SortedMap;
-import java.util.TreeMap;
-
-/**
- * Provides generic JavaBeans support for the Pack/UnpackAdapters
- */
-public abstract class Pack200Adapter {
-
- protected static final int DEFAULT_BUFFER_SIZE = 8192;
-
- private final PropertyChangeSupport support = new PropertyChangeSupport(this);
-
- private final SortedMap<String, String> properties = new TreeMap<String, String>();
-
- public SortedMap<String, String> properties() {
- return properties;
- }
-
- public void addPropertyChangeListener(PropertyChangeListener listener) {
- support.addPropertyChangeListener(listener);
- }
-
- protected void firePropertyChange(String propertyName, Object oldValue,
- Object newValue) {
- support.firePropertyChange(propertyName, oldValue, newValue);
- }
-
- public void removePropertyChangeListener(PropertyChangeListener listener) {
- support.removePropertyChangeListener(listener);
- }
-
- /**
- * Completion between 0..1
- * @param value
- */
- protected void completed(double value) {
- firePropertyChange("pack.progress", null, String.valueOf((int)(100*value))); //$NON-NLS-1$
- }
-}
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200PackerAdapter.java b/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200PackerAdapter.java
deleted file mode 100644
index b82aff0..0000000
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200PackerAdapter.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.compress.harmony.pack200;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.jar.JarFile;
-import java.util.jar.JarInputStream;
-import org.apache.commons.compress.java.util.jar.Pack200.Packer;
-
-
-/**
- * This class provides the binding between the standard Pack200 interface and the
- * internal interface for (un)packing. As this uses generics for the SortedMap,
- * this class must be compiled and run on a Java 1.5 system. However, Java 1.5
- * is not necessary to use the internal libraries for unpacking.
- */
-public class Pack200PackerAdapter extends Pack200Adapter implements Packer {
-
- private final PackingOptions options = new PackingOptions();
-
- public void pack(JarFile file, OutputStream out) throws IOException {
- if (file == null || out == null)
- throw new IllegalArgumentException(
- "Must specify both input and output streams");
- completed(0);
- try {
- new org.apache.commons.compress.harmony.pack200.Archive(file, out, options).pack();
- } catch (Pack200Exception e) {
- throw new IOException("Failed to pack Jar:" + String.valueOf(e));
- }
- completed(1);
- }
-
- public void pack(JarInputStream in, OutputStream out) throws IOException {
- if (in == null || out == null)
- throw new IllegalArgumentException(
- "Must specify both input and output streams");
- completed(0);
- PackingOptions options = new PackingOptions();
-
- try {
- new org.apache.commons.compress.harmony.pack200.Archive(in, out, options).pack();
- } catch (Pack200Exception e) {
- throw new IOException("Failed to pack Jar:" + String.valueOf(e));
- }
- completed(1);
- in.close();
- }
-
- protected void firePropertyChange(String propertyName, Object oldValue,
- Object newValue) {
- super.firePropertyChange(propertyName, oldValue, newValue);
- if(newValue != null && !newValue.equals(oldValue)) {
- if (propertyName.startsWith(CLASS_ATTRIBUTE_PFX)) {
- String attributeName = propertyName.substring(CLASS_ATTRIBUTE_PFX.length());
- options.addClassAttributeAction(attributeName, (String)newValue);
- } else if (propertyName.startsWith(CODE_ATTRIBUTE_PFX)) {
- String attributeName = propertyName.substring(CODE_ATTRIBUTE_PFX.length());
- options.addCodeAttributeAction(attributeName, (String)newValue);
- } else if (propertyName.equals(DEFLATE_HINT)) {
- options.setDeflateHint((String) newValue);
- } else if (propertyName.equals(EFFORT)) {
- options.setEffort(Integer.parseInt((String)newValue));
- } else if (propertyName.startsWith(FIELD_ATTRIBUTE_PFX)) {
- String attributeName = propertyName.substring(FIELD_ATTRIBUTE_PFX.length());
- options.addFieldAttributeAction(attributeName, (String)newValue);
- } else if (propertyName.equals(KEEP_FILE_ORDER)) {
- options.setKeepFileOrder(Boolean.parseBoolean((String)newValue));
- } else if (propertyName.startsWith(METHOD_ATTRIBUTE_PFX)) {
- String attributeName = propertyName.substring(METHOD_ATTRIBUTE_PFX.length());
- options.addMethodAttributeAction(attributeName, (String)newValue);
- } else if (propertyName.equals(MODIFICATION_TIME)) {
- options.setModificationTime((String)newValue);
- } else if (propertyName.startsWith(PASS_FILE_PFX)) {
- if(oldValue != null && !oldValue.equals("")) {
- options.removePassFile((String)oldValue);
- }
- options.addPassFile((String) newValue);
- } else if (propertyName.equals(SEGMENT_LIMIT)) {
- options.setSegmentLimit(Long.parseLong((String)newValue));
- } else if (propertyName.equals(UNKNOWN_ATTRIBUTE)) {
- options.setUnknownAttributeAction((String)newValue);
- }
- }
- }
-
-}
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/Pack200UnpackerAdapter.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/Pack200UnpackerAdapter.java
deleted file mode 100644
index ffb78de..0000000
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/Pack200UnpackerAdapter.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.compress.harmony.unpack200;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.jar.JarOutputStream;
-
-import org.apache.commons.compress.harmony.pack200.Pack200Adapter;
-import org.apache.commons.compress.harmony.pack200.Pack200Exception;
-import org.apache.commons.compress.java.util.jar.Pack200.Unpacker;
-
-/**
- * This class provides the binding between the standard Pack200 interface and
- * the internal interface for (un)packing. As this uses generics for the
- * SortedMap, this class must be compiled and run on a Java 1.5 system. However,
- * Java 1.5 is not necessary to use the internal libraries for unpacking.
- */
-public class Pack200UnpackerAdapter extends Pack200Adapter implements Unpacker {
- /*
- * (non-Javadoc)
- *
- * @see org.apache.commons.compress.java.util.jar.Pack200.Unpacker#unpack(java.io.InputStream,
- * java.util.jar.JarOutputStream)
- */
- public void unpack(InputStream in, JarOutputStream out) throws IOException {
- if (in == null || out == null)
- throw new IllegalArgumentException(
- "Must specify both input and output streams");
- completed(0);
- try {
- new Archive(in, out).unpack();
- } catch (Pack200Exception e) {
- throw new IOException("Failed to unpack Jar:" + String.valueOf(e));
- }
- completed(1);
- in.close();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.apache.commons.compress.java.util.jar.Pack200.Unpacker#unpack(java.io.File,
- * java.util.jar.JarOutputStream)
- */
- public void unpack(File file, JarOutputStream out) throws IOException {
- if (file == null || out == null)
- throw new IllegalArgumentException(
- "Must specify both input and output streams");
- int size = (int) file.length();
- int bufferSize = (size > 0 && size < DEFAULT_BUFFER_SIZE ? size
- : DEFAULT_BUFFER_SIZE);
- InputStream in = new BufferedInputStream(new FileInputStream(file),
- bufferSize);
- unpack(in, out);
- }
-}
diff --git a/src/main/java/org/apache/commons/compress/java/util/jar/Pack200.java b/src/main/java/org/apache/commons/compress/java/util/jar/Pack200.java
deleted file mode 100644
index a2c9df2..0000000
--- a/src/main/java/org/apache/commons/compress/java/util/jar/Pack200.java
+++ /dev/null
@@ -1,339 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.compress.java.util.jar;
-
-import java.beans.PropertyChangeListener;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.SortedMap;
-import java.util.jar.JarFile;
-import java.util.jar.JarInputStream;
-import java.util.jar.JarOutputStream;
-
-import org.apache.commons.compress.harmony.archive.internal.nls.Messages;
-
-/**
- * Class factory for {@link Pack200.Packer} and {@link Pack200.Unpacker}.
- */
-public abstract class Pack200 {
-
- private static final String SYSTEM_PROPERTY_PACKER = "java.util.jar.Pack200.Packer"; //$NON-NLS-1$
-
- private static final String SYSTEM_PROPERTY_UNPACKER = "java.util.jar.Pack200.Unpacker"; //$NON-NLS-1$
-
- /**
- * Prevent this class from being instantiated.
- */
- private Pack200() {
- // do nothing
- }
-
- /**
- * Returns a new instance of a packer engine.
- * <p>
- * The implementation of the packer engine is defined by the system property
- * {@code 'java.util.jar.Pack200.Packer'}. If this system property is
- * defined an instance of the specified class is returned, otherwise the
- * system's default implementation is returned.
- *
- * @return an instance of {@code Packer}
- */
- public static Pack200.Packer newPacker() {
- return (Packer) AccessController
- .doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
- String className = System
- .getProperty(SYSTEM_PROPERTY_PACKER,
- "org.apache.commons.compress.harmony.pack200.Pack200PackerAdapter"); //$NON-NLS-1$
- try {
- // TODO Not sure if this will cause problems with
- // loading the packer
- return ClassLoader.getSystemClassLoader()
- .loadClass(className).newInstance();
- } catch (Exception e) {
- throw new Error(Messages.getString("archive.3E",className), e); //$NON-NLS-1$
- }
- }
- });
-
- }
-
- /**
- * Returns a new instance of a unpacker engine.
- * <p>
- * The implementation of the unpacker engine is defined by the system
- * property {@code 'java.util.jar.Pack200.Unpacker'}. If this system
- * property is defined an instance of the specified class is returned,
- * otherwise the system's default implementation is returned.
- *
- * @return a instance of {@code Unpacker}.
- */
- public static Pack200.Unpacker newUnpacker() {
- return (Unpacker) AccessController
- .doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
- String className = System
- .getProperty(SYSTEM_PROPERTY_UNPACKER,
- "org.apache.commons.compress.harmony.unpack200.Pack200UnpackerAdapter");//$NON-NLS-1$
- try {
- return ClassLoader.getSystemClassLoader()
- .loadClass(className).newInstance();
- } catch (Exception e) {
- throw new Error(Messages.getString("archive.3E",className), e); //$NON-NLS-1$
- }
- }
- });
- }
-
- /**
- * The interface defining the API for converting a JAR file to an output
- * stream in the Pack200 format.
- */
- public static interface Packer {
-
- /**
- * the format of a class attribute name.
- */
- static final String CLASS_ATTRIBUTE_PFX = "pack.class.attribute."; //$NON-NLS-1$
-
- /**
- * the format of a code attribute name.
- */
- static final String CODE_ATTRIBUTE_PFX = "pack.code.attribute."; //$NON-NLS-1$
-
- /**
- * the deflation hint to set in the output archive.
- */
- static final String DEFLATE_HINT = "pack.deflate.hint";//$NON-NLS-1$
-
- /**
- * the indicated amount of effort to use in compressing the archive.
- */
- static final String EFFORT = "pack.effort";//$NON-NLS-1$
-
- /**
- * a String representation for {@code error}.
- */
- static final String ERROR = "error";//$NON-NLS-1$
-
- /**
- * a String representation of {@code false}.
- */
- static final String FALSE = "false";//$NON-NLS-1$
-
- /**
- * the format of a field attribute name.
- */
- static final String FIELD_ATTRIBUTE_PFX = "pack.field.attribute.";//$NON-NLS-1$
-
- /**
- * a String representation for {@code keep}.
- */
- static final String KEEP = "keep";//$NON-NLS-1$
-
- /**
- * decide if all elements shall transmit in their original order.
- */
- static final String KEEP_FILE_ORDER = "pack.keep.file.order";//$NON-NLS-1$
-
- /**
- * a String representation for {@code latest}.
- */
- static final String LATEST = "latest";//$NON-NLS-1$
-
- /**
- * the format of a method attribute name.
- */
- static final String METHOD_ATTRIBUTE_PFX = "pack.method.attribute.";//$NON-NLS-1$
-
- /**
- * if it shall attempt to determine the latest modification time if this
- * is set to {@code LATEST}.
- */
- static final String MODIFICATION_TIME = "pack.modification.time";//$NON-NLS-1$
-
- /**
- * a String representation of {@code pass}.
- */
- static final String PASS = "pass";//$NON-NLS-1$
-
- /**
- * the file that will not be compressed.
- */
- static final String PASS_FILE_PFX = "pack.pass.file.";//$NON-NLS-1$
-
- /**
- * packer progress as a percentage.
- */
- static final String PROGRESS = "pack.progress";//$NON-NLS-1$
-
- /**
- * The number of bytes of each archive segment.
- */
- static final String SEGMENT_LIMIT = "pack.segment.limit";//$NON-NLS-1$
-
- /**
- * a String representation of {@code strip}.
- */
- static final String STRIP = "strip";//$NON-NLS-1$
-
- /**
- * a String representation of {@code true}.
- */
- static final String TRUE = "true";//$NON-NLS-1$
-
- /**
- * the action to take if an unknown attribute is encountered.
- */
- static final String UNKNOWN_ATTRIBUTE = "pack.unknown.attribute";//$NON-NLS-1$
-
- /**
- * Returns a sorted map of the properties of this packer.
- *
- * @return the properties of the packer.
- */
- SortedMap<String, String> properties();
-
- /**
- * Pack the specified JAR file to the specified output stream.
- *
- * @param in
- * JAR file to be compressed.
- * @param out
- * stream of compressed data.
- * @throws IOException
- * if I/O exception occurs.
- */
- void pack(JarFile in, OutputStream out) throws IOException;
-
- /**
- * Pack the data from the specified jar input stream to the specified
- * output stream.
- *
- * @param in
- * stream of uncompressed JAR data.
- * @param out
- * stream of compressed data.
- * @throws IOException
- * if I/O exception occurs.
- */
- void pack(JarInputStream in, OutputStream out) throws IOException;
-
- /**
- * add a listener for PropertyChange events
- *
- * @param listener
- * the listener to listen if PropertyChange events occurs
- */
- void addPropertyChangeListener(PropertyChangeListener listener);
-
- /**
- * remove a listener
- *
- * @param listener
- * listener to remove
- */
- void removePropertyChangeListener(PropertyChangeListener listener);
- }
-
- /**
- * The interface defining the API for converting a packed stream in the
- * Pack200 format to a JAR file.
- */
- public static interface Unpacker {
-
- /**
- * The String indicating if the unpacker should ignore all transmitted
- * values,can be replaced by either {@code true} or {@code false}.
- */
- static final String DEFLATE_HINT = "unpack.deflate.hint";//$NON-NLS-1$
-
- /**
- * a String representation of {@code false}.
- */
- static final String FALSE = "false";//$NON-NLS-1$
-
- /**
- * a String representation of {@code keep}.
- */
- static final String KEEP = "keep";//$NON-NLS-1$
-
- /**
- * the progress as a {@code percentage}.
- */
- static final String PROGRESS = "unpack.progress";//$NON-NLS-1$
-
- /**
- * a String representation of {@code true}.
- */
- static final String TRUE = "true";//$NON-NLS-1$
-
- /**
- * Returns a sorted map of the properties of this unpacker.
- *
- * @return the properties of unpacker.
- */
- SortedMap<String, String> properties();
-
- /**
- * Unpack the specified stream to the specified JAR output stream.
- *
- * @param in
- * stream to uncompressed.
- * @param out
- * JAR output stream of uncompressed data.
- * @throws IOException
- * if I/O exception occurs.
- */
- void unpack(InputStream in, JarOutputStream out) throws IOException;
-
- /**
- * Unpack the contents of the specified {@code File} to the specified
- * JAR output stream.
- *
- * @param in
- * file to be uncompressed.
- * @param out
- * JAR output stream of uncompressed data.
- * @throws IOException
- * if I/O exception occurs.
- */
- void unpack(File in, JarOutputStream out) throws IOException;
-
- /**
- * add a listener for {@code PropertyChange} events.
- *
- * @param listener
- * the listener to listen if {@code PropertyChange} events
- * occurs.
- */
- void addPropertyChangeListener(PropertyChangeListener listener);
-
- /**
- * remove a listener.
- *
- * @param listener
- * listener to remove.
- */
- void removePropertyChangeListener(PropertyChangeListener listener);
- }
-
-}
diff --git a/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200FacadeTest.java b/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200FacadeTest.java
new file mode 100644
index 0000000..a598b0e
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200FacadeTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.commons.compress.compressors.pack200;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class Pack200FacadeTest {
+
+ @Test
+ public void loadsTheExpectedPacker() {
+ try {
+ Class.forName("java.util.jar.Pack200");
+ assertFalse(Pack200Facade.newPacker() instanceof HarmonyPack200.Pack200PackerAdapter);
+ } catch (ClassNotFoundException ex) {
+ assertTrue(Pack200Facade.newPacker() instanceof HarmonyPack200.Pack200PackerAdapter);
+ }
+ }
+
+ @Test
+ public void loadsTheExpectedUnpacker() {
+ try {
+ Class.forName("java.util.jar.Pack200");
+ assertFalse(Pack200Facade.newUnpacker() instanceof HarmonyPack200.Pack200UnpackerAdapter);
+ } catch (ClassNotFoundException ex) {
+ assertTrue(Pack200Facade.newUnpacker() instanceof HarmonyPack200.Pack200UnpackerAdapter);
+ }
+ }
+
+
+}