Package org.apache.pdfbox.io
Class PushBackInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.PushbackInputStream
-
- org.apache.pdfbox.io.PushBackInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
ByteArrayPushBackInputStream
public class PushBackInputStream extends java.io.PushbackInputStream
A simple subclass that adds a few convience methods.- Version:
- $Revision: 1.6 $
- Author:
- Ben Litchfield
-
-
Constructor Summary
Constructors Constructor Description PushBackInputStream(java.io.InputStream input, int size)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
fillBuffer()
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.long
getOffset()
Returns the current byte offset in the file.boolean
isEOF()
A simple test to see if we are at the end of the stream.int
peek()
This will peek at the next byte.int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
byte[]
readFully(int length)
Reads a given number of bytes from the underlying stream.void
seek(long newOffset)
Allows to seek to another position within stream in case the underlying stream implementsRandomAccessRead
.void
unread(byte[] b)
void
unread(byte[] b, int off, int len)
void
unread(int b)
-
Methods inherited from class java.io.PushbackInputStream
available, close, mark, markSupported, reset, skip
-
-
-
-
Method Detail
-
peek
public int peek() throws java.io.IOException
This will peek at the next byte.- Returns:
- The next byte on the stream, leaving it as available to read.
- Throws:
java.io.IOException
- If there is an error reading the next byte.
-
getOffset
public long getOffset()
Returns the current byte offset in the file.- Returns:
- the int byte offset
-
read
public int read() throws java.io.IOException
- Overrides:
read
in classjava.io.PushbackInputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException
- Overrides:
read
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
read
in classjava.io.PushbackInputStream
- Throws:
java.io.IOException
-
unread
public void unread(int b) throws java.io.IOException
- Overrides:
unread
in classjava.io.PushbackInputStream
- Throws:
java.io.IOException
-
unread
public void unread(byte[] b) throws java.io.IOException
- Overrides:
unread
in classjava.io.PushbackInputStream
- Throws:
java.io.IOException
-
unread
public void unread(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
unread
in classjava.io.PushbackInputStream
- Throws:
java.io.IOException
-
isEOF
public boolean isEOF() throws java.io.IOException
A simple test to see if we are at the end of the stream.- Returns:
- true if we are at the end of the stream.
- Throws:
java.io.IOException
- If there is an error reading the next byte.
-
fillBuffer
public void fillBuffer() throws java.io.IOException
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.- Throws:
java.io.IOException
- If there is an error filling the buffer.
-
readFully
public byte[] readFully(int length) throws java.io.IOException
Reads a given number of bytes from the underlying stream.- Parameters:
length
- the number of bytes to be read- Returns:
- a byte array containing the bytes just read
- Throws:
java.io.IOException
- if an I/O error occurs while reading data
-
seek
public void seek(long newOffset) throws java.io.IOException
Allows to seek to another position within stream in case the underlying stream implementsRandomAccessRead
. Otherwise anIOException
is thrown. Pushback buffer is cleared before seek operation by skipping over all bytes of buffer.- Parameters:
newOffset
- new position within stream from which to read next- Throws:
java.io.IOException
- if underlying stream does not implementRandomAccessRead
or seek operation on underlying stream was not successful
-
-