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 InputStreamprivate ReadableByteChannelprivate final HttpClientprivate static final org.slf4j.Loggerprivate longprivate final RetryHandlerprivate longprivate static final longprivate 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 voidprivate voidassertGoodHttpResponse(HttpResponse<?> response, boolean isRangeRequest) Checks for the approprate http response code and throws an exception if the wrong one is found.voidclose()private voidbooleanisOpen()private voidopenChannel(long position) longposition()position(long newPosition) intread(ByteBuffer dst) static intPerforms 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.longsize()truncate(long size) intwrite(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:
readin interfaceReadableByteChannel- Specified by:
readin 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:
writein interfaceSeekableByteChannel- Specified by:
writein interfaceWritableByteChannel
-
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
IOException
-
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
IOException
-
size
- Specified by:
sizein 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:
truncatein interfaceSeekableByteChannel
-
isOpen
public boolean isOpen() -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Throws:
IOException
-
closeSilently
private void closeSilently() -
openChannel
- Throws:
IOException
-