28package com.jogamp.common.util;
30import java.io.DataInputStream;
31import java.io.DataOutputStream;
32import java.io.IOException;
33import java.io.InputStream;
34import java.io.OutputStream;
35import java.util.zip.DataFormatException;
36import java.util.zip.Deflater;
37import java.util.zip.Inflater;
53 public static final int MAGIC = 0xDEF1A7E0;
63 throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException
68 final DataInputStream din =
new DataInputStream(in);
69 final int _magic = din.readInt();
70 if( _magic !=
MAGIC ) {
71 throw new IOException(
"wrong magic: "+Integer.toHexString(_magic)+
", expected "+Integer.toHexString(
MAGIC));
73 inLen = din.readInt();
74 outLen = din.readInt();
91 public static byte[]
inflateFromStream(
final InputStream in,
final int inLen,
final int outLen,
92 final byte[] output,
final int outOff)
93 throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException
95 if (inLen <= 0 || outLen <= 0 ) {
96 throw new IllegalArgumentException(
"Length[input "+inLen+
", output "+outLen+
"]");
98 if (outOff < 0 || output.length < outOff + outLen) {
99 throw new ArrayIndexOutOfBoundsException(
"output.length "+output.length+
", offset "+outOff+
", length "+outLen);
101 final byte[] input =
new byte[inLen];
105 final int remBytes = inLen - numBytes;
107 if ( 0 >= remBytes || (count = in.read(input, numBytes, remBytes)) == -1 ) {
115 if( inLen != numBytes ) {
116 throw new IOException(
"Got "+numBytes+
" bytes != expected "+inLen);
119 final Inflater inflater =
new Inflater();
120 inflater.setInput(input, 0, inLen);
121 final int outSize = inflater.inflate(output, outOff, outLen);
123 if( outLen != outSize ) {
124 throw new IOException(
"Got inflated "+outSize+
" bytes != expected "+outLen);
126 }
catch(
final DataFormatException dfe) {
127 throw new IOException(dfe);
143 public static int deflateToStream(
final byte[] input,
final int inOff,
final int inLen,
144 final int level,
final OutputStream out)
throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException {
146 throw new IllegalArgumentException(
"Length[input "+inLen+
"]");
148 if (inOff < 0 || input.length < inOff + inLen) {
149 throw new ArrayIndexOutOfBoundsException(
"input.length "+input.length+
", offset "+inOff+
", length "+inLen);
151 final byte[] output =
new byte[inLen];
152 final Deflater deflater =
new Deflater(level);
153 deflater.setInput(input, inOff, inLen);
155 final int outSize = deflater.deflate(output, 0, inLen);
158 final DataOutputStream dout =
new DataOutputStream(out);
160 dout.writeInt(outSize);
161 dout.writeInt(inLen);
163 out.write(output, 0, outSize);
All in memory inflater / deflator for small chunks using streams.
static byte[] inflateFromStream(final InputStream in)
static byte[] inflateFromStream(final InputStream in, final int inLen, final int outLen, final byte[] output, final int outOff)
static int deflateToStream(final byte[] input, final int inOff, final int inLen, final int level, final OutputStream out)
static final int MAGIC
Start of stream header for deflated data.