Reading streams over HTTP networks using Java I / O

Now I'm trying to improve the performance of Java I / O I have some crazy questions about using Java I / O to read / write streams on the network, as described below Several opinions came to mind But I want to get rid of all this

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,length);// Position 7
}

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

I understand

>Location 1: this will provide the object to communicate with the remote resource But this connection has not yet been established. > Location 2: build and get request. > Position 3: write bytes to bytearrayoutputstream. > Location 4: This is the place to communicate with the server We are writing bytes Therefore, the server can start reading 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: read the object as 4096 byte block. > Position 7: write the read bytes to bytearrayoutputstream. > Position 8: finish reading the response and converting it to a string

My question

>We can say what is the focus of the upload request? (I believe that this is completed when we execute the existing location 4) > what is the focus? Can we say that the response download is completed? (I have questions about points 5 and 8) > when we exit point 5, it means that the response is complete, downloaded or just started downloading? > To which point does the network (bandwidth) affect performance? (5, 6, 7...) > now I'm adjusting the InputStream read code If you have any suggestions, please share?

reference:

> 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

Solution

>When establishing a connection, the request can be buffered in the system buffer, so the sending of the request – "Upload" – cannot be guaranteed until you pass through position 6 At this point, you know that the get request has been received by the server because it is now sending a response Passing location 4 only means that your program has passed the request to your own system buffer. > Response received – "download" completed – after the last time to bit 6, when it returns - 1 to indicate the end of the stream. > When you exit position 5, you may or may not receive a reply All you do is get explicit access to the existing input stream The response has been fully received and buffered in the system TCP buffer, and no response may have been received. > You must open the network resource for the last iteration from location 4 to location 6 In the code, you will continue to keep the connection open indefinitely After looping out of position 6 / 7, you can save network resources by closing the connection. > (a) Don't disturb rqbytearrayoutputstream Instead, the output stream is wrapped from the connection in the outputstreamwriter and the request string is written directly to the output stream This saves a few lines of code and removes an iteration of the copy request string. > (b) Close() is called when the output stream and input stream are completed to allow network resources to be released

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>