class ScratchFileBuffer extends java.lang.Object implements RandomAccess
RandomAccess
as sequence of multiple fixed size pages handled
by ScratchFile
.Modifier and Type | Field 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 static org.apache.commons.logging.Log |
LOG |
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 int |
pageSize |
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 and Description |
---|
ScratchFileBuffer(ScratchFile pageHandler)
Creates a new buffer using pages handled by provided
ScratchFile . |
Modifier and Type | Method and Description |
---|---|
private void |
addPage()
Adds a new page and positions all pointers to start of new page.
|
int |
available()
Returns an estimate of the number of bytes that can be read.
|
private void |
checkClosed()
Checks if this buffer, or the underlying
ScratchFile have been closed,
throwing IOException if so. |
void |
clear()
Clears all data of the buffer.
|
void |
close() |
private boolean |
ensureAvailableBytesInPage(boolean addNewPageIfNeeded)
Ensures the current page has at least one byte left
(
positionInPage in < pageSize ). |
protected void |
finalize()
While calling finalize is normally discouraged we will have to
use it here as long as closing a scratch file buffer is not
done in every case.
|
long |
getPosition()
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 len)
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.
|
private final int pageSize
private ScratchFile pageHandler
private long size
private int currentPagePositionInPageIndexes
pageIndexes
(the nth page within this buffer).private long currentPageOffset
private byte[] currentPage
private int positionInPage
private boolean currentPageContentChanged
true
if current page was changed by a write methodprivate int[] pageIndexes
ScratchFile
)private int pageCount
private static final org.apache.commons.logging.Log LOG
ScratchFileBuffer(ScratchFile pageHandler) throws java.io.IOException
ScratchFile
.pageHandler
- The ScratchFile
managing the pages to be used by this buffer.java.io.IOException
- If getting first page failed.private void checkClosed() throws java.io.IOException
ScratchFile
have been closed,
throwing IOException
if so.java.io.IOException
- If either this buffer, or the underlying ScratchFile
have been closed.private void addPage() throws java.io.IOException
java.io.IOException
- if requesting a new page failspublic long length() throws java.io.IOException
length
in interface RandomAccessRead
java.io.IOException
- If there is an IO error while determining the
length of the data stream.private boolean ensureAvailableBytesInPage(boolean addNewPageIfNeeded) throws java.io.IOException
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.
addNewPageIfNeeded
- if true
it is allowed to add a new page in case
we are currently at end of last buffer pagetrue
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 pagejava.io.IOException
public void write(int b) throws java.io.IOException
write
in interface RandomAccessWrite
b
- The byte to write.java.io.IOException
- If there is an IO error while writing.public void write(byte[] b) throws java.io.IOException
write
in interface RandomAccessWrite
b
- The buffer to get the data from.java.io.IOException
- If there is an error while writing the data.public void write(byte[] b, int off, int len) throws java.io.IOException
write
in interface RandomAccessWrite
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.java.io.IOException
- If there is an error while writing the data.public final void clear() throws java.io.IOException
clear
in interface RandomAccessWrite
java.io.IOException
public long getPosition() throws java.io.IOException
getPosition
in interface RandomAccessRead
RandomAccessRead.read()
(if no more bytes are left it returns a value >= length of source)java.io.IOException
public void seek(long seekToPosition) throws java.io.IOException
seek
in interface RandomAccessRead
seekToPosition
- The position to seek to.java.io.IOException
- If there is an error while seeking.public boolean isClosed()
isClosed
in interface RandomAccessRead
public int peek() throws java.io.IOException
peek
in interface RandomAccessRead
java.io.IOException
- If there is an error reading the next byte.public void rewind(int bytes) throws java.io.IOException
rewind
in interface RandomAccessRead
bytes
- the number of bytes to be seeked backwardsjava.io.IOException
- If there is an error while seekingpublic byte[] readFully(int len) throws java.io.IOException
readFully
in interface RandomAccessRead
len
- the number of bytes to be readjava.io.IOException
- if an I/O error occurs while reading datapublic boolean isEOF() throws java.io.IOException
isEOF
in interface RandomAccessRead
java.io.IOException
- If there is an error reading the next byte.public int available() throws java.io.IOException
available
in interface RandomAccessRead
java.io.IOException
- if this random access has been closedpublic int read() throws java.io.IOException
read
in interface RandomAccessRead
java.io.IOException
- If there is an error while reading the data.public int read(byte[] b) throws java.io.IOException
read
in interface RandomAccessRead
b
- The buffer to write the data to.java.io.IOException
- If there was an error while reading the data.public int read(byte[] b, int off, int len) throws java.io.IOException
read
in interface RandomAccessRead
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.java.io.IOException
- If there was an error while reading the data.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
protected void finalize() throws java.lang.Throwable
COSStream
creates new
buffers without closing the old one - which might still be
used.
Enabling debugging one will see if there are still cases where the buffer is not closed.
finalize
in class java.lang.Object
java.lang.Throwable