我正在尝试使用openPrefetchingReadChannel GCSInputChannel
从存储桶中获取对象。作为Google developer tutorial says
:
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, 1024 * 1024);
Prefetching provides is a major performance advantage for most applications, because it
allows processing part of the file while more data is being downloaded in the background
in parallel.The third parameter is the size of the prefetch buffer, set in the example
to 1 megabyte.
嗯,这不适合我。请看一下我的代码:
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, 1024);
copy(Channels.newInputStream(readChannel), resp.getOutputStream());
private void copy(InputStream input, OutputStream output) throws IOException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}
} finally {
input.close();
output.close();
}
}
上面的代码应该从上传的对象传递1KB
数据,但它返回对象的整个数据,即8.4KB
。请看截图:
我不确定发生了什么。需要你帮助的人
答案 0 :(得分:2)
openPrefetchingReadChannel的第三个参数不是要读取(或限制)的最大大小。 是预取的内部缓冲区大小。在您的情况下,您可能想跟踪多少 你阅读并继续写作,直到达到预期的限度