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
My Problems
References :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With