title: Socket Timeout while reading from Servlet request inputstream
body: <p>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.</p>
  
  <p>For most cases and regular data, the code works perfectly fine. However in some cases where data is large (<code>content length = 600000</code>), 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.</p>
  
  <p>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 <code>Thread.sleep(400)</code> before the code and the code started to work fine for IE as well.</p>
  
  <p>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.</p>
  
  <p>Is there a way where i can tell the channel to not time out or remove the timeout altogether?</p>
  
  <p>Below is code being used,</p>
  
  <pre><code>ReadableByteChannel channel = Channels.newChannel(inputStream);
  byte[] postData = new byte[contentLength];
  ByteBuffer buf = ByteBuffer.allocateDirect(contentLength);
  int numRead = 0;
  int counter = 0;
  while (numRead &gt;= 0) {
      buf.rewind();
      numRead = channel.read(buf);
      buf.rewind();
      for (int i = 0; i &lt; numRead; i++) {
         postData[counter++] = buf.get();
      }
  }
  return postData;
  </code></pre>
  
  <p>The inputStream is directly via request.getInputStream() and content length is via request.getContentLength().</p>
  
  <p>The container used is Tomcat 7.0.42 in embedded mode.</p>