Top Description Fields Constructors Methods
org.apache.pdfbox.io

public Class RandomAccessReadBuffer

extends Object
implements RandomAccessRead
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.io.RandomAccessRead, java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
org.apache.pdfbox.io.RandomAccessReadWriteBuffer
Imports
java.io.IOException, .InputStream, java.nio.ByteBuffer, java.util.ArrayList, .List, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap

An implementation of the RandomAccessRead interface to store data in memory. The data will be stored in chunks organized in an ArrayList.

Field Summary

Modifier and TypeField and Description
private final List<ByteBuffer>
private int
private int
protected int
protected ByteBuffer
protected int
public static final int
protected long
private final ConcurrentMap<Long, RandomAccessReadBuffer>
protected long

Constructor Summary

AccessConstructor and Description
protected
RandomAccessReadBuffer()

Default constructor.

protected
RandomAccessReadBuffer(int definedChunkSize)

Default constructor.

public
RandomAccessReadBuffer(byte[]
the byte array to be read
input
)

Create a random access buffer using the given byte array.

public
RandomAccessReadBuffer(ByteBuffer
the ByteBuffer to be read
input
)

Create a random access buffer using the given ByteBuffer.

public
RandomAccessReadBuffer(InputStream
the input stream to be read
input
)

Create a random access read buffer of the given input stream by copying the data to the memory.

private

Method Summary

Modifier and TypeMethod and Description
protected void
checkClosed()

Ensure that the RandomAccessBuffer is not closed

public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

public static RandomAccessReadBuffer

Returns:

the RandomAccessReadBuffer holding the data of the InputStream
createBufferFromStream
(InputStream
the InputStream holding the data to be copied
inputStream
)

Create e new RandomAccessReadBuffer using the given InputStream.

public RandomAccessReadView
createView(long
start position within the underlying random access read
startPosition
,
long
stream length
streamLength
)

Implements org.apache.pdfbox.io.RandomAccessRead.createView.

Creates a random access read view starting at the given position with the given length.

protected void
expandBuffer()

create a new buffer chunk and adjust all pointers and indices.

public long
getPosition()

Implements org.apache.pdfbox.io.RandomAccessRead.getPosition.

Returns offset of next byte to be returned by a read method.

public boolean
isClosed()

Implements org.apache.pdfbox.io.RandomAccessRead.isClosed.

Returns true if this source has been closed.

public boolean
isEOF()

Implements org.apache.pdfbox.io.RandomAccessRead.isEOF.

A simple test to see if we are at the end of the data.

public long
length()

Implements org.apache.pdfbox.io.RandomAccessRead.length.

The total number of bytes that are available.

private void
nextBuffer()

switch to the next buffer chunk and reset the buffer pointer.

public int
read()

Implements org.apache.pdfbox.io.RandomAccessRead.read.

Read a single byte of data.

public int
read(byte[]
The buffer to write the data to.
b
,
int
Offset into the buffer to start writing.
offset
,
int
The amount of data to attempt to read.
length
)

Implements org.apache.pdfbox.io.RandomAccessRead.read.

Read a buffer of data.

private int
readRemainingBytes(byte[] b, int offset, int length)

protected void
resetBuffers()

Reset to position 0 and remove all buffers but the first one.

public void
seek(long
The position to seek to.
position
)

Implements org.apache.pdfbox.io.RandomAccessRead.seek.

Seek to a position in the data.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferListback to summary
private final List<ByteBuffer> bufferList
bufferListIndexback to summary
private int bufferListIndex
bufferListMaxIndexback to summary
private int bufferListMaxIndex
chunkSizeback to summary
protected int chunkSize
currentBufferback to summary
protected ByteBuffer currentBuffer
currentBufferPointerback to summary
protected int currentBufferPointer
DEFAULT_CHUNK_SIZE_4KBback to summary
public static final int DEFAULT_CHUNK_SIZE_4KB
pointerback to summary
protected long pointer
rarbCopiesback to summary
private final ConcurrentMap<Long, RandomAccessReadBuffer> rarbCopies
sizeback to summary
protected long size

Constructor Detail

RandomAccessReadBufferback to summary
protected RandomAccessReadBuffer()

Default constructor.

RandomAccessReadBufferback to summary
protected RandomAccessReadBuffer(int definedChunkSize)

Default constructor.

RandomAccessReadBufferback to summary
public RandomAccessReadBuffer(byte[] input)

Create a random access buffer using the given byte array.

Parameters
input:byte[]

the byte array to be read

RandomAccessReadBufferback to summary
public RandomAccessReadBuffer(ByteBuffer input)

Create a random access buffer using the given ByteBuffer.

Parameters
input:ByteBuffer

the ByteBuffer to be read

RandomAccessReadBufferback to summary
public RandomAccessReadBuffer(InputStream input) throws IOException

Create a random access read buffer of the given input stream by copying the data to the memory.

Parameters
input:InputStream

the input stream to be read

Exceptions
IOException:
if something went wrong while copying the data
RandomAccessReadBufferback to summary
private RandomAccessReadBuffer(RandomAccessReadBuffer parent)

Method Detail

checkClosedback to summary
protected void checkClosed() throws IOException

Ensure that the RandomAccessBuffer is not closed

Exceptions
IOException:
If RandomAccessBuffer already closed
closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
createBufferFromStreamback to summary
public static RandomAccessReadBuffer createBufferFromStream(InputStream inputStream) throws IOException

Create e new RandomAccessReadBuffer using the given InputStream. The data is copied to the memory and the InputStream is closed.

Parameters
inputStream:InputStream

the InputStream holding the data to be copied

Returns:RandomAccessReadBuffer

the RandomAccessReadBuffer holding the data of the InputStream

Exceptions
IOException:
if something went wrong while copying the data
createViewback to summary
public RandomAccessReadView createView(long startPosition, long streamLength) throws IOException

Implements org.apache.pdfbox.io.RandomAccessRead.createView.

Doc from org.apache.pdfbox.io.RandomAccessRead.createView.

Creates a random access read view starting at the given position with the given length.

Parameters
startPosition:long

start position within the underlying random access read

streamLength:long

stream length

Returns:RandomAccessReadView

the random access read view

Annotations
@Override
Exceptions
IOException:
if something went wrong when creating the view for the RandomAccessRead
expandBufferback to summary
protected void expandBuffer() throws IOException

create a new buffer chunk and adjust all pointers and indices.

getPositionback to summary
public long getPosition() throws IOException

Implements org.apache.pdfbox.io.RandomAccessRead.getPosition.

Doc from org.apache.pdfbox.io.RandomAccessRead.getPosition.

Returns offset of next byte to be returned by a read method.

Returns:long

offset of next byte which will be returned with next read() (if no more bytes are left it returns a value >= length of source)

Annotations
@Override
Exceptions
IOException:
If there was an error while getting the current position
isClosedback to summary
public boolean isClosed()

Implements org.apache.pdfbox.io.RandomAccessRead.isClosed.

Doc from org.apache.pdfbox.io.RandomAccessRead.isClosed.

Returns true if this source has been closed.

Returns:boolean

true if the source has been closed

Annotations
@Override
isEOFback to summary
public boolean isEOF() throws IOException

Implements org.apache.pdfbox.io.RandomAccessRead.isEOF.

Doc from org.apache.pdfbox.io.RandomAccessRead.isEOF.

A simple test to see if we are at the end of the data.

Returns:boolean

true if we are at the end of the data.

Annotations
@Override
Exceptions
IOException:
If there is an error reading the next byte.
lengthback to summary
public long length() throws IOException

Implements org.apache.pdfbox.io.RandomAccessRead.length.

Doc from org.apache.pdfbox.io.RandomAccessRead.length.

The total number of bytes that are available.

Returns:long

The number of bytes available.

Annotations
@Override
Exceptions
IOException:
If there is an IO error while determining the length of the data stream.
nextBufferback to summary
private void nextBuffer() throws IOException

switch to the next buffer chunk and reset the buffer pointer.

readback to summary
public int read() throws IOException

Implements org.apache.pdfbox.io.RandomAccessRead.read.

Doc from org.apache.pdfbox.io.RandomAccessRead.read.

Read a single byte of data.

Returns:int

The byte of data that is being read.

Annotations
@Override
Exceptions
IOException:
If there is an error while reading the data.
readback to summary
public int read(byte[] b, int offset, int length) throws IOException

Implements org.apache.pdfbox.io.RandomAccessRead.read.

Doc from org.apache.pdfbox.io.RandomAccessRead.read.

Read a buffer of data.

Parameters
b:byte[]

The buffer to write the data to.

offset:int

Offset into the buffer to start writing.

length:int

The amount of data to attempt to read.

Returns:int

The number of bytes that were actually read.

Annotations
@Override
Exceptions
IOException:
If there was an error while reading the data.
readRemainingBytesback to summary
private int readRemainingBytes(byte[] b, int offset, int length)
resetBuffersback to summary
protected void resetBuffers()

Reset to position 0 and remove all buffers but the first one.

seekback to summary
public void seek(long position) throws IOException

Implements org.apache.pdfbox.io.RandomAccessRead.seek.

Doc from org.apache.pdfbox.io.RandomAccessRead.seek.

Seek to a position in the data.

Parameters
position:long

The position to seek to.

Annotations
@Override
Exceptions
IOException:
If there is an error while seeking.