A simple subclass that adds a few convience methods.
Method from org.apache.pdfbox.io.PushBackInputStream Detail: |
public void fillBuffer() throws IOException {
int bufferLength = buf.length;
byte[] tmpBuffer = new byte[bufferLength];
int amountRead = 0;
int totalAmountRead = 0;
while( amountRead != -1 && totalAmountRead < bufferLength )
{
amountRead = this.read( tmpBuffer, totalAmountRead, bufferLength - totalAmountRead );
if( amountRead != -1 )
{
totalAmountRead += amountRead;
}
}
this.unread( tmpBuffer, 0, totalAmountRead );
}
This is a method used to fix PDFBox issue 974661, the PDF parsing code needs
to know if there is at least x amount of data left in the stream, but the available()
method returns how much data will be available without blocking. PDFBox is willing to
block to read the data, so we will first fill the internal buffer. |
public int getOffset() {
return offset;
}
Returns the current byte offset in the file. |
public boolean isEOF() throws IOException {
int peek = peek();
return peek == -1;
}
A simple test to see if we are at the end of the stream. |
public int peek() throws IOException {
int result = read();
if( result != -1 )
{
unread( result );
}
return result;
}
This will peek at the next byte. |
public int read() throws IOException {
int retval = super.read();
if (retval != -1)
{
offset++;
}
return retval;
}
|
public int read(byte[] b) throws IOException {
return this.read(b, 0, b.length);
}
|
public int read(byte[] b,
int off,
int len) throws IOException {
int retval = super.read(b, off, len);
if (retval != -1)
{
offset += retval;
}
return retval;
}
|
public byte[] readFully(int length) throws IOException {
byte[] data = new byte[length];
int pos = 0;
while (pos < length)
{
int amountRead = read( data, pos, length - pos );
if (amountRead < 0)
{
throw new EOFException("Premature end of file");
}
pos += amountRead;
}
return data;
}
Reads a given number of bytes from the underlying stream. |
public void unread(int b) throws IOException {
offset--;
super.unread(b);
}
|
public void unread(byte[] b) throws IOException {
this.unread(b, 0, b.length);
}
|
public void unread(byte[] b,
int off,
int len) throws IOException {
if (len > 0)
{
offset -= len;
super.unread(b, off, len);
}
}
|