public class RandomAccessBuffer extends java.lang.Object implements RandomAccess, java.lang.Cloneable
Modifier and Type | Field and Description |
---|---|
private java.util.List<byte[]> |
bufferList |
private int |
bufferListIndex |
private int |
bufferListMaxIndex |
private int |
chunkSize |
private byte[] |
currentBuffer |
private int |
currentBufferPointer |
private static int |
DEFAULT_CHUNK_SIZE |
private long |
pointer |
private long |
size |
Modifier | Constructor and Description |
---|---|
|
RandomAccessBuffer()
Default constructor.
|
|
RandomAccessBuffer(byte[] input)
Create a random access buffer using the given byte array.
|
|
RandomAccessBuffer(java.io.InputStream input)
Create a random access buffer of the given input stream by copying the data.
|
private |
RandomAccessBuffer(int definedChunkSize)
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read.
|
private void |
checkClosed()
Ensure that the RandomAccessBuffer is not closed
|
void |
clear()
Clears all data of the buffer.
|
RandomAccessBuffer |
clone() |
void |
close() |
private void |
expandBuffer()
create a new buffer chunk and adjust all pointers and indices.
|
long |
getPosition()
Returns offset of next byte to be returned by a read method.
|
boolean |
isClosed()
Returns true if this stream has been closed.
|
boolean |
isEOF()
A simple test to see if we are at the end of the data.
|
long |
length()
The total number of bytes that are available.
|
private void |
nextBuffer()
switch to the next buffer chunk and reset the buffer pointer.
|
int |
peek()
This will peek at the next byte.
|
int |
read()
Read a single byte of data.
|
int |
read(byte[] b)
Read a buffer of data.
|
int |
read(byte[] b,
int offset,
int length)
Read a buffer of data.
|
byte[] |
readFully(int length)
Reads a given number of bytes.
|
private int |
readRemainingBytes(byte[] b,
int offset,
int length) |
void |
rewind(int bytes)
Seek backwards the given number of bytes.
|
void |
seek(long position)
Seek to a position in the data.
|
void |
write(byte[] b)
Write a buffer of data to the stream.
|
void |
write(byte[] b,
int offset,
int length)
Write a buffer of data to the stream.
|
void |
write(int b)
Write a byte to the stream.
|
private static final int DEFAULT_CHUNK_SIZE
private int chunkSize
private java.util.List<byte[]> bufferList
private byte[] currentBuffer
private long pointer
private int currentBufferPointer
private long size
private int bufferListIndex
private int bufferListMaxIndex
public RandomAccessBuffer()
private RandomAccessBuffer(int definedChunkSize)
public RandomAccessBuffer(byte[] input)
input
- the byte array to be readpublic RandomAccessBuffer(java.io.InputStream input) throws java.io.IOException
input
- the input stream to be readjava.io.IOException
- if something went wrong while copying the datapublic RandomAccessBuffer clone()
clone
in class java.lang.Object
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
public void clear()
clear
in interface RandomAccessWrite
public void seek(long position) throws java.io.IOException
seek
in interface RandomAccessRead
position
- The position to seek to.java.io.IOException
- If there is an error while seeking.public long getPosition() throws java.io.IOException
getPosition
in interface RandomAccessRead
RandomAccessRead.read()
(if no more bytes are left it returns a value >= length of source)java.io.IOException
public int read() throws java.io.IOException
read
in interface RandomAccessRead
java.io.IOException
- If there is an error while reading the data.public int read(byte[] b, int offset, int length) throws java.io.IOException
read
in interface RandomAccessRead
b
- The buffer to write the data to.offset
- Offset into the buffer to start writing.length
- The amount of data to attempt to read.java.io.IOException
- If there was an error while reading the data.private int readRemainingBytes(byte[] b, int offset, int length)
public long length() throws java.io.IOException
length
in interface RandomAccessRead
java.io.IOException
- If there is an IO error while determining the
length of the data stream.public void write(int b) throws java.io.IOException
write
in interface RandomAccessWrite
b
- The byte to write.java.io.IOException
- If there is an IO error while writing.public void write(byte[] b) throws java.io.IOException
write
in interface RandomAccessWrite
b
- The buffer to get the data from.java.io.IOException
- If there is an error while writing the data.public void write(byte[] b, int offset, int length) throws java.io.IOException
write
in interface RandomAccessWrite
b
- The buffer to get the data from.offset
- An offset into the buffer to get the data from.length
- The length of data to write.java.io.IOException
- If there is an error while writing the data.private void expandBuffer() throws java.io.IOException
java.io.IOException
private void nextBuffer() throws java.io.IOException
java.io.IOException
private void checkClosed() throws java.io.IOException
java.io.IOException
public boolean isClosed()
isClosed
in interface RandomAccessRead
public boolean isEOF() throws java.io.IOException
isEOF
in interface RandomAccessRead
java.io.IOException
- If there is an error reading the next byte.public int available() throws java.io.IOException
available
in interface RandomAccessRead
java.io.IOException
- if this random access has been closedpublic int peek() throws java.io.IOException
peek
in interface RandomAccessRead
java.io.IOException
- If there is an error reading the next byte.public void rewind(int bytes) throws java.io.IOException
rewind
in interface RandomAccessRead
bytes
- the number of bytes to be seeked backwardsjava.io.IOException
- If there is an error while seekingpublic byte[] readFully(int length) throws java.io.IOException
readFully
in interface RandomAccessRead
length
- the number of bytes to be readjava.io.IOException
- if an I/O error occurs while reading datapublic int read(byte[] b) throws java.io.IOException
read
in interface RandomAccessRead
b
- The buffer to write the data to.java.io.IOException
- If there was an error while reading the data.