6

I need to deflate one or more byte arrays and later inflate them back to normal size. I've looked over the example given in the api docs, and found some other examples.

After looking these examples over, I have two questions which may be unrelated, but they seem connected as I'm trying to understand this.

  1. In the API documentation example, the output buffer for both the Inflater and Deflater is set at 1024 bytes. The example data is only a short sentence, so that is reasonable. But how would I know how big to make the output buffer? Or will Deflater (and Inflater) adjust the size of the output buffer as needed?

  2. Instead of guessing at the size of a buffer, can I use ByteArrayOutputStream and wrap a DeflatorOutputStream around that? Since ByteArrayOutputStream changes the size of the byte array, it wouldn't be necessary to know the size of the output or guess at it, as it seems one would have to do in the API example.

Tango
  • 649
  • 1
  • 12
  • 29

2 Answers2

3

1.In the API documentation example, the output buffer for both the Inflater and Deflater is set at 1024 bytes. The example data is only a short sentence, so that is reasonable. But how would I know how big to make the output buffer? Or will Deflater (and Inflater) adjust the size of the output buffer as needed?

In streams, buffers are just temporary space before passing onto the another stream. Changing the buffers size can change performance but has little to do with the amount of data processed.

2.Instead of guessing at the size of a buffer, can I use ByteArrayOutputStream and wrap a DeflatorOutputStream around that? Since ByteArrayOutputStream changes the size of the byte array, it wouldn't be necessary to know the size of the output or guess at it, as it seems one would have to do in the API example.

You can do that, or you can send it directly to the stream you want the data to go to.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Your answer to #2 is a huge help - especially for what I'm doing. But I'm still not clear about #1. Does Deflater or Inflater change the output buffer size if needed? If not, how do I know how big to make the output byte array? – Tango Jan 07 '13 at 18:05
  • The buffer size doesn't change, nor would it need to as it is just temporary storage. You would only change it for performance reasons. – Peter Lawrey Jan 07 '13 at 19:03
  • So when I pass a byte[] to Deflater, it's only a working buffer? Where does the output go? From the API example, it looks like the output goes into the buffer passed to Deflater. – Tango Jan 07 '13 at 19:22
  • Using the Deflator directly is hard work, if you want to see how it is done, look at DeflatorOutputStream (or just use DeflatorOutputStream) – Peter Lawrey Jan 07 '13 at 19:23
  • 1
    Okay - that helps a **lot**! From what I had seen, it looked a LOT easier to just wrap a DeflaterOutputStream around a ByteArrayOutputStream and do it that way. I was wondering if something was wrong since that looked easier than just using Deflater - figured I must not be getting something. – Tango Jan 07 '13 at 19:42
0

Here's an example of compressing and decompressing using byte arrays:

import java.util.zip.Deflater;
import java.util.zip.InflaterInputStream;
...

byte[] sourceData; // bytes to compress (reuse byte[] for compressed data)
String filename; // where to write
{
    // compress the data
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
    deflater.setInput(sourceData);
    deflater.finish();
    int compressedSize = deflater.deflate(data, 0, sourceData.length, Deflater.FULL_FLUSH);

    // write the data   
    OutputStream stream = new FileOutputStream(filename);
    stream.write(data, 0, compressedSize);
    stream.close();
}

{
    byte[] uncompressedData = new byte[1024]; // where to store the data
    // read the data
    InputStream stream = new InflaterInputStream(new FileInputStream(filename)); 
    // read data - note: may not read fully (or evenly), read from stream until len==0
    int len, offset = 0;
    while ((len = stream.read(uncompressedData , offset, uncompressedData .length-offset))>0) {
        offset += len;
    }           
    stream.close();
}
Thomas Taylor
  • 1,311
  • 14
  • 15
  • You have a bug, you never define where `data` comes from. – Jason S Aug 12 '14 at 15:30
  • The data comes from file (the compressed data was previously written). See the line `InputStream stream = new InflaterInputStream(new FileInputStream(filename))`. `stream` now has a fileinputstream. – nikk Oct 25 '16 at 01:48