Skip to content

Commit c7622ff

Browse files
author
Dhriti Chopra
committed
Adding full object checksum for grpc flow
1 parent 679cddd commit c7622ff

2 files changed

Lines changed: 443 additions & 1 deletion

File tree

google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.cloud.storage.Hasher.UncheckedChecksumMismatchException;
3434
import com.google.cloud.storage.ResponseContentLifecycleHandle.ChildRef;
3535
import com.google.cloud.storage.Retrying.Retrier;
36+
import java.util.OptionalLong;
3637
import com.google.cloud.storage.UnbufferedReadableByteChannelSession.UnbufferedReadableByteChannel;
3738
import com.google.common.base.Suppliers;
3839
import com.google.protobuf.ByteString;
@@ -91,7 +92,9 @@ final class GapicUnbufferedReadableByteChannel
9192
this.result = result;
9293
this.read = read;
9394
this.req = req;
94-
this.hasher = hasher;
95+
this.hasher = (req.getReadOffset() == 0)
96+
? new CumulativeHasher(hasher, 0, req.getReadLimit() <= 0 ? OptionalLong.empty() : OptionalLong.of(req.getReadLimit()))
97+
: hasher;
9598
this.fetchOffset = new AtomicLong(req.getReadOffset());
9699
this.blobOffset = req.getReadOffset();
97100
this.retrier = retrier;
@@ -174,6 +177,7 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
174177
}
175178
if (take == EOF_MARKER) {
176179
complete = true;
180+
validateCumulativeChecksum();
177181
break;
178182
}
179183

@@ -311,6 +315,18 @@ private IOException createError(String message) throws IOException {
311315
return new IOException(message, cause);
312316
}
313317

318+
private void validateCumulativeChecksum() throws IOException {
319+
if (hasher instanceof CumulativeHasher) {
320+
CumulativeHasher cumulativeHasher = (CumulativeHasher) hasher;
321+
try {
322+
cumulativeHasher.validateCumulativeChecksum(metadata);
323+
} catch (UncheckedCumulativeChecksumMismatchException exception) {
324+
throw new IOException(StorageException.coalesce(exception));
325+
}
326+
}
327+
}
328+
329+
314330
private final class ReadObjectObserver extends StateCheckingResponseObserver<ReadObjectResponse> {
315331

316332
private final SettableApiFuture<Void> open = SettableApiFuture.create();

0 commit comments

Comments
 (0)