openPrefetchingReadChannel无法在Google云端存储客户端API中使用

时间:2014-09-06 21:30:52

标签: java google-app-engine google-cloud-storage

我正在尝试使用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();
    }
  }

参考:https://code.google.com/p/appengine-gcs-client/source/browse/trunk/java/example/src/com/google/appengine/demos/GcsExampleServlet.java

上面的代码应该从上传的对象传递1KB数据,但它返回对象的整个数据,即8.4KB。请看截图:

Check Network tab of Inspect element

我不确定发生了什么。需要你帮助的人

1 个答案:

答案 0 :(得分:2)

openPrefetchingReadChannel的第三个参数不是要读取(或限制)的最大大小。 是预取的内部缓冲区大小。在您的情况下,您可能想跟踪多少 你阅读并继续写作,直到达到预期的限度