1

Here's example from the documentation:

 byte[] output = new byte[100];
 Deflater compresser = new Deflater();
 compresser.setInput(input);
 compresser.finish();
 int compressedDataLength = compresser.deflate(output);
 compresser.end();

but DeflaterOutputStream does this:

        if (!def.finished()) {
            def.setInput(b, off, len);
            while (!def.needsInput()) {
                 int len = def.deflate(buf, 0, buf.length);
                 if (len > 0) {
                 out.write(buf, 0, len);
               }
            }
        }

which makes me think that just because you've gotten a non-zero return from a call to deflate() does not mean that your entire input was compressed.

Is this correct? In the documentation, I'm having trouble finding where this is explicitly mentioned.

1
  • There might not be room in the buffer to compress the entire input.
    – tgdavies
    Commented Feb 11 at 21:22

1 Answer 1

2

It will not have finished if you didn't give it enough output space. You need to keep calling deflate() with more output space until finished() is true.

Note that it will never report that it finished if you didn't tell it that you have provided all of the input with finish(). Which the example does.

16
  • So if you call finish() before, then needsInput() == finished() and if you don't call finish(), then you can add more data input to be a part of the same "block"? Commented Feb 11 at 21:41
  • Yes, until you call finish(), you can keep providing more input with setInput() and more output space with deflate(), and keep compressing forever.
    – Mark Adler
    Commented Feb 11 at 21:48
  • I don't understand your needsInput() == finished(). What you could do is have an inner loop calling deflate() until the output buffer is not filled, and an outer loop called setInput() to provide more input. When that outer loop has reached the end of the input it intends to provide, it would call finish() before heading to the inner loop.
    – Mark Adler
    Commented Feb 11 at 21:50
  • 1
    I said until you request a flush.
    – Mark Adler
    Commented Feb 12 at 17:09
  • 2
    Comments are not for questions. Questions are for questions. I'm not going to answer any more of the infinite stream of questions in the comments here.
    – Mark Adler
    Commented Feb 12 at 17:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.