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?