Package org.apache.pdfbox.io
Class RandomAccessBuffer
- java.lang.Object
-
- org.apache.pdfbox.io.RandomAccessBuffer
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Cloneable,RandomAccess,RandomAccessRead,RandomAccessWrite
public class RandomAccessBuffer extends java.lang.Object implements RandomAccess, java.lang.Cloneable
An implementation of the RandomAccess interface to store data in memory. The data will be stored in chunks organized in an ArrayList.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<byte[]>bufferListprivate intbufferListIndexprivate intbufferListMaxIndexprivate intchunkSizeprivate byte[]currentBufferprivate intcurrentBufferPointerprivate static intDEFAULT_CHUNK_SIZEprivate longpointerprivate longsize
-
Constructor Summary
Constructors Modifier Constructor Description RandomAccessBuffer()Default constructor.RandomAccessBuffer(byte[] input)Create a random access buffer using the given byte array.privateRandomAccessBuffer(int definedChunkSize)Default constructor.RandomAccessBuffer(java.io.InputStream input)Create a random access buffer of the given input stream by copying the data.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns an estimate of the number of bytes that can be read.private voidcheckClosed()Ensure that the RandomAccessBuffer is not closedvoidclear()Clears all data of the buffer.RandomAccessBufferclone()voidclose()private voidexpandBuffer()create a new buffer chunk and adjust all pointers and indices.longgetPosition()Returns offset of next byte to be returned by a read method.booleanisClosed()Returns true if this stream has been closed.booleanisEOF()A simple test to see if we are at the end of the data.longlength()The total number of bytes that are available.private voidnextBuffer()switch to the next buffer chunk and reset the buffer pointer.intpeek()This will peek at the next byte.intread()Read a single byte of data.intread(byte[] b)Read a buffer of data.intread(byte[] b, int offset, int length)Read a buffer of data.byte[]readFully(int length)Reads a given number of bytes.private intreadRemainingBytes(byte[] b, int offset, int length)voidrewind(int bytes)Seek backwards the given number of bytes.voidseek(long position)Seek to a position in the data.voidwrite(byte[] b)Write a buffer of data to the stream.voidwrite(byte[] b, int offset, int length)Write a buffer of data to the stream.voidwrite(int b)Write a byte to the stream.
-
-
-
Field Detail
-
DEFAULT_CHUNK_SIZE
private static final int DEFAULT_CHUNK_SIZE
- See Also:
- Constant Field Values
-
chunkSize
private int chunkSize
-
bufferList
private java.util.List<byte[]> bufferList
-
currentBuffer
private byte[] currentBuffer
-
pointer
private long pointer
-
currentBufferPointer
private int currentBufferPointer
-
size
private long size
-
bufferListIndex
private int bufferListIndex
-
bufferListMaxIndex
private int bufferListMaxIndex
-
-
Constructor Detail
-
RandomAccessBuffer
public RandomAccessBuffer()
Default constructor.
-
RandomAccessBuffer
private RandomAccessBuffer(int definedChunkSize)
Default constructor.
-
RandomAccessBuffer
public RandomAccessBuffer(byte[] input)
Create a random access buffer using the given byte array.- Parameters:
input- the byte array to be read
-
RandomAccessBuffer
public RandomAccessBuffer(java.io.InputStream input) throws java.io.IOExceptionCreate a random access buffer of the given input stream by copying the data.- Parameters:
input- the input stream to be read- Throws:
java.io.IOException- if something went wrong while copying the data
-
-
Method Detail
-
clone
public RandomAccessBuffer clone()
- Overrides:
clonein classjava.lang.Object
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
clear
public void clear()
Clears all data of the buffer.- Specified by:
clearin interfaceRandomAccessWrite
-
seek
public void seek(long position) throws java.io.IOExceptionSeek to a position in the data.- Specified by:
seekin interfaceRandomAccessRead- Parameters:
position- The position to seek to.- Throws:
java.io.IOException- If there is an error while seeking.
-
getPosition
public long getPosition() throws java.io.IOExceptionReturns offset of next byte to be returned by a read method.- Specified by:
getPositionin interfaceRandomAccessRead- Returns:
- offset of next byte which will be returned with next
RandomAccessRead.read()(if no more bytes are left it returns a value >= length of source) - Throws:
java.io.IOException
-
read
public int read() throws java.io.IOExceptionRead a single byte of data.- Specified by:
readin interfaceRandomAccessRead- Returns:
- The byte of data that is being read.
- Throws:
java.io.IOException- If there is an error while reading the data.
-
read
public int read(byte[] b, int offset, int length) throws java.io.IOExceptionRead a buffer of data.- Specified by:
readin interfaceRandomAccessRead- Parameters:
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.- Returns:
- The number of bytes that were actually read.
- Throws:
java.io.IOException- If there was an error while reading the data.
-
readRemainingBytes
private int readRemainingBytes(byte[] b, int offset, int length)
-
length
public long length() throws java.io.IOExceptionThe total number of bytes that are available.- Specified by:
lengthin interfaceRandomAccessRead- Returns:
- The number of bytes available.
- Throws:
java.io.IOException- If there is an IO error while determining the length of the data stream.
-
write
public void write(int b) throws java.io.IOExceptionWrite a byte to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
b- The byte to write.- Throws:
java.io.IOException- If there is an IO error while writing.
-
write
public void write(byte[] b) throws java.io.IOExceptionWrite a buffer of data to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
b- The buffer to get the data from.- Throws:
java.io.IOException- If there is an error while writing the data.
-
write
public void write(byte[] b, int offset, int length) throws java.io.IOExceptionWrite a buffer of data to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
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.- Throws:
java.io.IOException- If there is an error while writing the data.
-
expandBuffer
private void expandBuffer() throws java.io.IOExceptioncreate a new buffer chunk and adjust all pointers and indices.- Throws:
java.io.IOException
-
nextBuffer
private void nextBuffer() throws java.io.IOExceptionswitch to the next buffer chunk and reset the buffer pointer.- Throws:
java.io.IOException
-
checkClosed
private void checkClosed() throws java.io.IOExceptionEnsure that the RandomAccessBuffer is not closed- Throws:
java.io.IOException- if the RandomAccessBuffer is already closed
-
isClosed
public boolean isClosed()
Returns true if this stream has been closed.- Specified by:
isClosedin interfaceRandomAccessRead
-
isEOF
public boolean isEOF() throws java.io.IOExceptionA simple test to see if we are at the end of the data.- Specified by:
isEOFin interfaceRandomAccessRead- Returns:
- true if we are at the end of the data.
- Throws:
java.io.IOException- If there is an error reading the next byte.
-
available
public int available() throws java.io.IOExceptionReturns an estimate of the number of bytes that can be read.- Specified by:
availablein interfaceRandomAccessRead- Returns:
- the number of bytes that can be read
- Throws:
java.io.IOException- if this random access has been closed
-
peek
public int peek() throws java.io.IOExceptionThis will peek at the next byte.- Specified by:
peekin interfaceRandomAccessRead- Returns:
- The next byte on the stream, leaving it as available to read.
- Throws:
java.io.IOException- If there is an error reading the next byte.
-
rewind
public void rewind(int bytes) throws java.io.IOExceptionSeek backwards the given number of bytes.- Specified by:
rewindin interfaceRandomAccessRead- Parameters:
bytes- the number of bytes to be seeked backwards- Throws:
java.io.IOException- If there is an error while seeking
-
readFully
public byte[] readFully(int length) throws java.io.IOExceptionReads a given number of bytes.- Specified by:
readFullyin interfaceRandomAccessRead- Parameters:
length- the number of bytes to be read- Returns:
- a byte array containing the bytes just read
- Throws:
java.io.IOException- if an I/O error occurs while reading data
-
read
public int read(byte[] b) throws java.io.IOExceptionRead a buffer of data.- Specified by:
readin interfaceRandomAccessRead- Parameters:
b- The buffer to write the data to.- Returns:
- The number of bytes that were actually read.
- Throws:
java.io.IOException- If there was an error while reading the data.
-
-