Java HTTP Requests Buffer size

Posted by behrk2 on Stack Overflow See other posts from Stack Overflow or by behrk2
Published on 2010-04-16T13:05:03Z Indexed on 2010/04/16 13:13 UTC
Read the original article Hit count: 376

Filed under:
|
|
|
|

Hello,

I have an HTTP Request Dispatcher class that works most of the time, but I had noticed that it "stalls" when receiving larger requests. After looking into the problem, I thought that perhaps I wasn't allocating enough bytes to the buffer. Before, I was doing:

byte[] buffer = new byte[10000];

After changing it to 20000, it seems to have stopped stalling:

String contentType = connection.getHeaderField("Content-type");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            InputStream responseData = connection.openInputStream();
            byte[] buffer = new byte[20000];
            int bytesRead = responseData.read(buffer);
            while (bytesRead > 0) {
                baos.write(buffer, 0, bytesRead);
                bytesRead = responseData.read(buffer);
            }
            baos.close();
            connection.close();

Am I doing this right? Is there anyway that I can dynamically set the number of bytes for the buffer based on the size of the request?

Thanks...

© Stack Overflow or respective owner

Related posts about java

Related posts about http