Unexpected end of ZLIB input stream

I am trying to concatenate a JSON string into a byte array using DeflaterOutputStream, but the code below throws java.io.EOFException: Unexpected end of ZLIB input stream .

It works when you replace the string "Hello world" or delete a few characters from the line 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) 
+7
source share
3 answers

I believe this is due to the "no-wrap" option that you pass "true" to Deflater and Inflater . Setting both of them to false fixes the problem - although I would recommend installing string encoding in both places (for example, in UTF-8) instead of using the default system encoding.

The docs for "nowrap" are pretty vague, but they indicate:

Note. When using the "nowrap" option, you must also provide an additional "dummy" byte as input. This is required by the native ZLIB library to support certain optimizations.

Presumably this dummy input byte is missing, although it does not explain where it should go ...

+8
source

I had this problem and it happened because I did not close the output streams correctly.

+13
source

I had this problem too and cleaned up my project: Build> Clean Project

0
source

All Articles