title: Why is the end of the input stream never reached using Java Sockets?
body: <p>I am writing a simple proxy in Java.  I am having trouble reading the entirety of a given request into a byte array.  Specifically, in the following loop, the call to 'read' blocks even though the client has sent all the data that it will (that is, the end of stream is never reached).  As I can't be sure that it is time to start writing output until I've read the entirety of the input, this is causing a bit of trouble.  If I kill the connection to the server, the end of stream is finally reached, and everything goes off without a hitch (all of the data from the client, in this case Firefox requesting www.google.com, has been read by the server, and it is able to process it as required, though obviously it can't send anything back to the client).</p>
  
  <pre><code>public static void copyStream(InputStream is, OutputStream os) throws IOException
  {
      int read = 0;
      byte[] buffer = new byte[BUFFER_SIZE];
      while((read = is.read(buffer, 0, BUFFER_SIZE)) != -1)
      {
        os.write(buffer, 0, read);
      }
      return;
  }
  </code></pre>
  
  <p>The InputStream comes from the client socket (getInputStream(), then buffered) directly; the OutputStream is a ByteArrayOutputStream.  </p>
  
  <p>What am I doing wrong?</p>