Package org.broadinstitute.http.nio
Class HttpSeekableByteChannel
java.lang.Object
org.broadinstitute.http.nio.HttpSeekableByteChannel
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ByteChannel
,Channel
,ReadableByteChannel
,SeekableByteChannel
,WritableByteChannel
Implementation for a
SeekableByteChannel
for URL
open as a connection.
The current implementation is thread-safe using the synchronized
keyword in every
method.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate InputStream
private ReadableByteChannel
private final HttpClient
private static final org.slf4j.Logger
private long
private final RetryHandler
private long
private static final long
private final URI
-
Constructor Summary
ConstructorsConstructorDescriptioncreate a new seekable channel with default setttings at beggining of the fileHttpSeekableByteChannel
(URI uri, long position) Create a new seekable channel with default setttins and seek to the given positionHttpSeekableByteChannel
(URI uri, HttpFileSystemProviderSettings settings, long position) Create a new seekable channel which reads from the requested URI -
Method Summary
Modifier and TypeMethodDescriptionprivate void
private void
assertGoodHttpResponse
(HttpResponse<?> response, boolean isRangeRequest) Checks for the approprate http response code and throws an exception if the wrong one is found.void
close()
private void
boolean
isOpen()
private void
openChannel
(long position) long
position()
position
(long newPosition) int
read
(ByteBuffer dst) static int
Performs the equivalent of a channel.read(buf) operation but in the case of an exception the state of the input buffer is not adversely impacted.long
size()
truncate
(long size) int
write
(ByteBuffer src)
-
Field Details
-
SKIP_DISTANCE
private static final long SKIP_DISTANCE- See Also:
-
LOGGER
private static final org.slf4j.Logger LOGGER -
uri
-
retryHandler
-
client
-
channel
-
backingStream
-
position
private long position -
size
private long size
-
-
Constructor Details
-
HttpSeekableByteChannel
create a new seekable channel with default setttings at beggining of the file- Parameters:
uri
- the URI to connect to, this should not include range parameters already- Throws:
IOException
- if no connection can be established
-
HttpSeekableByteChannel
Create a new seekable channel with default setttins and seek to the given position- Parameters:
uri
- the URI to connect to, this should not include range parameters alreadyposition
- an initial byte offset to open the file at- Throws:
IOException
- if no connection can be established
-
HttpSeekableByteChannel
public HttpSeekableByteChannel(URI uri, HttpFileSystemProviderSettings settings, long position) throws IOException Create a new seekable channel which reads from the requested URI- Parameters:
uri
- the URI to connect to, this should not include range parameters alreadysettings
- settings to configure the connection and retry handlingposition
- an initial byte offset to open the file at- Throws:
IOException
- if no connection can be established
-
-
Method Details
-
read
- Specified by:
read
in interfaceReadableByteChannel
- Specified by:
read
in interfaceSeekableByteChannel
- Throws:
IOException
-
readWithoutPerturbingTheBufferIfAnErrorOccurs
public static int readWithoutPerturbingTheBufferIfAnErrorOccurs(ByteBuffer dst, ReadableByteChannel channel) throws IOException Performs the equivalent of a channel.read(buf) operation but in the case of an exception the state of the input buffer is not adversely impacted.- Parameters:
dst
- a ByteBuffer to read intochannel
- the channel to reaad from- Returns:
- the number of bytes read from the channel
- Throws:
IOException
- if the read operation throws
-
assertChannelIsOpen
- Throws:
ClosedChannelException
-
write
- Specified by:
write
in interfaceSeekableByteChannel
- Specified by:
write
in interfaceWritableByteChannel
-
position
- Specified by:
position
in interfaceSeekableByteChannel
- Throws:
IOException
-
position
- Specified by:
position
in interfaceSeekableByteChannel
- Throws:
IOException
-
size
- Specified by:
size
in interfaceSeekableByteChannel
- Throws:
IOException
-
assertGoodHttpResponse
private void assertGoodHttpResponse(HttpResponse<?> response, boolean isRangeRequest) throws FileNotFoundException, UnexpectedHttpResponseException Checks for the approprate http response code and throws an exception if the wrong one is found.- Parameters:
response
- the completed HttpResponseisRangeRequest
- if this query was expecting a subrange of the file- Throws:
FileNotFoundException
- on a 404IncompatibleResponseToRangeQueryException
- if it expected a subset of the file but got the whole thing or vice versaUnexpectedHttpResponseException
- if it reieves any other http response
-
truncate
- Specified by:
truncate
in interfaceSeekableByteChannel
-
isOpen
public boolean isOpen() -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
closeSilently
private void closeSilently() -
openChannel
- Throws:
IOException
-