`setBufferedSize()` 方法有什么作用?
What does `setBufferedSize()` method do?
我不知道 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.
我不知道 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.