openPrefetchingReadChannel无法在Google云端存储客户端API中工作

问题描述:

我正尝试使用openPrefetchingReadChannelGCSInputChannel从存储桶中获取对象。由于Google developer tutorial saysopenPrefetchingReadChannel无法在Google云端存储客户端API中工作

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

我不知道发生了什么。需要你的帮助的人

openPrefetchingReadChannel的第三个参数不是读取(或限制)的最大大小。 是预取的内部缓冲区大小。在你的情况下,你可能想跟踪你读了多少,并保持书写,直到达到期望的限制