File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
google-cloud-storage/src/main/java/com/google/cloud/storage/transfermanager Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 2020import com .google .cloud .storage .BlobInfo ;
2121import com .google .cloud .storage .Storage ;
2222import com .google .cloud .storage .Storage .BlobSourceOption ;
23+ import com .google .cloud .storage .StorageException ;
2324import com .google .common .io .ByteStreams ;
2425import java .nio .channels .FileChannel ;
2526import java .nio .file .Path ;
@@ -64,6 +65,20 @@ public DownloadSegment call() {
6465 rc .limit (endPosition );
6566 wc .position (startPosition );
6667 bytesCopied = ByteStreams .copy (rc , wc );
68+ long bytesExpected = endPosition - startPosition ;
69+ if (bytesCopied != bytesExpected ) {
70+ return DownloadSegment .newBuilder (originalBlob , TransferStatus .FAILED_TO_FINISH )
71+ .setException (
72+ new StorageException (
73+ 0 ,
74+ "Unexpected end of stream, read "
75+ + bytesCopied
76+ + " expected "
77+ + bytesExpected
78+ + " from object "
79+ + originalBlob .getBlobId ().toGsUtilUriWithGeneration ()))
80+ .build ();
81+ }
6782 } catch (Exception e ) {
6883 if (bytesCopied == -1 ) {
6984 return DownloadSegment .newBuilder (originalBlob , TransferStatus .FAILED_TO_START )
Original file line number Diff line number Diff line change 2121import com .google .cloud .storage .BlobInfo ;
2222import com .google .cloud .storage .Storage ;
2323import com .google .cloud .storage .Storage .BlobSourceOption ;
24+ import com .google .cloud .storage .StorageException ;
2425import com .google .common .io .ByteStreams ;
2526import java .nio .channels .FileChannel ;
2627import java .nio .file .Path ;
@@ -60,6 +61,21 @@ public DownloadResult call() {
6061 StandardOpenOption .CREATE ,
6162 StandardOpenOption .TRUNCATE_EXISTING );
6263 bytesCopied = ByteStreams .copy (rc , wc );
64+ if (originalBlob .getSize () != null ) {
65+ if (bytesCopied != originalBlob .getSize ()) {
66+ return DownloadResult .newBuilder (originalBlob , TransferStatus .FAILED_TO_FINISH )
67+ .setException (
68+ new StorageException (
69+ 0 ,
70+ "Unexpected end of stream, read "
71+ + bytesCopied
72+ + " expected "
73+ + originalBlob .getSize ()
74+ + " from object "
75+ + originalBlob .getBlobId ().toGsUtilUriWithGeneration ()))
76+ .build ();
77+ }
78+ }
6379 } catch (Exception e ) {
6480 if (bytesCopied == -1 ) {
6581 return DownloadResult .newBuilder (originalBlob , TransferStatus .FAILED_TO_START )
You can’t perform that action at this time.
0 commit comments