gpt4 book ai didi

java - `setBufferedSize()`方法有什么作用?

转载 作者:行者123 更新时间:2023-12-02 02:09:58 24 4
gpt4 key购买 nike

我不知道setBufferSize()方法的作用。谁能解释一下它的作用吗?如果我将 setBufferSize(4 * 1024) 更改为 setBufferSize(5) 会发生什么?当我这样做时,我没有看到任何变化!谁能解释一下吗?谢谢。

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();
}
}

最佳答案

这里引用了 SetBufferedSize 的文档,相当完整:

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

关于java - `setBufferedSize()`方法有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50158975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com