A character-stream reader that allows characters to be pushed back into the
stream.
| Method from java.io.PushbackReader Detail: |
public void close() throws IOException {
super.close();
buf = null;
}
Closes the stream and releases any system resources associated with
it. Once the stream has been closed, further read(),
unread(), ready(), or skip() invocations will throw an IOException.
Closing a previously closed stream has no effect. |
public void mark(int readAheadLimit) throws IOException {
throw new IOException("mark/reset not supported");
}
Marks the present position in the stream. The mark
for class PushbackReader always throws an exception. |
public boolean markSupported() {
return false;
}
Tells whether this stream supports the mark() operation, which it does
not. |
public int read() throws IOException {
synchronized (lock) {
ensureOpen();
if (pos < buf.length)
return buf[pos++];
else
return super.read();
}
}
Reads a single character. |
public int read(char[] cbuf,
int off,
int len) throws IOException {
synchronized (lock) {
ensureOpen();
try {
if (len < = 0) {
if (len < 0) {
throw new IndexOutOfBoundsException();
} else if ((off < 0) || (off > cbuf.length)) {
throw new IndexOutOfBoundsException();
}
return 0;
}
int avail = buf.length - pos;
if (avail > 0) {
if (len < avail)
avail = len;
System.arraycopy(buf, pos, cbuf, off, avail);
pos += avail;
off += avail;
len -= avail;
}
if (len > 0) {
len = super.read(cbuf, off, len);
if (len == -1) {
return (avail == 0) ? -1 : avail;
}
return avail + len;
}
return avail;
} catch (ArrayIndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException();
}
}
}
Reads characters into a portion of an array. |
public boolean ready() throws IOException {
synchronized (lock) {
ensureOpen();
return (pos < buf.length) || super.ready();
}
}
Tells whether this stream is ready to be read. |
public void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
Resets the stream. The reset method of
PushbackReader always throws an exception. |
public long skip(long n) throws IOException {
if (n < 0L)
throw new IllegalArgumentException("skip value is negative");
synchronized (lock) {
ensureOpen();
int avail = buf.length - pos;
if (avail > 0) {
if (n < = avail) {
pos += n;
return n;
} else {
pos = buf.length;
n -= avail;
}
}
return avail + super.skip(n);
}
}
Skips characters. This method will block until some characters are
available, an I/O error occurs, or the end of the stream is reached. |
public void unread(int c) throws IOException {
synchronized (lock) {
ensureOpen();
if (pos == 0)
throw new IOException("Pushback buffer overflow");
buf[--pos] = (char) c;
}
}
Pushes back a single character by copying it to the front of the
pushback buffer. After this method returns, the next character to be read
will have the value (char)c. |
public void unread(char[] cbuf) throws IOException {
unread(cbuf, 0, cbuf.length);
}
Pushes back an array of characters by copying it to the front of the
pushback buffer. After this method returns, the next character to be
read will have the value cbuf[0], the character after that
will have the value cbuf[1], and so forth. |
public void unread(char[] cbuf,
int off,
int len) throws IOException {
synchronized (lock) {
ensureOpen();
if (len > pos)
throw new IOException("Pushback buffer overflow");
pos -= len;
System.arraycopy(cbuf, off, buf, pos, len);
}
}
Pushes back a portion of an array of characters by copying it to the
front of the pushback buffer. After this method returns, the next
character to be read will have the value cbuf[off], the
character after that will have the value cbuf[off+1], and
so forth. |