Hi,
For example: I have compressed data (deflate) in byte array. And I want to uncompress the byte array:
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
but sometimes I dont`t know, somebody set 'nowrap' on true or false.
If somebody compressed data by means of:
Deflater def = new Deflater(1, true);
then, when I will use:
Inflater decompresser = new Inflater(); // = new Inflater(false)
there is an exception:
java.util.zip.DataFormatException: unknown compression method
because, I had to use:
Inflater decompresser = new Inflater(true);
I have written a class in order to avoid this problem:
public class NewInflaterInputStream extends FilterInputStream
{
public InflaterInputStream(InputStream in) throws IOException
{
super(in);
in = new PushbackInputStream(in);
int firstByte = in.read();
((PushbackInputStream) in).unread(firstByte);
if (firstByte == 120)
this.in = new java.util.zip.InflaterInputStream(in);
else
this.in =
new java.util.zip.InflaterInputStream(in, new Inflater(true));
}
}
But, I think, it isn`t a good idea. Do you have any better idea?