I have a piece of code which reads the POST data from the Servlet Request's input stream. I am using java nio for reading the data.
For most cases and regular data, the code works perfectly fine. However in some cases where data is large (content length = 600000
), the Channel's read method seems to fail with a Socket timeout error. Also this seems to happen only with IE 9, it is working fine with Firefox and Chrome.
While investigating this i figured, that while using IE, the post data seems to take a bit longer than the other browsers to be available for reading. So i put a Thread.sleep(400)
before the code and the code started to work fine for IE as well.
I don't want to put a sleep before this code, one its just a workaround and not a proper solution, second, there is no correct sleep time, since if the data increases, 400 might not be enough.
Is there a way where i can tell the channel to not time out or remove the timeout altogether?
Below is code being used,
ReadableByteChannel channel = Channels.newChannel(inputStream);
byte[] postData = new byte[contentLength];
ByteBuffer buf = ByteBuffer.allocateDirect(contentLength);
int numRead = 0;
int counter = 0;
while (numRead >= 0) {
buf.rewind();
numRead = channel.read(buf);
buf.rewind();
for (int i = 0; i < numRead; i++) {
postData[counter++] = buf.get();
}
}
return postData;
The inputStream is directly via request.getInputStream() and content length is via request.getContentLength().
The container used is Tomcat 7.0.42 in embedded mode.