public interface DataSource
This abstraction serves three purposes:
byte[]
,
ByteBuffer
, RandomAccessFile
, memory-mapped file.ByteBuffer
may have worked as the unifying abstraction.There are following ways to obtain a chunk of data from the data source:
DataSink
using
feed
. This is best suited for scenarios where there is no
need to have the chunk's data accessible at the same time, for example, when computing the
digest of the chunk. If you need to keep the chunk's data around after feed
completes, you must create a copy during feed
. However, in that case the following
methods of obtaining the chunk's data may be more appropriate.ByteBuffer
containing the chunk's data using
getByteBuffer
. Depending on the data source, the chunk's
data may or may not be copied by this operation. This is best suited for scenarios where
you need to access the chunk's data in arbitrary order, but don't need to modify the data and
thus don't require a copy of the data.ByteBuffer
using
copyTo
. This is best suited for scenarios where
you require a copy of the chunk's data, such as to when you need to modify the data.
Modifier and Type | Method and Description |
---|---|
void |
copyTo(long offset,
int size,
ByteBuffer dest)
Copies the specified chunk from this data source into the provided destination buffer,
advancing the destination buffer's position by
size . |
void |
feed(long offset,
long size,
DataSink sink)
Feeds the specified chunk from this data source into the provided sink.
|
ByteBuffer |
getByteBuffer(long offset,
int size)
Returns a buffer holding the contents of the specified chunk of data from this data source.
|
long |
size()
Returns the amount of data (in bytes) contained in this data source.
|
DataSource |
slice(long offset,
long size)
Returns a data source representing the specified region of data of this data source.
|
long size()
void feed(long offset, long size, DataSink sink) throws IOException
offset
- index (in bytes) at which the chunk starts inside data sourcesize
- size (in bytes) of the chunkIOException
ByteBuffer getByteBuffer(long offset, int size) throws IOException
The returned buffer's position is 0
, and the buffer's limit and capacity is
size
.
offset
- index (in bytes) at which the chunk starts inside data sourcesize
- size (in bytes) of the chunkIOException
void copyTo(long offset, int size, ByteBuffer dest) throws IOException
size
.offset
- index (in bytes) at which the chunk starts inside data sourcesize
- size (in bytes) of the chunkIOException
DataSource slice(long offset, long size)
offset
- index (in bytes) at which the region starts inside data sourcesize
- size (in bytes) of the regionCopyright © 2016. All rights reserved.