java.io.EOFException: Unexpected end of ZLIB input stream during reading stream data

5.6k views Asked by At

Below code is used to read data from an API. Stream of data from the API is in gzip encoding. When single API is hit then data comes correctly but when few APIs (different) are hit at the same time using multi threading then it throws below exception at reader.readLine() line in the below code.

JDK used: 1.8

Exception:

java.io.EOFException: Unexpected end of ZLIB input stream at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240) ~[?:1.8.0_191]

url = new URL(urlString);
                    urlConnection = (HttpURLConnection) url.openConnection();
    
                    if (urlConnection != null) {
                        //encoding
                        urlConnection.setConnectTimeout(5000);
                        urlConnection.setReadTimeout(0);
                        urlConnection.setRequestMethod("GET");
                        urlConnection.setRequestProperty("Authorization", "Basic " + encoded);
                        urlConnection.setRequestProperty("Accept-Encoding", "gzip");
    
                        urlResponseCode = urlConnection.getResponseCode();
                        if (urlResponseCode == HttpURLConnection.HTTP_OK) {
                            inputStream = urlConnection.getInputStream();
                            if (inputStream != null) {
                                if ("gzip".equalsIgnoreCase(urlConnection.getContentEncoding())) {
                                    reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(inputStream)));
                                } else {
                                    reader = new BufferedReader(new InputStreamReader(inputStream));
                                }
                                String line;
                                long mb = 1024 * 1024;
                                while ((line = reader.readLine()) != null) {
                                    //adding the line to a String Builder
                                }   

I know that there are some stackoverflow articles based on this exception but those are coming in different situation. So, i have asked this.

I also know that exception can be caught & moved forward. But this may lead to data loss. So, it's better if alternate approach is provided.

What could be the reason for this exception?

1

There are 1 answers

0
Stephen C On

I also know that exception can be caught & moved forward. But this may lead to data loss. So, it's better if alternate approach is provided.

Unless I am mistaken, there is nothing that you can do about it on the client side.

The problem is that the input stream that your client code is reading has been truncated.

It could be:

  • Application code in the server has not properly closed the stream that is generating the gzipped data. That is liable to cause the last block to not be compressed and written. (A flush() is not sufficient. The GZIPOutputStream itself must be closed ... assuming that is how the compress data stream is being generated.)
  • The server has set an incorrect content length in the response, causing the client side HTTP stack to truncate the stream.

It is also possible that the server has "given up" on the request because the client was too slow reading it, or encountered an error. Either of these things could be due to a client (or clients) overloading the server with requests. But if that is the case, the solution still has to be on the server side; e.g. reduce the number of server-side worker threads so that the server handles overload better.