public abstract class BlockSource
extends java.lang.Object
implements java.lang.AutoCloseable
BlockSource
implementations must decide if they will be thread-safe,
or not.
Constructor and Description |
---|
BlockSource() |
Modifier and Type | Method and Description |
---|---|
void |
adviseSequentialRead(long startPos,
long endPos)
Advise the
BlockSource a sequential scan is starting. |
abstract void |
close() |
static BlockSource |
from(byte[] content)
Wrap a byte array as a
BlockSource . |
static BlockSource |
from(java.nio.channels.FileChannel ch)
Read from a
FileChannel . |
static BlockSource |
from(java.io.FileInputStream in)
Read from a
FileInputStream . |
abstract java.nio.ByteBuffer |
read(long position,
int blockSize)
Read a block from the file.
|
abstract long |
size()
Determine the size of the file.
|
public static BlockSource from(byte[] content)
BlockSource
.content
- input file.content
.public static BlockSource from(java.io.FileInputStream in)
FileInputStream
.
The returned BlockSource
is not thread-safe, as it must seek the
file channel to read a block.
in
- the file. The BlockSource
will close in
.in
.public static BlockSource from(java.nio.channels.FileChannel ch)
FileChannel
.
The returned BlockSource
is not thread-safe, as it must seek the
file channel to read a block.
ch
- the file. The BlockSource
will close ch
.ch
.public abstract java.nio.ByteBuffer read(long position, int blockSize) throws java.io.IOException
To reduce copying, the returned ByteBuffer should have an accessible
array and arrayOffset() == 0
. The caller will discard the
ByteBuffer and directly use the backing array.
position
- position of the block in the file, specified in bytes from the
beginning of the file.blockSize
- size to read.java.io.IOException
- if block cannot be read.public abstract long size() throws java.io.IOException
java.io.IOException
- if size cannot be obtained.public void adviseSequentialRead(long startPos, long endPos)
BlockSource
a sequential scan is starting.startPos
- starting position.endPos
- ending position.public abstract void close()
close
in interface java.lang.AutoCloseable