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

public Class NonSeekableRandomAccessReadInputStream

extends Object
implements RandomAccessRead
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.io.RandomAccessRead, java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.IOException, .InputStream, org.apache.commons.logging.Log, .LogFactory

An implementation of the RandomAccessRead interface using an InputStream as source. It is optimized for a minimal memory footprint by using a small buffer to read from the input stream instead of copying the whole input stream to memory. This reduces the random access read abilities, so that peek/rewind operations are limited to the data within the buffer. This class is meant to be used by consumers which process the data more or less in a serial manner and therefore don't need full random access.

Field Summary

Modifier and TypeField and Description
private static final int
private final int[]
private final byte[][]
private static final int
protected int
private final InputStream
private boolean
private boolean
private static final int
private static final Log
private static final int
protected long
protected long

Constructor Summary

AccessConstructor and Description
public
NonSeekableRandomAccessReadInputStream(InputStream inputStream)

Default constructor.

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 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.

private boolean
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.

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.

public void
rewind(int
the number of bytes to be seeked backwards
bytes
)

Overrides default org.apache.pdfbox.io.RandomAccessRead.rewind.

Seek backwards the given number of bytes.

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

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

Seek to a position in the data.

public void
skip(int
the number of bytes to be skipped
length
)

Overrides default org.apache.pdfbox.io.RandomAccessRead.skip.

Skips a given number of bytes.

private void
switchBuffers(int firstBuffer, int secondBuffer)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFFER_SIZEback to summary
private static final int BUFFER_SIZE
bufferBytesback to summary
private final int[] bufferBytes
buffersback to summary
private final byte[][] buffers
CURRENTback to summary
private static final int CURRENT
currentBufferPointerback to summary
protected int currentBufferPointer
isback to summary
private final InputStream is
isClosedback to summary
private boolean isClosed
isEOFback to summary
private boolean isEOF
LASTback to summary
private static final int LAST
LOGback to summary
private static final Log LOG
NEXTback to summary
private static final int NEXT
positionback to summary
protected long position
sizeback to summary
protected long size

Constructor Detail

NonSeekableRandomAccessReadInputStreamback to summary
public NonSeekableRandomAccessReadInputStream(InputStream inputStream)

Default constructor.

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
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
fetchback to summary
private boolean fetch() throws IOException
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.
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.
rewindback to summary
public void rewind(int bytes) throws IOException

Overrides default org.apache.pdfbox.io.RandomAccessRead.rewind.

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

Seek backwards the given number of bytes.

Parameters
bytes:int

the number of bytes to be seeked backwards

Annotations
@Override
Exceptions
IOException:
If there is an error while seeking
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.
skipback to summary
public void skip(int length) throws IOException

Overrides default org.apache.pdfbox.io.RandomAccessRead.skip.

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

Skips a given number of bytes.

Parameters
length:int

the number of bytes to be skipped

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs while reading data
switchBuffersback to summary
private void switchBuffers(int firstBuffer, int secondBuffer)