public class GoogleCloudStorageReadChannel
extends java.lang.Object
implements java.nio.channels.SeekableByteChannel
| Modifier and Type | Field and Description |
|---|---|
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 |
static double |
DEFAULT_BACKOFF_RANDOMIZATION_FACTOR |
| Modifier | Constructor and Description |
|---|---|
protected |
GoogleCloudStorageReadChannel()
Constructs an instance of GoogleCloudStorageReadChannel.
|
protected |
GoogleCloudStorageReadChannel(GoogleCloudStorageReadOptions readOptions)
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)
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 |
closeReadChannel()
Closes the underlying
ReadableByteChannel. |
protected com.google.api.services.storage.Storage.Objects.Get |
createRequest() |
protected static com.google.cloud.hadoop.gcsio.GoogleCloudStorageReadChannel.FileEncoding |
getEncoding(com.google.api.services.storage.model.StorageObject metadata)
Returns the FileEncoding of a file given its metadata.
|
protected com.google.api.services.storage.model.StorageObject |
getMetadata()
Retrieve the object's metadata from GCS.
|
boolean |
isOpen()
Tells whether this channel is open.
|
protected java.io.InputStream |
openStreamAndSetMetadata(long newPosition)
Opens the underlying stream, sets its position to the given value and sets size based on
stream content size.
|
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 readChannel
whenever an exception occurs while reading from it.
|
protected void |
setSize(com.google.api.client.http.HttpResponse response,
long offset)
Set the size of the content.
|
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 newPosition)
Validates that the given position is valid for this channel.
|
int |
write(java.nio.ByteBuffer src) |
public static final int DEFAULT_BACKOFF_INITIAL_INTERVAL_MILLIS
public static final double DEFAULT_BACKOFF_RANDOMIZATION_FACTOR
public static final double DEFAULT_BACKOFF_MULTIPLIER
public static final int DEFAULT_BACKOFF_MAX_INTERVAL_MILLIS
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.FileNotFoundException - if the given object does not existjava.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,
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.java.io.FileNotFoundException - if the given object does not existjava.io.IOException - on IO errorprotected GoogleCloudStorageReadChannel()
throws java.io.IOException
java.io.IOException - on IO errorprotected GoogleCloudStorageReadChannel(GoogleCloudStorageReadOptions readOptions) throws java.io.IOException
java.io.IOException - on IO errorpublic 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 closeReadChannel()
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
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
size in interface java.nio.channels.SeekableByteChanneljava.io.IOException - on IO errorprotected void setSize(long size)
protected void validatePosition(long newPosition)
throws java.io.IOException
java.io.IOExceptionprotected com.google.api.services.storage.model.StorageObject getMetadata()
throws java.io.IOException
java.io.IOException - on IO error.protected static com.google.cloud.hadoop.gcsio.GoogleCloudStorageReadChannel.FileEncoding getEncoding(com.google.api.services.storage.model.StorageObject metadata)
metadata - the object's metadata.protected void setSize(com.google.api.client.http.HttpResponse response,
long offset)
throws java.io.IOException
First, we examine the Content-Length header. If neither exists, we then look for and parse the Content-Range header. If there is no way to determine the content length, an exception is thrown. If the Content-Length header is present, then the offset is added to this value (i.e., offset is the number of bytes that were not requested).
response - response to parse.offset - the number of bytes that were not requested.java.io.IOException - on IO error.protected java.io.InputStream openStreamAndSetMetadata(long newPosition)
throws java.io.IOException
If the file encoding in GCS is gzip (and therefore the HTTP client will attempt to 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.
newPosition - position to seek into the new stream.java.io.IOException - on IO errorprotected com.google.api.services.storage.Storage.Objects.Get createRequest()
throws java.io.IOException
java.io.IOExceptionCopyright © 2018. All rights reserved.