2

I have a Servlet which has request ratio around 10,000 request/hour.

In which, I got one of these two exception frequently at same line which is around daily 5 to 6 times but not every time when this Servlet is called.

java.io.IOException: Stream closed
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:312)
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:200)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:154)
    at java.io.BufferedReader.read1(BufferedReader.java:205)
    at java.io.BufferedReader.read(BufferedReader.java:279)

or

java.io.IOException: Underlying input stream returned zero bytes
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:287)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:154)
        at java.io.BufferedReader.read1(BufferedReader.java:205)
        at java.io.BufferedReader.read(BufferedReader.java:279)

Both exception occurs at the same line.I don't know why? Same type of all requests excecuted and get responded successfully.

Here is my code :

InputStream ist = request.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ist));
StringBuffer sb = new StringBuffer();
char[] c = new char[1];
while(in.read(c, 0, 1) == 1)  // Getting exception at this line
{
    sb.append(c[0]);
}
String payload = sb.toString();

Note : Streams are properly closed at the end of the code.

Is it because of high request ratio? Give solution/suggestion if you have any.

8
  • The stream is closed. Did you close it? Commented Aug 6, 2014 at 15:57
  • No, that is not an issue. Closing of stream is properly handled, which is called at the end of all process.That's why I specially mentioned that I got this exception only 5 to 6 times per day not for all requests. Commented Aug 6, 2014 at 16:03
  • Is is possible another thread is accessing this same InputStream? Commented Aug 6, 2014 at 16:36
  • No..The Code which I post is the beginning part of simple servlet(not an async) where I try to get post data which is bind with request. After getting valid payload , it will be processed further but I got this exception before that while reading data. Commented Aug 6, 2014 at 17:25
  • 5
    most likely the client closed/broke the connection. networks interruptions happen all the time. Commented Aug 7, 2014 at 14:03

2 Answers 2

2

As @jtahlborn rightly mentioned in comment,

There is no bug in code. It happens mostly when the client closed / broke the connection or maybe network interruption / slowness which you can not control.

Luckily, I have never faced this error again.

Sign up to request clarification or add additional context in comments.

Comments

1

I was having an error similar to this one, where I received a 'java.io.IOException: Stream closed' (ServletException). After testing every part of the service separately, I realized that somehow the POST method was getting me the error. I replaced for a GET method and the error was gone. The case was similar, getting the error once in while (like 4-5 times in an hour for a 30-second-delay request). Now, after a couple of days, I haven't seen it anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.