Methods Summary |
---|
public int | available()Returns the number of bytes that are available before this stream will
block. This is the sum of the bytes available in the pushback buffer and
those available from the source stream.
if (buf == null) {
throw new IOException();
}
return buf.length - pos + in.available();
|
public void | close()Closes this stream. This implementation closes the source stream
and releases the pushback buffer.
if (in != null) {
in.close();
in = null;
buf = null;
}
|
public void | mark(int readlimit)Marks the current position in this stream. Setting a mark is not
supported in this class; this implementation does nothing.
return;
|
public boolean | markSupported()Indicates whether this stream supports the {@code mark(int)} and
{@code reset()} methods. {@code PushbackInputStream} does not support
them, so it returns {@code false}.
return false;
|
public int | read()Reads a single byte from this stream and returns it as an integer in the
range from 0 to 255. If the pushback buffer does not contain any
available bytes then a byte from the source input stream is returned.
Blocks until one byte has been read, the end of the source stream is
detected or an exception is thrown.
if (buf == null) {
throw new IOException();
}
// Is there a pushback byte available?
if (pos < buf.length) {
return (buf[pos++] & 0xFF);
}
// Assume read() in the InputStream will return low-order byte or -1
// if end of stream.
return in.read();
|
public int | read(byte[] buffer, int offset, int length)Reads at most {@code length} bytes from this stream and stores them in
the byte array {@code buffer} starting at {@code offset}. Bytes are read
from the pushback buffer first, then from the source stream if more bytes
are required. Blocks until {@code count} bytes have been read, the end of
the source stream is detected or an exception is thrown.
if (buf == null) {
throw new IOException();
}
// BEGIN android-changed
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
// avoid int overflow
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// removed redundant check, used (offset | length) < 0
// instead of (offset < 0) || (length < 0) to safe one operation
if ((offset | length) < 0 || length > buffer.length - offset) {
throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
int copiedBytes = 0, copyLength = 0, newOffset = offset;
// Are there pushback bytes available?
if (pos < buf.length) {
copyLength = (buf.length - pos >= length) ? length : buf.length
- pos;
System.arraycopy(buf, pos, buffer, newOffset, copyLength);
newOffset += copyLength;
copiedBytes += copyLength;
// Use up the bytes in the local buffer
pos += copyLength;
}
// Have we copied enough?
if (copyLength == length) {
return length;
}
int inCopied = in.read(buffer, newOffset, length - copiedBytes);
if (inCopied > 0) {
return inCopied + copiedBytes;
}
if (copiedBytes == 0) {
return inCopied;
}
return copiedBytes;
|
public void | reset()Resets this stream to the last marked position. Resetting the stream is
not supported in this class; this implementation always throws an
{@code IOException}.
throw new IOException();
|
public long | skip(long count)Skips {@code count} bytes in this stream. This implementation skips bytes
in the pushback buffer first and then in the source stream if necessary.
if (in == null) {
throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
}
if (count <= 0) {
return 0;
}
int numSkipped = 0;
if (pos < buf.length) {
numSkipped += (count < buf.length - pos) ? count : buf.length - pos;
pos += numSkipped;
}
if (numSkipped < count) {
numSkipped += in.skip(count - numSkipped);
}
return numSkipped;
|
public void | unread(byte[] buffer, int offset, int length)Pushes a subset of the bytes in {@code buffer} back to this stream. The
subset is defined by the start position {@code offset} within
{@code buffer} and the number of bytes specified by {@code length}. The
bytes are pushed back in such a way that the next byte read from this
stream is {@code buffer[offset]}, then {@code buffer[1]} and so on.
If this stream's internal pushback buffer cannot store the selected
subset of {@code buffer}, an {@code IOException} is thrown. Parts of
{@code buffer} may have already been copied to the pushback buffer when
the exception is thrown.
if (length > pos) {
// Pushback buffer full
throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
}
// avoid int overflow
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// removed redundant check, made implicit null check explicit
// used (offset | length) < 0 instead of (offset < 0) || (length < 0)
// to safe one operation
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((offset | length) < 0 || length > buffer.length - offset) {
throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
for (int i = offset + length - 1; i >= offset; i--) {
unread(buffer[i]);
}
|
public void | unread(int oneByte)Pushes the specified byte {@code oneByte} back to this stream. Only the
least significant byte of the integer {@code oneByte} is pushed back.
This is done in such a way that the next byte read from this stream is
{@code (byte) oneByte}.
If this stream's internal pushback buffer cannot store the byte, an
{@code IOException} is thrown.
if (buf == null) {
throw new IOException();
}
if (pos == 0) {
throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
}
buf[--pos] = (byte) oneByte;
|
public void | unread(byte[] buffer)Pushes all the bytes in {@code buffer} back to this stream. The bytes are
pushed back in such a way that the next byte read from this stream is
buffer[0], then buffer[1] and so on.
If this stream's internal pushback buffer cannot store the entire
contents of {@code buffer}, an {@code IOException} is thrown. Parts of
{@code buffer} may have already been copied to the pushback buffer when
the exception is thrown.
unread(buffer, 0, buffer.length);
|