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

pack-priv Class ScratchFileBuffer

extends Object
implements RandomAccess
Class Inheritance
All Implemented Interfaces
org.apache.pdfbox.io.RandomAccess, org.apache.pdfbox.io.RandomAccessWrite, java.io.Closeable, java.lang.AutoCloseable, org.apache.pdfbox.io.RandomAccessRead
Imports
java.io.EOFException, .IOException

Implementation of RandomAccess as sequence of multiple fixed size pages handled by ScratchFile.

Field Summary

Modifier and TypeField and Description
private byte[]
currentPage

The current page data.

private boolean
currentPageContentChanged

true if current page was changed by a write method

private long
currentPageOffset

The offset of the current page within this buffer.

private int
currentPagePositionInPageIndexes

Index of current page in pageIndexes (the nth page within this buffer).

private int
pageCount

number of pages held by this buffer

private ScratchFile
pageHandler

The underlying page handler.

private int[]
pageIndexes

contains ordered list of pages with the index the page is known by page handler (ScratchFile)

private final int
private int
positionInPage

The current position (for next read/write) of the buffer as an offset in the current page.

private long
size

The number of bytes of content in this buffer.

Constructor Summary

AccessConstructor and Description
pack-priv
ScratchFileBuffer(ScratchFile
The ScratchFile managing the pages to be used by this buffer.
pageHandler
)

Creates a new buffer using pages handled by provided ScratchFile.

Method Summary

Modifier and TypeMethod and Description
private void
addPage()

Adds a new page and positions all pointers to start of new page.

private void
checkClosed()

Checks if this buffer, or the underlying ScratchFile have been closed, throwing IOException if so.

public final void
clear()

Implements org.apache.pdfbox.io.RandomAccessWrite.clear.

Clears all data of the buffer.

public void
close()

Implements java.io.Closeable.close.

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

pack-priv void
close(boolean
remove buffer from ScratchFile if set to true
removeBuffer
)

Release all resources and remove this buffer from ScratchFile.

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

Returns:

true if we were successful positioning pointer before end of page; we might return false if it is not allowed to add another page and current pointer points at end of last page
ensureAvailableBytesInPage
(boolean
if true it is allowed to add a new page in case we are currently at end of last buffer page
addNewPageIfNeeded
)

Ensures the current page has at least one byte left (positionInPage in < pageSize).

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.

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

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

Seek to a position in the data.

public void
write(int
The byte to write.
b
)

Implements org.apache.pdfbox.io.RandomAccessWrite.write.

Write a byte to the stream.

public void
write(byte[]
The buffer to get the data from.
b
)

Implements org.apache.pdfbox.io.RandomAccessWrite.write.

Write a buffer of data to the stream.

public void
write(byte[]
The buffer to get the data from.
b
,
int
An offset into the buffer to get the data from.
off
,
int
The length of data to write.
len
)

Implements org.apache.pdfbox.io.RandomAccessWrite.write.

Write a buffer of data to the stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentPageback to summary
private byte[] currentPage

The current page data.

currentPageContentChangedback to summary
private boolean currentPageContentChanged

true if current page was changed by a write method

currentPageOffsetback to summary
private long currentPageOffset

The offset of the current page within this buffer.

currentPagePositionInPageIndexesback to summary
private int currentPagePositionInPageIndexes

Index of current page in pageIndexes (the nth page within this buffer).

pageCountback to summary
private int pageCount

number of pages held by this buffer

pageHandlerback to summary
private ScratchFile pageHandler

The underlying page handler.

pageIndexesback to summary
private int[] pageIndexes

contains ordered list of pages with the index the page is known by page handler (ScratchFile)

pageSizeback to summary
private final int pageSize
positionInPageback to summary
private int positionInPage

The current position (for next read/write) of the buffer as an offset in the current page.

sizeback to summary
private long size

The number of bytes of content in this buffer.

Constructor Detail

ScratchFileBufferback to summary
pack-priv ScratchFileBuffer(ScratchFile pageHandler) throws IOException

Creates a new buffer using pages handled by provided ScratchFile.

Parameters
pageHandler:ScratchFile

The ScratchFile managing the pages to be used by this buffer.

Exceptions
IOException:
If getting first page failed.

Method Detail

addPageback to summary
private void addPage() throws IOException

Adds a new page and positions all pointers to start of new page.

Exceptions
IOException:
if requesting a new page fails
checkClosedback to summary
private void checkClosed() throws IOException

Checks if this buffer, or the underlying ScratchFile have been closed, throwing IOException if so.

Exceptions
IOException:
If either this buffer, or the underlying ScratchFile have been closed.
clearback to summary
public final void clear() throws IOException

Implements org.apache.pdfbox.io.RandomAccessWrite.clear.

Doc from org.apache.pdfbox.io.RandomAccessWrite.clear.

Clears all data of the buffer.

Annotations
@Override
Exceptions
IOException:
If there is an error while clearing the data.
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
closeback to summary
pack-priv void close(boolean removeBuffer)

Release all resources and remove this buffer from ScratchFile.

Parameters
removeBuffer:boolean

remove buffer from ScratchFile if set to true

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
ensureAvailableBytesInPageback to summary
private boolean ensureAvailableBytesInPage(boolean addNewPageIfNeeded) throws IOException

Ensures the current page has at least one byte left (positionInPage in < pageSize).

If this is not the case we go to next page (writing current one if changed). If current buffer has no more pages we add a new one.

Parameters
addNewPageIfNeeded:boolean

if true it is allowed to add a new page in case we are currently at end of last buffer page

Returns:boolean

true if we were successful positioning pointer before end of page; we might return false if it is not allowed to add another page and current pointer points at end of last page

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.
seekback to summary
public void seek(long seekToPosition) 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
seekToPosition:long

The position to seek to.

Annotations
@Override
Exceptions
IOException:
If there is an error while seeking.
writeback to summary
public void write(int b) throws IOException

Implements org.apache.pdfbox.io.RandomAccessWrite.write.

Doc from org.apache.pdfbox.io.RandomAccessWrite.write.

Write a byte to the stream.

Parameters
b:int

The byte to write.

Annotations
@Override
Exceptions
IOException:
If there is an IO error while writing.
writeback to summary
public void write(byte[] b) throws IOException

Implements org.apache.pdfbox.io.RandomAccessWrite.write.

Doc from org.apache.pdfbox.io.RandomAccessWrite.write.

Write a buffer of data to the stream.

Parameters
b:byte[]

The buffer to get the data from.

Annotations
@Override
Exceptions
IOException:
If there is an error while writing the data.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Implements org.apache.pdfbox.io.RandomAccessWrite.write.

Doc from org.apache.pdfbox.io.RandomAccessWrite.write.

Write a buffer of data to the stream.

Parameters
b:byte[]

The buffer to get the data from.

off:int

An offset into the buffer to get the data from.

len:int

The length of data to write.

Annotations
@Override
Exceptions
IOException:
If there is an error while writing the data.