openPrefetchingReadChannel在Google云端存储客户端API中不起作用 [英] openPrefetchingReadChannel is not working in Google Cloud Storage Client API

查看:101
本文介绍了openPrefetchingReadChannel在Google云端存储客户端API中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 openPrefetchingReadChannel GCSInputChannel 。作为 Google开发者教程说::

  GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel fileName,0,1024 * 1024); 

预取提供对于大多数应用程序来说是一个主要的性能优势,因为
允许处理文件的一部分,同时更多数据被并行地下载到后台
中。第三个参数是预取缓冲区的大小,在示例
中设置为1兆字节。

好吧,这不会发生在我身上。请看我的代码片段:

  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 。请看截图:





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

解决方案

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


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

解决方案

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

这篇关于openPrefetchingReadChannel在Google云端存储客户端API中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆