public class GoogleCloudStorageReadChannel extends Object implements 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.
|
|
GoogleCloudStorageReadChannel(com.google.api.services.storage.Storage gcs,
String bucketName,
String objectName,
ApiErrorExtractor errorExtractor,
ClientRequestHelper<com.google.api.services.storage.model.StorageObject> requestHelper)
Constructs an instance of GoogleCloudStorageReadChannel.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this channel.
|
protected void |
closeReadChannel()
Closes the underlying
ReadableByteChannel. |
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 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.
|
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 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.
|
SeekableByteChannel |
truncate(long size) |
protected void |
validatePosition(long newPosition)
Validates that the given position is valid for this channel.
|
int |
write(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,
String bucketName,
String objectName,
ApiErrorExtractor errorExtractor,
ClientRequestHelper<com.google.api.services.storage.model.StorageObject> requestHelper)
throws 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 headersFileNotFoundException - if the given object does not existIOException - on IO errorprotected GoogleCloudStorageReadChannel()
throws IOException
IOException - on IO errorpublic 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 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 IOException
close in interface Closeableclose in interface AutoCloseableclose in interface ChannelIOException - on IO errorpublic long position()
throws IOException
position in interface SeekableByteChannelIOExceptionpublic SeekableByteChannel position(long newPosition) throws IOException
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
size in interface SeekableByteChannelIOException - on IO errorprotected void setSize(long size)
protected void validatePosition(long newPosition)
protected com.google.api.services.storage.model.StorageObject getMetadata()
throws IOException
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 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.IOException - on IO error.protected InputStream openStreamAndSetMetadata(long newPosition) throws 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.IOException - on IO errorCopyright © 2016. All rights reserved.