public class GoogleCloudStorageReadChannel extends Object implements SeekableByteChannel
| Modifier and Type | Field and Description |
|---|---|
protected long |
contentChannelPosition |
protected long |
currentPosition |
protected boolean |
metadataInitialized |
| Constructor and Description |
|---|
GoogleCloudStorageReadChannel(com.google.api.services.storage.Storage gcs,
StorageResourceId resourceId,
ApiErrorExtractor errorExtractor,
ClientRequestHelper<com.google.api.services.storage.model.StorageObject> requestHelper,
GoogleCloudStorageReadOptions readOptions)
Constructs an instance of GoogleCloudStorageReadChannel.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this channel.
|
protected void |
closeContentChannel()
Closes the underlying
ReadableByteChannel. |
protected com.google.api.services.storage.Storage.Objects.Get |
createDataRequest() |
protected com.google.api.services.storage.Storage.Objects.Get |
createMetadataRequest() |
protected GoogleCloudStorageItemInfo |
getInitialMetadata()
Returns
GoogleCloudStorageItemInfo used to initialize metadata in constructor or null if GoogleCloudStorageReadOptions.getFastFailOnNotFound() is set to false. |
protected void |
initMetadata(com.google.api.client.http.HttpHeaders headers)
Initializes metadata (size, encoding, etc) from HTTP
headers. |
protected void |
initMetadata(String encoding,
long sizeFromMetadata,
long generation)
Initializes metadata (size, encoding, etc) from passed parameters.
|
boolean |
isOpen()
Tells whether this channel is open.
|
protected InputStream |
openStream(long bytesToRead)
Opens the underlying stream, sets its position to the
currentPosition. |
long |
position()
Returns this channel's current position.
|
SeekableByteChannel |
position(long newPosition)
Sets this channel's position.
|
int |
read(ByteBuffer buffer)
Reads from this channel and stores read data in the given buffer.
|
void |
setMaxRetries(int maxRetries)
Sets the number of times to automatically retry by re-opening the underlying contentChannel
whenever an exception occurs while reading from it.
|
protected void |
setSize(long size)
Sets size of this channel to the given value.
|
long |
size()
Returns size of the object to which this channel is connected.
|
SeekableByteChannel |
truncate(long size) |
protected void |
validatePosition(long position)
Validates that the given position is valid for this channel.
|
int |
write(ByteBuffer src) |
protected long currentPosition
protected long contentChannelPosition
protected boolean metadataInitialized
public GoogleCloudStorageReadChannel(com.google.api.services.storage.Storage gcs,
StorageResourceId resourceId,
ApiErrorExtractor errorExtractor,
ClientRequestHelper<com.google.api.services.storage.model.StorageObject> requestHelper,
@Nonnull
GoogleCloudStorageReadOptions readOptions)
throws IOException
gcs - storage object instanceresourceId - contains information about a specific resourcerequestHelper - a ClientRequestHelper used to set any extra headersreadOptions - fine-grained options specifying things like retry settings, buffering, etc.
Could not be null.IOException - on IO error@Nullable protected GoogleCloudStorageItemInfo getInitialMetadata() throws IOException
GoogleCloudStorageItemInfo used to initialize metadata in constructor or null if GoogleCloudStorageReadOptions.getFastFailOnNotFound() is set to false.IOExceptionpublic void setMaxRetries(int maxRetries)
public int read(ByteBuffer buffer) throws IOException
On unexpected failure, will attempt to close the channel and clean up state.
read in interface ReadableByteChannelread in interface SeekableByteChannelbuffer - buffer to read data intoIOException - on IO errorpublic SeekableByteChannel truncate(long size) throws IOException
truncate in interface SeekableByteChannelIOExceptionpublic int write(ByteBuffer src) throws IOException
write in interface SeekableByteChannelwrite in interface WritableByteChannelIOExceptionpublic boolean isOpen()
protected void closeContentChannel()
ReadableByteChannel.
Catches and ignores all exceptions as there is not a lot the user can do to fix errors here
and a new connection will be needed. Especially SSLExceptions since the there's a high
probability that SSL connections would be broken in a way that causes Channel.close() itself to throw an exception, even though underlying sockets
have already been cleaned up; close() on an SSLSocketImpl requires a shutdown handshake in
order to shutdown cleanly, and if the connection has been broken already, then this is not
possible, and the SSLSocketImpl was already responsible for performing local cleanup at the
time the exception was raised.
public void close()
public long position()
throws IOException
position in interface SeekableByteChannelIOExceptionpublic SeekableByteChannel position(long newPosition) throws IOException
This method will throw an exception if newPosition is greater than object size,
which contradicts SeekableByteChannel contract.
TODO(user): decide if this needs to be fixed.
position in interface SeekableByteChannelnewPosition - the new position, counting the number of bytes from the beginning.FileNotFoundException - if the underlying object does not exist.IOException - on IO errorpublic long size()
throws IOException
Note: this method will return -1 until metadata will be lazily initialized during first
read(java.nio.ByteBuffer) method call.
size in interface SeekableByteChannel-1 otherwise.IOException - on IO errorprotected void setSize(long size)
protected void validatePosition(long position)
throws IOException
IOExceptionprotected void initMetadata(com.google.api.client.http.HttpHeaders headers)
throws IOException
headers. Used for lazy
initialization when fail fast is disabled.IOExceptionprotected void initMetadata(@Nullable String encoding, long sizeFromMetadata, long generation) throws IOException
IOExceptionprotected InputStream openStream(long bytesToRead) throws IOException
currentPosition.
If the file encoding in GCS is gzip (and therefore the HTTP client will decompress it), the entire file is always requested and we seek to the position requested. If the file encoding is not gzip, only the remaining bytes to be read are requested from GCS.
bytesToRead - number of bytes to read from new stream. Ignored if GoogleCloudStorageReadOptions.getFadvise() is equal to GoogleCloudStorageReadOptions.Fadvise.SEQUENTIAL.IOException - on IO errorprotected com.google.api.services.storage.Storage.Objects.Get createDataRequest()
throws IOException
IOExceptionprotected com.google.api.services.storage.Storage.Objects.Get createMetadataRequest()
throws IOException
IOExceptionCopyright © 2022. All rights reserved.