Skip to content

storage: read blob in chunk will get exception when reach the end the blob #723

Description

@sudison

Following code is used to read blob chunk by chunk:

try (ReadChannel reader = blob.reader()) {
  WritableByteChannel channel = Channels.newChannel(outputStream);
  ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
  while (reader.read(bytes) > 0) {
    bytes.flip();
    channel.write(bytes);
    bytes.clear();
  }
}

reader.read() will throw exception:

com.google.gcloud.storage.StorageException: java.lang.NullPointerException

at com.google.gcloud.storage.StorageException.translateAndThrow(StorageException.java:74)
at com.google.gcloud.storage.BlobReadChannel.read(BlobReadChannel.java:159)

The root cause is, reader is blindly reading chunks from cloud storage, if it reaches beyond the end of the blob, then cloud storage will throw an error: 416 Requested range not satisfiable.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions