2014-09-06 198 views
1

我正嘗試使用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

我不知道發生了什麼。需要你的幫助的人

回答

2

openPrefetchingReadChannel的第三個參數不是讀取(或限制)的最大大小。 是預取的內部緩衝區大小。在你的情況下,你可能想跟蹤你讀了多少,並保持書寫,直到達到期望的限制

相關問題