openPrefetchingReadChannel is not working in Google Cloud Storage Client API

徘徊边缘 提交于 2020-01-05 12:49:07

问题


I am trying to fetch object from bucket using openPrefetchingReadChannel GCSInputChannel. As 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.

Well this is not happening for me. Please have a look at my snippet:

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

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

Above code should deliver 1KB of data from uploaded object but it is returning the whole data of object i.e. 8.4KB. Please look at the screenshot:

I am not sure what is happening. Need your help guys


回答1:


The third argument for openPrefetchingReadChannel is not the max size to read (or limit). Is the the internal buffer size for prefetching. In your case you may want to track how much you read and keep writing until reached the desired limit



来源:https://stackoverflow.com/questions/25704937/openprefetchingreadchannel-is-not-working-in-google-cloud-storage-client-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!