Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default chunk length of Android HttpURLConnection.setChunkedStreamingMode()?

In the documentation of HttpURLConnection.setChunkedStreamingMode(), it said if I specify 0 in the parameter, it will use default chunk length, such as:

conn.setChunkedStreamingMode(0);

What is the exact value of the default chunk length? and what is the unit of the parameter? in bytes?

like image 313
Raptor Avatar asked Dec 19 '22 09:12

Raptor


1 Answers

Your question made me curious so I tested some stuff.

What is the exact value of the default chunk length?

I found here that chunkLength is a protected variable of the HttpURLConnection class, meaning it's only accessible inside the class itself or in a subclass. So I made a subclass of HttpURLConnection and tried to print out the chunkLength

class HttpTest extends HttpURLConnection {

    protected HttpTest(URL url) {
        super(url);
        Log.d("CHUNKLENGTH", String.format("%d", this.chunkLength));
        this.setChunkedStreamingMode(0);
        Log.d("CHUNKLENGTH", String.format("%d", this.chunkLength));
    }

    @Override
    public void disconnect() {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean usingProxy() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void connect() throws IOException {
        // TODO Auto-generated method stub

    }
}

Calling it like this

try {
    HttpTest test = new HttpTest(new URL("http://www.google.com/"));
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

These were the results

enter image description here

From that, we can conclude that the default value it uses is 1024


what is the unit of the parameter?

At the link you posted, it is mentioned that

A small chunk length increases the number of bytes that must be transmitted because of the header on every chunk.

I think it's safe to assume you have to give the number of bytes. The default 1024 also fits that criteria

like image 53
Tim Avatar answered Apr 07 '23 05:04

Tim