title: InputStream.read(byte[], 0 length) stops early?
body: <p>I have been writing something to read a request stream (containing gzipped data) from an incoming HttpServletRequest ('request' below), however it appears that the normal InputStream read method doesn't actually read all content?</p>
  
  <p>My code was:</p>
  
  <pre><code>InputStream requestStream = request.getInputStream();
  if ((length = request.getContentLength()) != -1)
  {
      received = new byte[length];
      requestStream.read(received, 0, length);
  }
  else
  {
      // create a variable length list of bytes
      List&lt;Byte&gt; bytes = new ArrayList&lt;Byte&gt;();
  
      boolean endLoop = false;
      while (!endLoop)
      {
          // try and read the next value from the stream.. if not -1, add it to the list as a byte. if
          // it is, we've reached the end.
          int currentByte = requestStream.read();
          if (currentByte != -1)
              bytes.add((byte) currentByte);
          else
              endLoop = true;
      }
      // initialize the final byte[] to the right length and add each byte into it in the right order.
      received = new byte[bytes.size()];
      for (int i = 0; i &lt; bytes.size(); i++)
      {
          received[i] = bytes.get(i);
      }
  }
  </code></pre>
  
  <p>What I found during testing was that sometimes the top part (for when a content length is present) would just stop reading part way through the incoming request stream and leave the remainder of the 'received' byte array blank.  If I just make it run the else part of the if statement at all times, it reads fine and all the expected bytes are placed in 'received'.</p>
  
  <p>So, it seems like I can just leave my code alone now with that change, but does anyone have any idea why the normal 'read'(byte[], int, int)' method stopped reading?  The description says that it may stop if an end of file is present.  Could it be that the gzipped data just happened to include bytes matching whatever the signature for that looks like?</p>