diff options
author | Sven Gothel <[email protected]> | 2015-09-24 18:31:03 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2015-09-24 18:31:03 +0200 |
commit | a1009b450e68c083c0d3c56129602697a4f674d9 (patch) | |
tree | 4a585b6e73e012d00b7d094a0123190ccb401abf /src | |
parent | 0e723ff0866919cbc4ddfc3d7773c3485252eba3 (diff) |
Bug 1231: Usability of CustomCompress API incl zip-level; Replace test-exe deflated files w/ zip-level 9
Diffstat (limited to 'src')
-rw-r--r-- | src/java/com/jogamp/common/util/CustomCompress.java | 96 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/bin/exe-windows-i386.defl | bin | 305 -> 300 bytes | |||
-rw-r--r-- | src/java/com/jogamp/common/util/bin/exe-windows-x86_64.defl | bin | 353 -> 345 bytes | |||
-rw-r--r-- | src/junit/com/jogamp/common/util/CustomDeflate.java | 2 |
4 files changed, 72 insertions, 26 deletions
diff --git a/src/java/com/jogamp/common/util/CustomCompress.java b/src/java/com/jogamp/common/util/CustomCompress.java index 6850873..6bca095 100644 --- a/src/java/com/jogamp/common/util/CustomCompress.java +++ b/src/java/com/jogamp/common/util/CustomCompress.java @@ -39,7 +39,7 @@ import java.util.zip.Inflater; /** * All in memory inflater / deflator for small chunks using streams * <p> - * Stream format for deflated data: + * Stream header of deflated data: * <ul> * <li>4 bytes magic 0xDEF1A7E0 (Big Endian)</li> * <li>4 bytes integer deflated-size (Big Endian)</li> @@ -49,31 +49,60 @@ import java.util.zip.Inflater; * </p> */ public class CustomCompress { + /** Start of stream header for deflated data */ public static final int MAGIC = 0xDEF1A7E0; - public static byte[] inflateFromStream(final InputStream in) throws IOException { - final int inSize; - final int outSize; + /** + * + * @param in {@link InputStream} at start of stream header, i.e. position {@link #MAGIC}. + * @return the inflated bytes from the stream + * @throws IOException if an I/O or deflation exception occurs + * @throws IllegalArgumentException if {@code inLen} ≤ 0 or {@code outLen} ≤ 0, as read from header + */ + public static byte[] inflateFromStream(final InputStream in) + throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException + { + final int inLen; + final int outLen; { final DataInputStream din = new DataInputStream(in); final int _magic = din.readInt(); if( _magic != MAGIC ) { throw new IOException("wrong magic: "+Integer.toHexString(_magic)+", expected "+Integer.toHexString(MAGIC)); } - inSize = din.readInt(); - outSize = din.readInt(); + inLen = din.readInt(); + outLen = din.readInt(); } - if( 0 >= inSize ) { - throw new IOException("Invalid deflated-size "+inSize); + return inflateFromStream(in, inLen, outLen, new byte[outLen], 0); + } + + /** + * + * @param in {@link InputStream} at start of deflated bytes, i.e. after the stream header. + * @param inLen number of deflated bytes in stream {@code in} + * @param outLen number of inflated {@code output} bytes at {@code outOff} + * @param output sink for deflated bytes + * @param outOff offset to {@code output} + * @return the inflated bytes from the stream, passing {@code output} for chaining + * @throws IOException if an I/O or deflation exception occurs + * @throws ArrayIndexOutOfBoundsException if {@code outOff} and {@code outLen} exceeds {@code output} + * @throws IllegalArgumentException if {@code inLen} ≤ 0 or {@code outLen} ≤ 0 + */ + public static byte[] inflateFromStream(final InputStream in, final int inLen, final int outLen, + final byte[] output, final int outOff) + throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException + { + if (inLen <= 0 || outLen <= 0 ) { + throw new IllegalArgumentException("Length[input "+inLen+", output "+outLen+"]"); } - if( 0 >= outSize ) { - throw new IOException("Invalid inflated-size "+outSize); + if (outOff < 0 || output.length < outOff + outLen) { + throw new ArrayIndexOutOfBoundsException("output.length "+output.length+", offset "+outOff+", length "+outLen); } - final byte[] input = new byte[inSize]; + final byte[] input = new byte[inLen]; int numBytes = 0; try { while (true) { - final int remBytes = inSize - numBytes; + final int remBytes = inLen - numBytes; int count; if ( 0 >= remBytes || (count = in.read(input, numBytes, remBytes)) == -1 ) { break; @@ -83,17 +112,16 @@ public class CustomCompress { } finally { in.close(); } - if( inSize != numBytes ) { - throw new IOException("Got "+numBytes+" bytes != expected "+inSize); + if( inLen != numBytes ) { + throw new IOException("Got "+numBytes+" bytes != expected "+inLen); } - final byte[] output = new byte[outSize]; try { final Inflater inflater = new Inflater(); - inflater.setInput(input, 0, inSize); - final int outSize2 = inflater.inflate(output, 0, outSize); + inflater.setInput(input, 0, inLen); + final int outSize = inflater.inflate(output, outOff, outLen); inflater.end(); - if( outSize != outSize2 ) { - throw new IOException("Got inflated "+outSize2+" bytes != expected "+outSize); + if( outLen != outSize ) { + throw new IOException("Got inflated "+outSize+" bytes != expected "+outLen); } } catch(final DataFormatException dfe) { throw new IOException(dfe); @@ -101,18 +129,36 @@ public class CustomCompress { return output; } - public static int deflateToStream(final byte[] input, final OutputStream out) throws IOException { - final byte[] output = new byte[input.length]; - final Deflater deflater = new Deflater(); - deflater.setInput(input, 0, input.length); + /** + * @param input raw input bytes + * @param inOff offset to {@code input} + * @param inLen number of {@code input} bytes at {@code inOff} + * @param level compression level 0-9 or {@link Deflater#DEFAULT_COMPRESSION} + * @param out sink for deflated bytes + * @return number of deflated bytes written, not including the header. + * @throws IOException if an I/O or deflation exception occurs + * @throws ArrayIndexOutOfBoundsException if {@code inOff} and {@code inLen} exceeds {@code input} + * @throws IllegalArgumentException if {@code inLen} ≤ 0 + */ + public static int deflateToStream(final byte[] input, final int inOff, final int inLen, + final int level, final OutputStream out) throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException { + if (inLen <= 0 ) { + throw new IllegalArgumentException("Length[input "+inLen+"]"); + } + if (inOff < 0 || input.length < inOff + inLen) { + throw new ArrayIndexOutOfBoundsException("input.length "+input.length+", offset "+inOff+", length "+inLen); + } + final byte[] output = new byte[inLen]; + final Deflater deflater = new Deflater(level); + deflater.setInput(input, inOff, inLen); deflater.finish(); - final int outSize = deflater.deflate(output, 0, output.length); + final int outSize = deflater.deflate(output, 0, inLen); deflater.end(); { final DataOutputStream dout = new DataOutputStream(out); dout.writeInt(CustomCompress.MAGIC); dout.writeInt(outSize); - dout.writeInt(input.length); + dout.writeInt(inLen); } out.write(output, 0, outSize); return outSize; diff --git a/src/java/com/jogamp/common/util/bin/exe-windows-i386.defl b/src/java/com/jogamp/common/util/bin/exe-windows-i386.defl Binary files differindex 9e68aea..d8f1716 100644 --- a/src/java/com/jogamp/common/util/bin/exe-windows-i386.defl +++ b/src/java/com/jogamp/common/util/bin/exe-windows-i386.defl diff --git a/src/java/com/jogamp/common/util/bin/exe-windows-x86_64.defl b/src/java/com/jogamp/common/util/bin/exe-windows-x86_64.defl Binary files differindex e4b8d8d..be0998b 100644 --- a/src/java/com/jogamp/common/util/bin/exe-windows-x86_64.defl +++ b/src/java/com/jogamp/common/util/bin/exe-windows-x86_64.defl diff --git a/src/junit/com/jogamp/common/util/CustomDeflate.java b/src/junit/com/jogamp/common/util/CustomDeflate.java index 876c6e4..d4682e8 100644 --- a/src/junit/com/jogamp/common/util/CustomDeflate.java +++ b/src/junit/com/jogamp/common/util/CustomDeflate.java @@ -71,7 +71,7 @@ public class CustomDeflate { throw new IOException("Got "+numBytes+" bytes != expected "+inSize); } out = new FileOutputStream(fileOut); - CustomCompress.deflateToStream(input, out); + CustomCompress.deflateToStream(input, 0, inSize, 9, out); } catch (final IOException ioe) { ioe.printStackTrace(); } finally { |