質問

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.

役に立ちましたか?

解決

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.

他のヒント

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();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top