Package org.apache.pdfbox.io
Class ByteArrayPushBackInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.PushbackInputStream
-
- org.apache.pdfbox.io.PushBackInputStream
-
- org.apache.pdfbox.io.ByteArrayPushBackInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class ByteArrayPushBackInputStream extends PushBackInputStream
PushBackInputStream for byte arrays. The inheritance from PushBackInputStream is only to avoid the introduction of an interface with all PushBackInputStream methods. The parent PushBackInputStream is not used in any way and all methods are overridden. (Thus when adding new methods to PushBackInputStream override them in this class as well!) unread() is limited to the number of bytes already read from this stream (i.e. the current position in the array). This limitation usually poses no problem to a parser, but allows for some optimization since only one array has to be dealt with. Note: This class is not thread safe. Clients must provide synchronization if needed. Note: Calling unread() after mark() will cause (part of) the unread data to be read again after reset(). Thus do not call unread() between mark() and reset().- Version:
- $Revision: 1.2 $
- Author:
- Andreas Weiss (andreas.weiss@switzerland.org)
-
-
Constructor Summary
Constructors Constructor Description ByteArrayPushBackInputStream(byte[] input)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
Available bytes.boolean
isEOF()
A simple test to see if we are at the end of the stream.int
localRead(byte[] buffer, int off, int len)
Read a number of bytes.void
mark(int readlimit)
Save the state of this stream.boolean
markSupported()
Check if mark is supported.int
peek()
This will peek at the next byte.int
read()
Read a byte.int
read(byte[] buffer)
Read a number of bytes.int
read(byte[] buffer, int off, int len)
Read a number of bytes.void
reset()
Restore the state of this stream to the last saveState call.int
seek(int newpos)
Position the stream at a given index.int
size()
Totally available bytes in the underlying array.long
skip(long num)
Skips over and discards n bytes of data from this input stream.void
unread(byte[] buffer)
Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.void
unread(byte[] buffer, int off, int len)
Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.void
unread(int by)
Pushes back a byte.-
Methods inherited from class org.apache.pdfbox.io.PushBackInputStream
fillBuffer, getOffset, readFully, seek
-
-
-
-
Constructor Detail
-
ByteArrayPushBackInputStream
public ByteArrayPushBackInputStream(byte[] input) throws java.io.IOException
Constructor.- Parameters:
input
- Data to read from. Note that calls to unread() will modify this array! If this is not desired, pass a copy.- Throws:
java.io.IOException
- If there is an IO error.
-
-
Method Detail
-
peek
public int peek()
This will peek at the next byte.- Overrides:
peek
in classPushBackInputStream
- Returns:
- The next byte on the stream, leaving it as available to read.
-
isEOF
public boolean isEOF()
A simple test to see if we are at the end of the stream.- Overrides:
isEOF
in classPushBackInputStream
- Returns:
- true if we are at the end of the stream.
-
mark
public void mark(int readlimit)
Save the state of this stream.- Overrides:
mark
in classjava.io.PushbackInputStream
- Parameters:
readlimit
- Has no effect.- See Also:
InputStream.mark(int)
-
markSupported
public boolean markSupported()
Check if mark is supported.- Overrides:
markSupported
in classjava.io.PushbackInputStream
- Returns:
- Always true.
- See Also:
InputStream.markSupported()
-
reset
public void reset()
Restore the state of this stream to the last saveState call.- Overrides:
reset
in classjava.io.PushbackInputStream
- See Also:
InputStream.reset()
-
available
public int available()
Available bytes.- Overrides:
available
in classjava.io.PushbackInputStream
- Returns:
- Available bytes.
- See Also:
InputStream.available()
-
size
public int size()
Totally available bytes in the underlying array.- Returns:
- Available bytes.
-
unread
public void unread(int by) throws java.io.IOException
Pushes back a byte. After this method returns, the next byte to be read will have the value (byte)by.- Overrides:
unread
in classPushBackInputStream
- Parameters:
by
- the int value whose low-order byte is to be pushed back.- Throws:
java.io.IOException
- - If there is not enough room in the buffer for the byte.- See Also:
PushbackInputStream.unread(int)
-
unread
public void unread(byte[] buffer, int off, int len) throws java.io.IOException
Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[off], the byte after that will have the value b[off+1], and so forth.- Overrides:
unread
in classPushBackInputStream
- Parameters:
buffer
- the byte array to push back.off
- the start offset of the data.len
- the number of bytes to push back.- Throws:
java.io.IOException
- If there is not enough room in the pushback buffer for the specified number of bytes.- See Also:
PushbackInputStream.unread(byte[], int, int)
-
unread
public void unread(byte[] buffer) throws java.io.IOException
Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value buffer[0], the byte after that will have the value buffer[1], and so forth.- Overrides:
unread
in classPushBackInputStream
- Parameters:
buffer
- the byte array to push back.- Throws:
java.io.IOException
- If there is not enough room in the pushback buffer for the specified number of bytes.- See Also:
PushbackInputStream.unread(byte[])
-
read
public int read()
Read a byte.- Overrides:
read
in classPushBackInputStream
- Returns:
- Byte read or -1 if no more bytes are available.
- See Also:
InputStream.read()
-
read
public int read(byte[] buffer)
Read a number of bytes.- Overrides:
read
in classPushBackInputStream
- Parameters:
buffer
- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
- See Also:
InputStream.read(byte[])
-
read
public int read(byte[] buffer, int off, int len)
Read a number of bytes.- Overrides:
read
in classPushBackInputStream
- Parameters:
buffer
- the buffer into which the data is read.off
- the start offset in array buffer at which the data is written.len
- the maximum number of bytes to read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
- See Also:
InputStream.read(byte[], int, int)
-
localRead
public int localRead(byte[] buffer, int off, int len)
Read a number of bytes. Internal method that assumes off and len to be valid.- Parameters:
buffer
- the buffer into which the data is read.off
- the start offset in array buffer at which the data is written.len
- the maximum number of bytes to read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
- See Also:
InputStream.read(byte[], int, int)
-
skip
public long skip(long num)
Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.- Overrides:
skip
in classjava.io.PushbackInputStream
- Parameters:
num
- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- See Also:
InputStream.skip(long)
-
seek
public int seek(int newpos)
Position the stream at a given index. Positioning the stream at position size() will cause the next call to read() to return -1.- Parameters:
newpos
- Position in the underlying array. A negative value will be interpreted as 0, a value greater than size() as size().- Returns:
- old position.
-
-