Author: bodewig Date: Fri Feb 28 12:17:32 2014 New Revision: 1572929 URL: http://svn.apache.org/r1572929 Log: COMPRESS-257 add the remaining BCJ implementations provided by XZ for Java
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java?rev=1572929&r1=1572928&r2=1572929&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java Fri Feb 28 12:17:32 2014 @@ -18,6 +18,7 @@ package org.apache.commons.compress.archivers.sevenz; import java.io.FilterInputStream; +import java.io.FilterOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -31,8 +32,15 @@ import java.util.zip.InflaterInputStream import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; +import org.tukaani.xz.ARMOptions; +import org.tukaani.xz.ARMThumbOptions; +import org.tukaani.xz.FilterOptions; +import org.tukaani.xz.FinishableOutputStream; import org.tukaani.xz.FinishableWrapperOutputStream; +import org.tukaani.xz.IA64Options; import org.tukaani.xz.LZMAInputStream; +import org.tukaani.xz.PowerPCOptions; +import org.tukaani.xz.SPARCOptions; import org.tukaani.xz.X86Options; class Coders { @@ -43,7 +51,12 @@ class Coders { put(SevenZMethod.DEFLATE, new DeflateDecoder()); put(SevenZMethod.BZIP2, new BZIP2Decoder()); put(SevenZMethod.AES256SHA256, new AES256SHA256Decoder()); - put(SevenZMethod.X86, new X86Decoder()); + put(SevenZMethod.BCJ_X86_FILTER, new BCJDecoder(new X86Options())); + put(SevenZMethod.BCJ_PPC_FILTER, new BCJDecoder(new PowerPCOptions())); + put(SevenZMethod.BCJ_IA64_FILTER, new BCJDecoder(new IA64Options())); + put(SevenZMethod.BCJ_ARM_FILTER, new BCJDecoder(new ARMOptions())); + put(SevenZMethod.BCJ_ARM_THUMB_FILTER, new BCJDecoder(new ARMThumbOptions())); + put(SevenZMethod.BCJ_SPARC_FILTER, new BCJDecoder(new SPARCOptions())); }}; static CoderBase findByMethod(SevenZMethod method) { @@ -97,15 +110,25 @@ class Coders { } } - static class X86Decoder extends CoderBase { + static class BCJDecoder extends CoderBase { + private final FilterOptions opts; + BCJDecoder(FilterOptions opts) { + this.opts = opts; + } + @Override InputStream decode(final InputStream in, final Coder coder, byte[] password) throws IOException { - return new X86Options().getInputStream(in); + return opts.getInputStream(in); } @Override OutputStream encode(final OutputStream out, final Object _) { - return new X86Options().getOutputStream(new FinishableWrapperOutputStream(out)); + final FinishableOutputStream fo = opts.getOutputStream(new FinishableWrapperOutputStream(out)); + return new FilterOutputStream(fo) { + @Override + public void flush() { + } + }; } } Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java?rev=1572929&r1=1572928&r2=1572929&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethod.java Fri Feb 28 12:17:32 2014 @@ -21,6 +21,17 @@ import java.util.Arrays; /** * The (partially) supported compression/encryption methods used in 7z archives. + * + * <p>All methods with a _FILTER suffix are used as preprocessors with + * the goal of creating a better compression ratio with the compressor + * that comes next in the chain of methods. 7z will in general only + * allow them to be used together with a "real" compression method but + * Commons Compress doesn't enforce this.</p> + * + * <p>The BCJ_ filters work on executable files for the given platform + * and convert relative addresses to absolute addresses in CALL + * instructions. This means they are only useful when applied to + * executables of the chosen platform.</p> */ public enum SevenZMethod { /** no compression at all */ @@ -39,10 +50,35 @@ public enum SevenZMethod { */ AES256SHA256(new byte[] { (byte)0x06, (byte)0xf1, (byte)0x07, (byte)0x01 }), /** - * BCJ x86 version 1. + * BCJ x86 platform version 1. * @since 1.8 */ - X86(new byte[] { 0x03, 0x03, 0x01, 0x03 }); + BCJ_X86_FILTER(new byte[] { 0x03, 0x03, 0x01, 0x03 }), + /** + * BCJ PowerPC platform. + * @since 1.8 + */ + BCJ_PPC_FILTER(new byte[] { 0x03, 0x03, 0x02, 0x05 }), + /** + * BCJ I64 platform. + * @since 1.8 + */ + BCJ_IA64_FILTER(new byte[] { 0x03, 0x03, 0x04, 0x01 }), + /** + * BCJ ARM platform. + * @since 1.8 + */ + BCJ_ARM_FILTER(new byte[] { 0x03, 0x03, 0x05, 0x01 }), + /** + * BCJ ARM Thumb platform. + * @since 1.8 + */ + BCJ_ARM_THUMB_FILTER(new byte[] { 0x03, 0x03, 0x07, 0x01 }), + /** + * BCJ Sparc platform. + * @since 1.8 + */ + BCJ_SPARC_FILTER(new byte[] { 0x03, 0x03, 0x08, 0x05 }); private final byte[] id; Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java?rev=1572929&r1=1572928&r2=1572929&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java Fri Feb 28 12:17:32 2014 @@ -234,6 +234,30 @@ public class SevenZOutputFileTest extend testRoundTrip(SevenZMethod.DEFLATE); } + public void testBCJX86Roundtrip() throws Exception { + testFilterRoundTrip(new SevenZMethodConfiguration(SevenZMethod.BCJ_X86_FILTER)); + } + + public void testBCJARMRoundtrip() throws Exception { + testFilterRoundTrip(new SevenZMethodConfiguration(SevenZMethod.BCJ_ARM_FILTER)); + } + + public void testBCJARMThumbRoundtrip() throws Exception { + testFilterRoundTrip(new SevenZMethodConfiguration(SevenZMethod.BCJ_ARM_THUMB_FILTER)); + } + + public void testBCJIA64Roundtrip() throws Exception { + testFilterRoundTrip(new SevenZMethodConfiguration(SevenZMethod.BCJ_IA64_FILTER)); + } + + public void testBCJPPCRoundtrip() throws Exception { + testFilterRoundTrip(new SevenZMethodConfiguration(SevenZMethod.BCJ_PPC_FILTER)); + } + + public void testBCJSparcRoundtrip() throws Exception { + testFilterRoundTrip(new SevenZMethodConfiguration(SevenZMethod.BCJ_SPARC_FILTER)); + } + public void testStackOfContentCompressions() throws Exception { output = new File(dir, "multiple-methods.7z"); ArrayList<SevenZMethodConfiguration> methods = new ArrayList<SevenZMethodConfiguration>(); @@ -393,6 +417,14 @@ public class SevenZOutputFileTest extend createAndReadBack(output, methods); } + private void testFilterRoundTrip(SevenZMethodConfiguration method) throws Exception { + output = new File(dir, method + "-roundtrip.7z"); + ArrayList<SevenZMethodConfiguration> methods = new ArrayList<SevenZMethodConfiguration>(); + methods.add(method); + methods.add(new SevenZMethodConfiguration(SevenZMethod.LZMA2)); + createAndReadBack(output, methods); + } + private void createAndReadBack(File output, Iterable<SevenZMethodConfiguration> methods) throws Exception { SevenZOutputFile outArchive = new SevenZOutputFile(output); outArchive.setContentMethods(methods);