I have a Tomcat server streaming data to a Java client over http. It is copying bytes from a file to HTTPServletResponse's outputstream in a servlet.
A client uses HttpURLConnection to connect and read the data.
Sometimes all is fine, other times both the client and server throw an exception.
The client says there is a "Premature EOF".
The server claims "ClientAbortException".
Isn't just one of the above possible?
CLIENT:
java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.fastRead(ChunkedInputStream.java:234)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:662)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2669)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2664)java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.fastRead(ChunkedInputStream.java:234)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:662)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2669)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2664)
SERVER:
ClientAbortException: java.io.IOException
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:358)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:434)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:349)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:381)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:370)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89)
...
Caused by: java.io.IOException
at org.apache.coyote.ajp.AjpAprProcessor.flush(AjpAprProcessor.java:1223)
at org.apache.coyote.ajp.AjpAprProcessor$SocketOutputBuffer.doWrite(AjpAprProcessor.java:1310)
at org.apache.coyote.Response.doWrite(Response.java:560)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:353)
... 23 more
They are not mutually exclusive.
This situation can and will occur if the socket is closed unexpectedly. For example, consider what would happen if your firewall just terminated the socket. From the server's point of view, when it tried to write data, the socket would appear closed, and the ClientAbortException would be triggered. From the client's point of view, the next read of bytes would fail, causing the premature end exception.
I was having similar problem while ago and I solved it by not using BufferedReader
but reading one byte at a time and put the read in a try-catch for EOFException. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With