public final class SeekableByteChannelPrefetcher implements SeekableByteChannel
SeekableByteChannelPrefetcher wraps an existing SeekableByteChannel to add prefetching. The prefetching is done on a different thread, so you can write simple code that repeatedly calls read() to get data, processes it, and then calls read again -- and yet this simple code overlaps computation and communication for you. (Of course this is only worthwhile if the underlying SeekableByteChannel doesn't already implement prefetching).
Implements
SeekableByteChannelStatic Methods
addPrefetcher(int bufferSizeMB, SeekableByteChannel channel)
public static SeekableByteChannel addPrefetcher(int bufferSizeMB, SeekableByteChannel channel)
Wraps the provided SeekableByteChannel within a SeekableByteChannelPrefetcher, using the provided buffer size
Name | Description |
bufferSizeMB | int buffer size in MB |
channel | SeekableByteChannel channel to wrap in the prefetcher |
Type | Description |
SeekableByteChannel | wrapped channel |
Type | Description |
IOException |
Methods
close()
public void close()
Closes this channel.
After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.
If this channel is already closed then invoking this method has no effect.
This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.
Type | Description |
IOException | If an I/O error occurs |
fetch(long position)
public ByteBuffer fetch(long position)
Name | Description |
position | long |
Type | Description |
ByteBuffer |
Type | Description |
InterruptedException | |
ExecutionException |
getStatistics()
public SeekableByteChannelPrefetcher.Statistics getStatistics()
Type | Description |
SeekableByteChannelPrefetcher.Statistics |
isOpen()
public boolean isOpen()
Tells whether or not this channel is open.
Type | Description |
boolean | true if, and only if, this channel is open |
position()
public long position()
Returns this channel's position.
Type | Description |
long | This channel's position, a non-negative integer counting the number of bytes from the beginning of the entity to the current position |
Type | Description |
IOException | If this channel is closed |
position(long newPosition)
public SeekableByteChannel position(long newPosition)
Sets this channel's position.
Setting the position to a value that is greater than the current size is legal but does not change the size of the entity. A later attempt to read bytes at such a position will immediately return an end-of-file indication. A later attempt to write bytes at such a position will cause the entity to grow to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.
Setting the channel's position is not recommended when connected to an entity, typically a file, that is opened with the APPEND option. When opened for append, the position is first advanced to the end before writing.
Name | Description |
newPosition | long The new position, a non-negative integer counting the number of bytes from the beginning of the entity |
Type | Description |
SeekableByteChannel | This channel |
Type | Description |
IOException | If this channel is closed |
read(ByteBuffer dst)
public synchronized int read(ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer.
Bytes are read starting at this channel's current position, and then the position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the java.nio.channels.ReadableByteChannel interface.
Name | Description |
dst | ByteBuffer buffer to write into |
Type | Description |
int |
Type | Description |
IOException |
size()
public long size()
Returns the current size of entity to which this channel is connected.
Type | Description |
long | The current size, measured in bytes |
Type | Description |
IOException | If this channel is closed |
truncate(long size)
public SeekableByteChannel truncate(long size)
Not supported.
Name | Description |
size | long |
Type | Description |
SeekableByteChannel |
Type | Description |
IOException |
write(ByteBuffer src)
public int write(ByteBuffer src)
Writing isn't supported.
Name | Description |
src | ByteBuffer |
Type | Description |
int |
Type | Description |
IOException |