Package org.apache.pdfbox.io
Class ScratchFileBuffer
- java.lang.Object
-
- org.apache.pdfbox.io.ScratchFileBuffer
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,RandomAccess,RandomAccessRead,RandomAccessWrite
class ScratchFileBuffer extends java.lang.Object implements RandomAccess
Implementation ofRandomAccessas sequence of multiple fixed size pages handled byScratchFile.
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]currentPageThe current page data.private booleancurrentPageContentChangedtrueif current page was changed by a write methodprivate longcurrentPageOffsetThe offset of the current page within this buffer.private intcurrentPagePositionInPageIndexesIndex of current page inpageIndexes(the nth page within this buffer).private intpageCountnumber of pages held by this bufferprivate ScratchFilepageHandlerThe underlying page handler.private int[]pageIndexescontains ordered list of pages with the index the page is known by page handler (ScratchFile)private intpageSizeprivate intpositionInPageThe current position (for next read/write) of the buffer as an offset in the current page.private longsizeThe number of bytes of content in this buffer.
-
Constructor Summary
Constructors Constructor Description ScratchFileBuffer(ScratchFile pageHandler)Creates a new buffer using pages handled by providedScratchFile.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddPage()Adds a new page and positions all pointers to start of new page.private voidcheckClosed()Checks if this buffer, or the underlyingScratchFilehave been closed, throwingIOExceptionif so.voidclear()Clears all data of the buffer.voidclose()(package private) voidclose(boolean removeBuffer)Release all resources and remove this buffer from ScratchFile.RandomAccessReadViewcreateView(long startPosition, long streamLength)Creates a random access read view starting at the given position with the given length.private booleanensureAvailableBytesInPage(boolean addNewPageIfNeeded)Ensures the current page has at least one byte left (positionInPagein <pageSize).longgetPosition()Returns offset of next byte to be returned by a read method.booleanisClosed()Returns true if this source has been closed.booleanisEOF()A simple test to see if we are at the end of the data.longlength()The total number of bytes that are available.intread()Read a single byte of data.intread(byte[] b, int off, int len)Read a buffer of data.voidseek(long seekToPosition)Seek to a position in the data.voidwrite(byte[] b)Write a buffer of data to the stream.voidwrite(byte[] b, int off, int len)Write a buffer of data to the stream.voidwrite(int b)Write a byte to the stream.
-
-
-
Field Detail
-
pageSize
private final int pageSize
-
pageHandler
private ScratchFile pageHandler
The underlying page handler.
-
size
private long size
The number of bytes of content in this buffer.
-
currentPagePositionInPageIndexes
private int currentPagePositionInPageIndexes
Index of current page inpageIndexes(the nth page within this buffer).
-
currentPageOffset
private long currentPageOffset
The offset of the current page within this buffer.
-
currentPage
private byte[] currentPage
The current page data.
-
positionInPage
private int positionInPage
The current position (for next read/write) of the buffer as an offset in the current page.
-
currentPageContentChanged
private boolean currentPageContentChanged
trueif current page was changed by a write method
-
pageIndexes
private int[] pageIndexes
contains ordered list of pages with the index the page is known by page handler (ScratchFile)
-
pageCount
private int pageCount
number of pages held by this buffer
-
-
Constructor Detail
-
ScratchFileBuffer
ScratchFileBuffer(ScratchFile pageHandler) throws java.io.IOException
Creates a new buffer using pages handled by providedScratchFile.- Parameters:
pageHandler- TheScratchFilemanaging the pages to be used by this buffer.- Throws:
java.io.IOException- If getting first page failed.
-
-
Method Detail
-
checkClosed
private void checkClosed() throws java.io.IOExceptionChecks if this buffer, or the underlyingScratchFilehave been closed, throwingIOExceptionif so.- Throws:
java.io.IOException- If either this buffer, or the underlyingScratchFilehave been closed.
-
addPage
private void addPage() throws java.io.IOExceptionAdds a new page and positions all pointers to start of new page.- Throws:
java.io.IOException- if requesting a new page fails
-
length
public long length() throws java.io.IOExceptionThe total number of bytes that are available.- Specified by:
lengthin interfaceRandomAccessRead- Returns:
- The number of bytes available.
- Throws:
java.io.IOException- If there is an IO error while determining the length of the data stream.
-
ensureAvailableBytesInPage
private boolean ensureAvailableBytesInPage(boolean addNewPageIfNeeded) throws java.io.IOExceptionEnsures the current page has at least one byte left (positionInPagein <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- iftrueit is allowed to add a new page in case we are currently at end of last buffer page- Returns:
trueif we were successful positioning pointer before end of page; we might returnfalseif it is not allowed to add another page and current pointer points at end of last page- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOExceptionWrite a byte to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
b- The byte to write.- Throws:
java.io.IOException- If there is an IO error while writing.
-
write
public void write(byte[] b) throws java.io.IOExceptionWrite a buffer of data to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
b- The buffer to get the data from.- Throws:
java.io.IOException- If there is an error while writing the data.
-
write
public void write(byte[] b, int off, int len) throws java.io.IOExceptionWrite a buffer of data to the stream.- Specified by:
writein 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:
java.io.IOException- If there is an error while writing the data.
-
clear
public final void clear() throws java.io.IOExceptionClears all data of the buffer.- Specified by:
clearin interfaceRandomAccessWrite- Throws:
java.io.IOException- If there is an error while clearing the data.
-
getPosition
public long getPosition() throws java.io.IOExceptionReturns offset of next byte to be returned by a read method.- Specified by:
getPositionin 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:
java.io.IOException- If there was an error while getting the current position
-
seek
public void seek(long seekToPosition) throws java.io.IOExceptionSeek to a position in the data.- Specified by:
seekin interfaceRandomAccessRead- Parameters:
seekToPosition- The position to seek to.- Throws:
java.io.IOException- If there is an error while seeking.
-
isClosed
public boolean isClosed()
Returns true if this source has been closed.- Specified by:
isClosedin interfaceRandomAccessRead- Returns:
- true if the source has been closed
-
isEOF
public boolean isEOF() throws java.io.IOExceptionA simple test to see if we are at the end of the data.- Specified by:
isEOFin interfaceRandomAccessRead- Returns:
- true if we are at the end of the data.
- Throws:
java.io.IOException- If there is an error reading the next byte.
-
read
public int read() throws java.io.IOExceptionRead a single byte of data.- Specified by:
readin interfaceRandomAccessRead- Returns:
- The byte of data that is being read.
- Throws:
java.io.IOException- If there is an error while reading the data.
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionRead a buffer of data.- Specified by:
readin 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:
java.io.IOException- If there was an error while reading the data.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.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
-
createView
public RandomAccessReadView createView(long startPosition, long streamLength) throws java.io.IOException
Description copied from interface:RandomAccessReadCreates a random access read view starting at the given position with the given length.- Specified by:
createViewin interfaceRandomAccessRead- Parameters:
startPosition- start position within the underlying random access readstreamLength- stream length- Returns:
- the random access read view
- Throws:
java.io.IOException- if something went wrong when creating the view for the RandomAccessRead
-
-