Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Size of Java Deflater (and Inflater) Output Byte Buffer

Tags:

java

zlib

deflate

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.

like image 551
Tango Avatar asked Oct 21 '22 20:10

Tango


1 Answers

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.

like image 62
Peter Lawrey Avatar answered Nov 03 '22 21:11

Peter Lawrey