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().
Method from org.apache.pdfbox.io.ByteArrayPushBackInputStream Detail: |
public int available() {
int av = datalen - datapos;
return av > 0 ? av : 0;
}
|
public boolean isEOF() {
return datapos >= datalen;
}
A simple test to see if we are at the end of the stream. |
public int localRead(byte[] buffer,
int off,
int len) {
if (len == 0)
{
return 0; // must return 0 even if at end!
}
else if (datapos >= datalen)
{
return -1;
}
else
{
int newpos = datapos + len;
if (newpos > datalen)
{
newpos = datalen;
len = newpos - datapos;
}
System.arraycopy(data, datapos, buffer, off, len);
datapos = newpos;
return len;
}
}
Read a number of bytes. Internal method that assumes off and len to be
valid. |
public void mark(int readlimit) {
if (false)
{
++readlimit; // avoid unused param warning
}
save = datapos;
}
Save the state of this stream. |
public boolean markSupported() {
return true;
}
Check if mark is supported. |
public int peek() {
try
{
// convert negative values to 128..255
return (data[datapos] + 0x100) & 0xff;
}
catch (ArrayIndexOutOfBoundsException ex)
{
// could check this before, but this is a rare case
// and this method is called sufficiently often to justify this
// optimization
return -1;
}
}
This will peek at the next byte. |
public int read() {
try
{
// convert negative values to 128..255
return (data[datapos++] + 0x100) & 0xff;
}
catch (ArrayIndexOutOfBoundsException ex)
{
// could check this before, but this is a rare case
// and this method is called sufficiently often to justify this
// optimization
datapos = datalen;
return -1;
}
}
|
public int read(byte[] buffer) {
return localRead(buffer, 0, buffer.length);
}
|
public int read(byte[] buffer,
int off,
int len) {
if (len < = 0 || off >= buffer.length)
{
return 0;
}
if (off < 0)
{
off = 0;
}
if (len > buffer.length)
{
len = buffer.length;
}
return localRead(buffer, off, len);
}
|
public void reset() {
datapos = save;
}
Restore the state of this stream to the last saveState call. |
public int seek(int newpos) {
if (newpos < 0)
{
newpos = 0;
}
else if (newpos > datalen)
{
newpos = datalen;
}
int oldpos = pos;
pos = newpos;
return oldpos;
}
Position the stream at a given index. Positioning the stream
at position size() will cause the next call to read() to return -1. |
public int size() {
return datalen;
}
Totally available bytes in the underlying array. |
public long skip(long num) {
if (num < = 0)
{
return 0;
}
else
{
long newpos = datapos + num;
if (newpos >= datalen)
{
num = datalen - datapos;
datapos = datalen;
}
else
{
datapos = (int)newpos;
}
return 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. |
public void unread(int by) throws IOException {
if (datapos == 0)
{
throw new IOException("ByteArrayParserInputStream.unread(int): " +
"cannot unread 1 byte at buffer position " + datapos);
}
--datapos;
data[datapos] = (byte)by;
}
Pushes back a byte.
After this method returns, the next byte to be read will have the value (byte)by. |
public void unread(byte[] buffer) throws IOException {
localUnread(buffer, 0, buffer.length);
}
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. |
public void unread(byte[] buffer,
int off,
int len) throws IOException {
if (len < = 0 || off >= buffer.length)
{
return;
}
if (off < 0)
{
off = 0;
}
if (len > buffer.length)
{
len = buffer.length;
}
localUnread(buffer, off, len);
}
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. |