Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading streams over a HTTP network using Java I/O

Right now I'm working with performance improvement with regards to the java I/O. I have some crazy doubts with reading / writing streams over the network using java I/O as I mentioned below. There are several opinions coming and going in my mind. But I want to clear out all of them

Code

URL url = new URL("http://example.com/connector/url2Service");  

URLConnection urlConnection = url.openConnection(); // Position 1

HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;

String requestStr = buildRequestString();// Position 2

ByteArrayOutputStream rqByteArrayOutputStream = new ByteArrayOutputStream();
rqByteArrayOutputStream.write(((String)requestStr).getBytes()); // Position 3

httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");

rqByteArrayOutputStream.writeTo(httpURLConnection.getOutputStream()); // Position 4

// Waiting for the response.

InputStream inputStream = httpURLConnection.getInputStream(); // Position 5

ByteArrayOutputStream rsByteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int length = 0;

while ((length = inputStream.read(buffer)) != -1) { // Position 6
    rsByteArrayOutputStream.write(buffer, 0, length);// Position 7
}

String response  = new String(rsByteArrayOutputStream.toByteArray());// Position 8

My understanding

  • Position 1 : This will provides an object to communicate with the remote resorce. But the connection has not been established.
  • Position 2 : Build and Get the request.
  • Position 3 : Write the bytes to the ByteArrayOutputStream.
  • Position 4 : This is the place that the communicating starts with the server. We are writing Bytes. So, the server can START to read them. When the execution exits this line, we have finished sending the request object.
  • Position 5 : When we exit this line the server has finished sending the response object. So, we can start reading the response object.
  • Position 6 : Reading the object as 4096 byte chunks.
  • Position 7 : Writing the read bytes in to a ByteArrayOutputStream.
  • Position 8 : Completed reading the response and converted it to a String.

My Problems

  1. What is the point that we can say that the request uploading is completed? (I believe this is completed when we the execution exists the Position 4)
  2. What is the point that we can say that the response downloading is completed? (I have a doubt between Point 5 and 8)
  3. When we exit the Point 5, is that mean the response is fully downloaded or it is just started to download?
  4. Upto which point Network (Bandwidth) will effect to the performance? (Point 5, 6, 7...)
  5. Right now I'm working on a tuning up the InputStream reading code. If you have any suggestion please share?

References :

  • http://www.oracle.com/technetwork/articles/javase/perftuning-137844.html
  • http://www.oracle.com/technetwork/java/performance-139413.html
  • http://www.javaworld.com/article/2076241/build-ci-sdlc/tweak-your-io-performance-for-faster-runtime.html
  • http://www.kegel.com/java/wp-javaio.html
like image 582
namalfernandolk Avatar asked Mar 20 '23 15:03

namalfernandolk


1 Answers

What is the point that we can say that the request uploading is completed? (I believe this is completed when we the execution exists the Position 4)

Yes, but you've already wasted some time with the ByteArrayOutputStream. Just write the request directly to the connection output stream. Save memory and latency.

What is the point that we can say that the response downloading is completed? (I have a doubt between Point 5 and 8)

When you receive -1 from the read() method.

When we exit the Point 5, is that mean the response is fully downloaded or it is just started to download?

Neither. The request has been written and you haven't started to do load anything, so nothing has been downloaded. It may have started arriving at the socket receive buffer, but you can't see that.

Up to which point Network (Bandwidth) will effect to the performance? (Point 5, 6, 7...)

None of those. You are using network bandwidth until you get the -1.

Right now I'm working on a tuning up the InputStream reading code. If you have any suggestion please share?

Use a bigger buffer. There's not much else you can do.

like image 115
user207421 Avatar answered Mar 22 '23 06:03

user207421