FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: update 416 & OUT_OF_RANGE handling for ReadChannel by BenWhitehead · Pull Request #3018 · googleapis/java-storage · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ private ScatteringByteChannel open() {
if (statusCode == 404) {
throw new StorageException(404, "Failure while trying to resume download", e);
}
} else if (e.getStatusCode() == 416) {
returnEOF = true;
}
throw StorageException.translate(e);
} catch (IOException e) {
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.api.gax.retrying.BasicResultRetryAlgorithm;
import com.google.api.gax.retrying.ResultRetryAlgorithm;
import com.google.api.gax.rpc.ApiExceptions;
import com.google.api.gax.rpc.OutOfRangeException;
import com.google.api.gax.rpc.StateCheckingResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.api.gax.rpc.WatchdogTimeoutException;
Expand Down Expand Up @@ -365,6 +366,15 @@ protected void onResponseImpl(ReadObjectResponse response) {

@Override
protected void onErrorImpl(Throwable t) {
if (t instanceof OutOfRangeException) {
try {
queue.offer(EOF_MARKER);
open.set(null);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Code.ABORTED.toStatus().withCause(e).asRuntimeException();
}
}
if (t instanceof CancellationException) {
cancellation.set(t);
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.google.cloud.storage.it.runner.registry.Generator;
import com.google.cloud.storage.it.runner.registry.ObjectsFixture;
import com.google.cloud.storage.it.runner.registry.ObjectsFixture.ObjectAndContent;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.BaseEncoding;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -528,6 +529,36 @@ public void responseWith416ReturnsZeroAndLeavesTheChannelOpen() throws IOExcepti
}
}

@Test
public void responseWith416AttemptingToReadStartingPastTheEndOfTheObjectIsTerminallyEOF()
throws IOException {
int length = 10;
byte[] bytes = DataGenerator.base64Characters().genBytes(length);

BlobInfo info1 =
BlobInfo.newBuilder(bucket, generator.randomObjectName())
.setMetadata(ImmutableMap.of("gen", "1"))
.build();
Blob gen1 = storage.create(info1, bytes, BlobTargetOption.doesNotExist());

try (ReadChannel reader = storage.reader(gen1.getBlobId())) {
reader.seek(length + 1);
ByteBuffer buf = ByteBuffer.allocate(1);
assertThat(reader.read(buf)).isEqualTo(-1);
assertThat(reader.read(buf)).isEqualTo(-1);

BlobInfo update = gen1.toBuilder().setMetadata(ImmutableMap.of("gen", "2")).build();
BlobInfo gen2 =
storage.create(
update,
DataGenerator.base64Characters().genBytes(length + 2),
BlobTargetOption.generationMatch());

assertThat(reader.read(buf)).isEqualTo(-1);
assertThat(reader.read(buf)).isEqualTo(-1);
}
}

/** Read channel does not consider itself closed once it returns {@code -1} from read. */
@Test
public void readChannelIsAlwaysOpen_willReturnNegative1UntilExplicitlyClosed() throws Exception {
Expand Down

Back | FazBrowse Home | New Git URL