public class GoogleCloudStorageReadChannel
extends java.lang.Object
implements java.nio.channels.SeekableByteChannel
| Modifier and Type | Field and Description |
|---|---|
protected long |
contentChannelPosition |
protected long |
currentPosition |
static int |
DEFAULT_BACKOFF_INITIAL_INTERVAL_MILLIS
|
static int |
DEFAULT_BACKOFF_MAX_ELAPSED_TIME_MILLIS
|
static int |
DEFAULT_BACKOFF_MAX_INTERVAL_MILLIS
|
static double |
DEFAULT_BACKOFF_MULTIPLIER
Deprecated.
|
static double |
DEFAULT_BACKOFF_RANDOMIZATION_FACTOR
|
protected boolean |
metadataInitialized |
| Modifier | Constructor and Description |
|---|---|
protected |
GoogleCloudStorageReadChannel(GoogleCloudStorageReadOptions readOptions)
Used for unit testing only.
|
|
GoogleCloudStorageReadChannel(com.google.api.services.storage.Storage gcs,
java.lang.String bucketName,
java.lang.String objectName,
ApiErrorExtractor errorExtractor,
ClientRequestHelper<com.google.api.services.storage.model.StorageObject> requestHelper)
Constructs an instance of GoogleCloudStorageReadChannel.
|
|
GoogleCloudStorageReadChannel(com.google.api.services.storage.Storage gcs,
java.lang.String bucketName,
java.lang.String objectName,
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 |
createRequest() |
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(java.lang.String encoding,
long sizeFromMetadata,
java.lang.String generation)
Initializes metadata (size, encoding, etc) from passed parameters.
|
boolean |
isOpen()
Tells whether this channel is open.
|
protected java.io.InputStream |
openStream(long bytesToRead)
Opens the underlying stream, sets its position to the
currentPosition. |
long |
position()
Returns this channel's current position.
|
java.nio.channels.SeekableByteChannel |
position(long newPosition)
Sets this channel's position.
|
int |
read(java.nio.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.
|
java.nio.channels.SeekableByteChannel |
truncate(long size) |
protected void |
validatePosition(long position)
Validates that the given position is valid for this channel.
|
int |
write(java.nio.ByteBuffer src) |
protected long currentPosition
protected long contentChannelPosition
protected boolean metadataInitialized
@Deprecated public static final int DEFAULT_BACKOFF_INITIAL_INTERVAL_MILLIS
@Deprecated public static final double DEFAULT_BACKOFF_RANDOMIZATION_FACTOR
@Deprecated public static final double DEFAULT_BACKOFF_MULTIPLIER
GoogleCloudStorageReadOptions.DEFAULT_BACKOFF_MULTIPLIER instead@Deprecated public static final int DEFAULT_BACKOFF_MAX_INTERVAL_MILLIS
@Deprecated public static final int DEFAULT_BACKOFF_MAX_ELAPSED_TIME_MILLIS
public GoogleCloudStorageReadChannel(com.google.api.services.storage.Storage gcs,
java.lang.String bucketName,
java.lang.String objectName,
ApiErrorExtractor errorExtractor,
ClientRequestHelper<com.google.api.services.storage.model.StorageObject> requestHelper)
throws java.io.IOException
gcs - storage object instancebucketName - name of the bucket containing the object to readobjectName - name of the object to readrequestHelper - a ClientRequestHelper used to set any extra headersjava.io.IOException - on IO errorpublic GoogleCloudStorageReadChannel(com.google.api.services.storage.Storage gcs,
java.lang.String bucketName,
java.lang.String objectName,
ApiErrorExtractor errorExtractor,
ClientRequestHelper<com.google.api.services.storage.model.StorageObject> requestHelper,
@Nonnull
GoogleCloudStorageReadOptions readOptions)
throws java.io.IOException
gcs - storage object instancebucketName - name of the bucket containing the object to readobjectName - name of the object to readrequestHelper - a ClientRequestHelper used to set any extra headersreadOptions - fine-grained options specifying things like retry settings, buffering, etc.
Could not be null.java.io.IOException - on IO errorprotected GoogleCloudStorageReadChannel(@Nonnull
GoogleCloudStorageReadOptions readOptions)
throws java.io.IOException
Constructs an instance of GoogleCloudStorageReadChannel.
readOptions - fine-grained options specifying things like retry settings, buffering, etc.
Could not be null.java.io.IOException - on IO error@Nullable protected GoogleCloudStorageItemInfo getInitialMetadata() throws java.io.IOException
GoogleCloudStorageItemInfo used to initialize metadata in constructor or null if GoogleCloudStorageReadOptions.getFastFailOnNotFound() is set to false.java.io.IOExceptionpublic void setMaxRetries(int maxRetries)
public int read(java.nio.ByteBuffer buffer)
throws java.io.IOException
On unexpected failure, will attempt to close the channel and clean up state.
read in interface java.nio.channels.ReadableByteChannelread in interface java.nio.channels.SeekableByteChannelbuffer - buffer to read data intojava.io.IOException - on IO errorpublic java.nio.channels.SeekableByteChannel truncate(long size)
throws java.io.IOException
truncate in interface java.nio.channels.SeekableByteChanneljava.io.IOExceptionpublic int write(java.nio.ByteBuffer src)
throws java.io.IOException
write in interface java.nio.channels.SeekableByteChannelwrite in interface java.nio.channels.WritableByteChanneljava.io.IOExceptionpublic boolean isOpen()
isOpen in interface java.nio.channels.Channelprotected 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()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in interface java.nio.channels.Channeljava.io.IOException - on IO errorpublic long position()
throws java.io.IOException
position in interface java.nio.channels.SeekableByteChanneljava.io.IOExceptionpublic java.nio.channels.SeekableByteChannel position(long newPosition)
throws java.io.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 java.nio.channels.SeekableByteChannelnewPosition - the new position, counting the number of bytes from the beginning.java.io.FileNotFoundException - if the underlying object does not exist.java.io.IOException - on IO errorpublic long size()
throws java.io.IOException
Note: this method will return -1 until metadata will be lazily initialized during first
read(java.nio.ByteBuffer) method call.
size in interface java.nio.channels.SeekableByteChannel-1 otherwise.java.io.IOException - on IO errorprotected void setSize(long size)
protected void validatePosition(long position)
throws java.io.IOException
java.io.IOExceptionprotected void initMetadata(com.google.api.client.http.HttpHeaders headers)
throws java.io.IOException
headers.java.io.IOExceptionprotected void initMetadata(@Nullable
java.lang.String encoding,
long sizeFromMetadata,
@Nullable
java.lang.String generation)
throws java.io.IOException
java.io.IOExceptionprotected java.io.InputStream openStream(long bytesToRead)
throws java.io.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.java.io.IOException - on IO errorprotected com.google.api.services.storage.Storage.Objects.Get createRequest()
throws java.io.IOException
java.io.IOExceptionCopyright © 2019. All rights reserved.