Hadi
Hadi

Reputation: 574

What does `setBufferedSize()` method do?

I don't know what setBufferSize() method does. can anyone explain me what it does? What happens if i change setBufferSize(4 * 1024) to setBufferSize(5)? i don't see any change when i do that!!. can anyone explain? Thanks.

public class Test {

    public static void main(String[] args) {
        try {
            String url = "DOWNLOAD_LINK";
            HttpGet request = new HttpGet(url);
            ConnectionConfig connectionConfig = ConnectionConfig.custom()
                    .setBufferSize(4 * 1024).build();

            HttpClientBuilder builder = HttpClientBuilder.create();
            builder.setDefaultConnectionConfig(connectionConfig);

            HttpClient client = builder.build();

            HttpResponse response = client.execute(request);

            HttpEntity entity = response.getEntity();

            InputStream inputStream = entity.getContent();
            int len = 0;
            byte[] buffer = new byte[4098];
            while ((len = inputStream.read(buffer)) != -1) {
                System.out.println("len: " + len);
                //write into file , etc.
            }
            inputStream.close();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

Views: 898

Answers (1)

Alexandre Beaudet
Alexandre Beaudet

Reputation: 2834

Here is a quote from the documentation of SetBufferedSize, fairly complete :

Sets the preferred buffer size for the body of the response. The servlet container will use a buffer at least as large as the size requested. The actual buffer size used can be found using getBufferSize.

A larger buffer allows more content to be written before anything is actually sent, thus providing the servlet with more time to set appropriate status codes and headers. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.

Source

Apache's documentation

Upvotes: 2

Related Questions