9

I'm trying to round-trip a JSON string to a byte array with DeflaterOutputStream, but the code below throwing java.io.EOFException: Unexpected end of ZLIB input stream.

It works when you replace the string with "Hello world", or if you remove a few characters from the string below.

Any ideas?

public static void main(String[] args) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    DeflaterOutputStream deflate = new DeflaterOutputStream(bytes, new Deflater(Deflater.BEST_COMPRESSION, true));
    OutputStreamWriter writer = new OutputStreamWriter(deflate);
    writer.write("[1,null,null,\"a\",null,null,null,null,[1,null,null,null,null,null,null,null,null,null,null,null,null,0.0,0.0,null,null]");
    writer.flush();
    writer.close();

    InflaterInputStream inflaterIn = new InflaterInputStream(new ByteArrayInputStream(bytes.toByteArray()), new Inflater(true));
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inflaterIn));
    System.out.println(bufferedReader.readLine());
}

Java version (OSX):

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)
slipheed
  • 7,170
  • 3
  • 27
  • 38

4 Answers4

16

I had this problem and it was because i wasn't correctly closing my output streams.

Sam
  • 6,240
  • 4
  • 42
  • 53
  • 9
    I had this a similar problem with `GZIPOutputStream`. I needed to call `finish()` after I was done writing to the gzipped stream. – abellina Mar 29 '13 at 16:29
8

I believe it's to do with the "no-wrap" option which you're passing "true" for in both the Deflater and Inflater. Setting both of these to false fixes the problem - although I'd recommend setting the string encoding in both places to (e.g. to UTF-8) instead of using the system default encoding.

The docs for "nowrap" are fairly vague, but they state:

Note: When using the 'nowrap' option it is also necessary to provide an extra "dummy" byte as input. This is required by the ZLIB native library in order to support certain optimizations.

Presumably this dummy input byte is missing, although it doesn't explain where it should go...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

I had this problem too and what I did was to clean up my project: Build > Clean Project

Christian
  • 4,902
  • 4
  • 24
  • 42
-1

I had the same problem. It was happening because the response was chunked. I fixed it by adding the right header.

HttpHeaders.ACCEPT_ENCODING,"deflate"