Methods Summary |
---|
public void | close()Closes this reader. This implementation closes the source reader
and releases the pushback buffer.
synchronized (lock) {
buf = null;
in.close();
}
|
public void | mark(int readAheadLimit)Marks the current position in this stream. Setting a mark is not
supported in this class; this implementation always throws an
{@code IOException}.
throw new IOException(Msg.getString("K007f")); //$NON-NLS-1$
|
public boolean | markSupported()Indicates whether this reader supports the {@code mark(int)} and
{@code reset()} methods. {@code PushbackReader} does not support them, so
it returns {@code false}.
return false;
|
public int | read()Reads a single character from this reader and returns it as an integer
with the two higher-order bytes set to 0. Returns -1 if the end of the
reader has been reached. If the pushback buffer does not contain any
available characters then a character from the source reader is returned.
Blocks until one character has been read, the end of the source reader is
detected or an exception is thrown.
synchronized (lock) {
if (buf == null) {
throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
}
/* Is there a pushback character available? */
if (pos < buf.length) {
return buf[pos++];
}
/**
* Assume read() in the InputStream will return 2 lowest-order bytes
* or -1 if end of stream.
*/
return in.read();
}
|
public int | read(char[] buffer, int offset, int count)Reads at most {@code length} bytes from this reader and stores them in
byte array {@code buffer} starting at {@code offset}. Characters are
read from the pushback buffer first, then from the source reader if more
bytes are required. Blocks until {@code count} characters have been read,
the end of the source reader is detected or an exception is thrown.
synchronized (lock) {
if (null == buf) {
throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
}
// avoid int overflow
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// made implicit null check explicit, used (offset | count) < 0
// instead of (offset < 0) || (count < 0) to safe one operation
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((offset | count) < 0 || offset > buffer.length - count) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
int copiedChars = 0;
int copyLength = 0;
int newOffset = offset;
/* Are there pushback chars available? */
if (pos < buf.length) {
copyLength = (buf.length - pos >= count) ? count : buf.length
- pos;
System.arraycopy(buf, pos, buffer, newOffset, copyLength);
newOffset += copyLength;
copiedChars += copyLength;
/* Use up the chars in the local buffer */
pos += copyLength;
}
/* Have we copied enough? */
if (copyLength == count) {
return count;
}
int inCopied = in.read(buffer, newOffset, count - copiedChars);
if (inCopied > 0) {
return inCopied + copiedChars;
}
if (copiedChars == 0) {
return inCopied;
}
return copiedChars;
}
|
public boolean | ready()Indicates whether this reader is ready to be read without blocking.
Returns {@code true} if this reader will not block when {@code read} is
called, {@code false} if unknown or blocking will occur.
synchronized (lock) {
if (buf == null) {
throw new IOException(Msg.getString("K0080")); //$NON-NLS-1$
}
return (buf.length - pos > 0 || in.ready());
}
|
public void | reset()Resets this reader to the last marked position. Resetting the reader is
not supported in this class; this implementation always throws an
{@code IOException}.
throw new IOException(Msg.getString("K007f")); //$NON-NLS-1$
|
public long | skip(long count)Skips {@code count} characters in this reader. This implementation skips
characters in the pushback buffer first and then in the source reader if
necessary.
if (count < 0) {
throw new IllegalArgumentException();
}
synchronized (lock) {
if (buf == null) {
throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
}
if (count == 0) {
return 0;
}
long inSkipped;
int availableFromBuffer = buf.length - pos;
if (availableFromBuffer > 0) {
long requiredFromIn = count - availableFromBuffer;
if (requiredFromIn <= 0) {
pos += count;
return count;
}
pos += availableFromBuffer;
inSkipped = in.skip(requiredFromIn);
} else {
inSkipped = in.skip(count);
}
return inSkipped + availableFromBuffer;
}
|
public void | unread(char[] buffer)Pushes all the characters in {@code buffer} back to this reader. The
characters are pushed back in such a way that the next character read
from this reader is buffer[0], then buffer[1] and so on.
If this reader'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);
|
public void | unread(char[] buffer, int offset, int count)Pushes a subset of the characters in {@code buffer} back to this reader.
The subset is defined by the start position {@code offset} within
{@code buffer} and the number of characters 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.
synchronized (lock) {
if (buf == null) {
throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
}
if (count > pos) {
// Pushback buffer full
throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
}
// 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.
// used (offset | count) < 0 instead of (offset < 0) || (count < 0)
// to safe one operation
if ((offset | count) < 0 || offset > buffer.length - count) {
throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
for (int i = offset + count - 1; i >= offset; i--) {
unread(buffer[i]);
}
}
|
public void | unread(int oneChar)Pushes the specified character {@code oneChar} back to this reader. This
is done in such a way that the next character read from this reader is
{@code (char) oneChar}.
If this reader's internal pushback buffer cannot store the character, an
{@code IOException} is thrown.
synchronized (lock) {
if (buf == null) {
throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
}
if (pos == 0) {
throw new IOException(Msg.getString("K007e")); //$NON-NLS-1$
}
buf[--pos] = (char) oneChar;
}
|