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

public Class RandomAccessReadBufferedFile

extends Object
implements RandomAccessRead
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.io.RandomAccessRead, java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.File, .IOException, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.nio.file.Path, .StandardOpenOption, java.util.LinkedHashMap, .Map, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap

Provides random access to portions of a file combined with buffered reading of content. Start of next bytes to read can be set via seek method. File is accessed via FileChannel and is read in ByteBuffer chunks which are cached.
Author
Timo Boehme

Field Summary

Modifier and TypeField and Description
private ByteBuffer
private long
private final FileChannel
private final long
private long
private boolean
private ByteBuffer
private static final int
private int
private static final long
private static final int
private static final int
private final Map<Long, ByteBuffer>
pageCache

Create a LRU page cache.

private final Path
private final ConcurrentMap<Long, RandomAccessReadBufferedFile>

Constructor Summary

AccessConstructor and Description
public
RandomAccessReadBufferedFile(String
the filename of the file to be read.
filename
)

Create a random access buffered file instance for the file with the given name.

public
RandomAccessReadBufferedFile(File
the file to be read.
file
)

Create a random access buffered file instance for the given file.

public
RandomAccessReadBufferedFile(Path
path of the file to be read.
path
)

Create a random access buffered file instance using the given path.

Method Summary

Modifier and TypeMethod and Description
private 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.

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.
off
,
int
The amount of data to attempt to read.
len
)

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

Read a buffer of data.

private ByteBuffer
readPage()

Reads a page with data from current file position.

public void
seek(final long
the position to seek to.
position
)

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

Seeks to new position.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

curPageback to summary
private ByteBuffer curPage
curPageOffsetback to summary
private long curPageOffset
fileChannelback to summary
private final FileChannel fileChannel
fileLengthback to summary
private final long fileLength
fileOffsetback to summary
private long fileOffset
isClosedback to summary
private boolean isClosed
lastRemovedCachePageback to summary
private ByteBuffer lastRemovedCachePage
MAX_CACHED_PAGESback to summary
private static final int MAX_CACHED_PAGES
offsetWithinPageback to summary
private int offsetWithinPage
PAGE_OFFSET_MASKback to summary
private static final long PAGE_OFFSET_MASK
PAGE_SIZEback to summary
private static final int PAGE_SIZE
PAGE_SIZE_SHIFTback to summary
private static final int PAGE_SIZE_SHIFT
pageCacheback to summary
private final Map<Long, ByteBuffer> pageCache

Create a LRU page cache.

pathback to summary
private final Path path
rafCopiesback to summary
private final ConcurrentMap<Long, RandomAccessReadBufferedFile> rafCopies

Constructor Detail

RandomAccessReadBufferedFileback to summary
public RandomAccessReadBufferedFile(String filename) throws IOException

Create a random access buffered file instance for the file with the given name.

Parameters
filename:String

the filename of the file to be read.

Exceptions
IOException:
if something went wrong while accessing the given file.
RandomAccessReadBufferedFileback to summary
public RandomAccessReadBufferedFile(File file) throws IOException

Create a random access buffered file instance for the given file.

Parameters
file:File

the file to be read.

Exceptions
IOException:
if something went wrong while accessing the given file.
RandomAccessReadBufferedFileback to summary
public RandomAccessReadBufferedFile(Path path) throws IOException

Create a random access buffered file instance using the given path.

Parameters
path:Path

path of the file to be read.

Exceptions
IOException:
if something went wrong while accessing the given file.

Method Detail

checkClosedback to summary
private 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
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 off, int len) 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.

off:int

Offset into the buffer to start writing.

len: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.
readPageback to summary
private ByteBuffer readPage() throws IOException

Reads a page with data from current file position. If we have a previously removed page from cache the buffer of this page is reused. Otherwise a new byte buffer is created.

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

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

Seeks to new position. If new position is outside of current page the new page is either taken from cache or read from file and added to cache.

Parameters
position:long

the position to seek to.

Annotations
@Override
Exceptions
IOException:
if something went wrong.