Package org.apache.pdfbox.io
Class ScratchFileBuffer
java.lang.Object
org.apache.pdfbox.io.ScratchFileBuffer
- All Implemented Interfaces:
Closeable
,AutoCloseable
,RandomAccess
,RandomAccessRead
,RandomAccessWrite
Implementation of
RandomAccess
as sequence of multiple fixed size pages handled
by ScratchFile
.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]
The current page data.private boolean
true
if current page was changed by a write methodprivate long
The offset of the current page within this buffer.private int
Index of current page inpageIndexes
(the nth page within this buffer).private int
number of pages held by this bufferprivate ScratchFile
The underlying page handler.private int[]
contains ordered list of pages with the index the page is known by page handler (ScratchFile
)private final int
private int
The current position (for next read/write) of the buffer as an offset in the current page.private long
The number of bytes of content in this buffer. -
Constructor Summary
ConstructorsConstructorDescriptionScratchFileBuffer
(ScratchFile pageHandler) Creates a new buffer using pages handled by providedScratchFile
. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addPage()
Adds a new page and positions all pointers to start of new page.int
Returns an estimate of the number of bytes that can be read.private void
Checks if this buffer, or the underlyingScratchFile
have been closed, throwingIOException
if so.final void
clear()
Clears all data of the buffer.void
close()
(package private) void
close
(boolean removeBuffer) Release all resources and remove this buffer from ScratchFile.private boolean
ensureAvailableBytesInPage
(boolean addNewPageIfNeeded) Ensures the current page has at least one byte left (positionInPage
in <pageSize
).long
Returns offset of next byte to be returned by a read method.boolean
isClosed()
Returns true if this stream has been closed.boolean
isEOF()
A simple test to see if we are at the end of the data.long
length()
The total number of bytes that are available.int
peek()
This will peek at the next byte.int
read()
Read a single byte of data.int
read
(byte[] b) Read a buffer of data.int
read
(byte[] b, int off, int len) Read a buffer of data.byte[]
readFully
(int length) Reads a given number of bytes.void
rewind
(int bytes) Seek backwards the given number of bytes.void
seek
(long seekToPosition) Seek to a position in the data.void
write
(byte[] b) Write a buffer of data to the stream.void
write
(byte[] b, int off, int len) Write a buffer of data to the stream.void
write
(int b) Write a byte to the stream.
-
Field Details
-
pageSize
private final int pageSize -
pageHandler
The underlying page handler. -
size
private long sizeThe number of bytes of content in this buffer. -
currentPagePositionInPageIndexes
private int currentPagePositionInPageIndexesIndex of current page inpageIndexes
(the nth page within this buffer). -
currentPageOffset
private long currentPageOffsetThe offset of the current page within this buffer. -
currentPage
private byte[] currentPageThe current page data. -
positionInPage
private int positionInPageThe current position (for next read/write) of the buffer as an offset in the current page. -
currentPageContentChanged
private boolean currentPageContentChangedtrue
if current page was changed by a write method -
pageIndexes
private int[] pageIndexescontains ordered list of pages with the index the page is known by page handler (ScratchFile
) -
pageCount
private int pageCountnumber of pages held by this buffer
-
-
Constructor Details
-
ScratchFileBuffer
ScratchFileBuffer(ScratchFile pageHandler) throws IOException Creates a new buffer using pages handled by providedScratchFile
.- Parameters:
pageHandler
- TheScratchFile
managing the pages to be used by this buffer.- Throws:
IOException
- If getting first page failed.
-
-
Method Details
-
checkClosed
Checks if this buffer, or the underlyingScratchFile
have been closed, throwingIOException
if so.- Throws:
IOException
- If either this buffer, or the underlyingScratchFile
have been closed.
-
addPage
Adds a new page and positions all pointers to start of new page.- Throws:
IOException
- if requesting a new page fails
-
length
The total number of bytes that are available.- Specified by:
length
in interfaceRandomAccessRead
- Returns:
- The number of bytes available.
- Throws:
IOException
- If there is an IO error while determining the length of the data stream.
-
ensureAvailableBytesInPage
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
- iftrue
it is allowed to add a new page in case we are currently at end of last buffer page- Returns:
true
if we were successful positioning pointer before end of page; we might returnfalse
if it is not allowed to add another page and current pointer points at end of last page- Throws:
IOException
-
write
Write a byte to the stream.- Specified by:
write
in interfaceRandomAccessWrite
- Parameters:
b
- The byte to write.- Throws:
IOException
- If there is an IO error while writing.
-
write
Write a buffer of data to the stream.- Specified by:
write
in interfaceRandomAccessWrite
- Parameters:
b
- The buffer to get the data from.- Throws:
IOException
- If there is an error while writing the data.
-
write
Write a buffer of data to the stream.- Specified by:
write
in interfaceRandomAccessWrite
- Parameters:
b
- The buffer to get the data from.off
- An offset into the buffer to get the data from.len
- The length of data to write.- Throws:
IOException
- If there is an error while writing the data.
-
clear
Clears all data of the buffer.- Specified by:
clear
in interfaceRandomAccessWrite
- Throws:
IOException
-
getPosition
Returns offset of next byte to be returned by a read method.- Specified by:
getPosition
in 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:
IOException
-
seek
Seek to a position in the data.- Specified by:
seek
in interfaceRandomAccessRead
- Parameters:
seekToPosition
- The position to seek to.- Throws:
IOException
- If there is an error while seeking.
-
isClosed
public boolean isClosed()Returns true if this stream has been closed.- Specified by:
isClosed
in interfaceRandomAccessRead
-
peek
This will peek at the next byte.- Specified by:
peek
in interfaceRandomAccessRead
- Returns:
- The next byte on the stream, leaving it as available to read.
- Throws:
IOException
- If there is an error reading the next byte.
-
rewind
Seek backwards the given number of bytes.- Specified by:
rewind
in interfaceRandomAccessRead
- Parameters:
bytes
- the number of bytes to be seeked backwards- Throws:
IOException
- If there is an error while seeking
-
readFully
Reads a given number of bytes.- Specified by:
readFully
in interfaceRandomAccessRead
- Parameters:
length
- the number of bytes to be read- Returns:
- a byte array containing the bytes just read
- Throws:
IOException
- if an I/O error occurs while reading data
-
isEOF
A simple test to see if we are at the end of the data.- Specified by:
isEOF
in interfaceRandomAccessRead
- Returns:
- true if we are at the end of the data.
- Throws:
IOException
- If there is an error reading the next byte.
-
available
Returns an estimate of the number of bytes that can be read.- Specified by:
available
in interfaceRandomAccessRead
- Returns:
- the number of bytes that can be read
- Throws:
IOException
- if this random access has been closed
-
read
Read a single byte of data.- Specified by:
read
in interfaceRandomAccessRead
- Returns:
- The byte of data that is being read.
- Throws:
IOException
- If there is an error while reading the data.
-
read
Read a buffer of data.- Specified by:
read
in interfaceRandomAccessRead
- Parameters:
b
- The buffer to write the data to.- Returns:
- The number of bytes that were actually read.
- Throws:
IOException
- If there was an error while reading the data.
-
read
Read a buffer of data.- Specified by:
read
in interfaceRandomAccessRead
- Parameters:
b
- The buffer to write the data to.off
- Offset into the buffer to start writing.len
- The amount of data to attempt to read.- Returns:
- The number of bytes that were actually read.
- Throws:
IOException
- If there was an error while reading the data.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
close
void close(boolean removeBuffer) Release all resources and remove this buffer from ScratchFile.- Parameters:
removeBuffer
- remove buffer from ScratchFile if set to true
-