Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of HttpUrlConnection.setChunkedStreamingMode

I don't get a good understanding about HttpUrlConnection.setChunkedStreamingMode, what is the effect of this mode?

I have following sample code:

HttpURLConnection conn = getHttpURLConnection(_url);
conn.setChunkedStreamingMode(4096); //4k
conn.setConnectTimeout(3000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
byte[] buffer = new byte[1024 * 10];//10k
FileInputStream in= new FileInputStream(file); //Write the content of the file to the server
int len;
while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
}

out.flush();
in.close();

Say, The file size is 101k, and I set the chunk size to be 4096.

  1. The HttpUrlConnection will send 4096 bytes to the server every write? and 1k for the last time?

  2. Note that I have used a 10k buffer to write to the outputstream, Does it matter that the chunk size and buffer size are not the same?

  3. If I disable the ChunkedStreamMode in my code, what's the effect compared to the code that I have set 4096?

like image 358
Tom Avatar asked Sep 16 '17 02:09

Tom


People also ask

Do I need to disconnect HttpURLConnection?

Yes you need to close the inputstream first and close httpconnection next. As per javadoc.

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.

What is the use of HttpURLConnection in Java?

HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol.

How do I read HttpURLConnection response?

The getResponseMessage is a method of Java HttpURLConnection class. This method is used to get response code from HTTP response message. For example, if the response code from a server is HTTP/1.0 200 OK or HTTP/1.0 404 Not Found, it extracts the string OK and Not Found else returns null if HTTP is not valid.


1 Answers

  1. The HttpUrlConnection will send 4096 bytes to the server every write? and 1k for the last time?

Yes.

  1. Note that I have used a 10k buffer to write to the outputstream, Does it matter that the chunk size and buffer size are not the same?

No.

  1. If I disable the ChunkedStreamMode in my code, what's the effect compared to the code that I have set 4096?

The effect is that the entire output is buffered until you close, so that the Content-length header can be set and sent first, which adds a lot of latency and memory. Not recommended in the case of large files.

like image 172
user207421 Avatar answered Oct 05 '22 11:10

user207421