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 http://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).
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; }
The InputStream comes from the client socket (getInputStream(), then buffered) directly; the OutputStream is a ByteArrayOutputStream.
What am I doing wrong?
Typically in HTTP the
Content-Lengthheader indicates how much data you’re supposed to read from the stream. Basically it tells you how many bytes follow the double-newline (actually double-\r\n) that indicates the end of the HTTP headers. See W3C for more info…If there is no
Content-Lengthheader sent, you could try interrupting the read after a certain amount of time passes with no data sent over the connection, although that’s definitely not preferable.(I’m assuming that you’re going to be processing the data you’re reading somehow, otherwise you could just write out each byte as you read it)