FileDocCategorySizeDatePackage
PushbackReader.javaAPI DocJava SE 6 API8212Tue Jun 10 00:25:34 BST 2008java.io

PushbackReader

public class PushbackReader extends FilterReader
A character-stream reader that allows characters to be pushed back into the stream.
version
1.21, 06/01/24
author
Mark Reinhold
since
JDK1.1

Fields Summary
private char[]
buf
Pushback buffer
private int
pos
Current position in buffer
Constructors Summary
public PushbackReader(Reader in, int size)
Creates a new pushback reader with a pushback buffer of the given size.

param
in The reader from which characters will be read
param
size The size of the pushback buffer
exception
IllegalArgumentException if size is <= 0

	super(in);
        if (size <= 0) {
            throw new IllegalArgumentException("size <= 0");
        }
	this.buf = new char[size];
	this.pos = size;
    
public PushbackReader(Reader in)
Creates a new pushback reader with a one-character pushback buffer.

param
in The reader from which characters will be read

	this(in, 1);
    
Methods Summary
public voidclose()
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.

exception
IOException If an I/O error occurs

	super.close();
	buf = null;
    
private voidensureOpen()
Checks to make sure that the stream has not been closed.

	if (buf == null)
	    throw new IOException("Stream closed");
    
public voidmark(int readAheadLimit)
Marks the present position in the stream. The mark for class PushbackReader always throws an exception.

exception
IOException Always, since mark is not supported

	throw new IOException("mark/reset not supported");
    
public booleanmarkSupported()
Tells whether this stream supports the mark() operation, which it does not.

	return false;
    
public intread()
Reads a single character.

return
The character read, or -1 if the end of the stream has been reached
exception
IOException If an I/O error occurs

	synchronized (lock) {
	    ensureOpen();
	    if (pos < buf.length)
		return buf[pos++];
	    else
		return super.read();
	}
    
public intread(char[] cbuf, int off, int len)
Reads characters into a portion of an array.

param
cbuf Destination buffer
param
off Offset at which to start writing characters
param
len Maximum number of characters to read
return
The number of characters read, or -1 if the end of the stream has been reached
exception
IOException If an I/O error occurs

	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();
            }
        }
    
public booleanready()
Tells whether this stream is ready to be read.

exception
IOException If an I/O error occurs

	synchronized (lock) {
	    ensureOpen();
	    return (pos < buf.length) || super.ready();
	}
    
public voidreset()
Resets the stream. The reset method of PushbackReader always throws an exception.

exception
IOException Always, since reset is not supported

	throw new IOException("mark/reset not supported");
    
public longskip(long 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.

param
n The number of characters to skip
return
The number of characters actually skipped
exception
IllegalArgumentException If n is negative.
exception
IOException If an I/O error occurs

        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);
        }
    
public voidunread(int 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.

param
c The int value representing a character to be pushed back
exception
IOException If the pushback buffer is full, or if some other I/O error occurs

	synchronized (lock) {
	    ensureOpen();
	    if (pos == 0)
		throw new IOException("Pushback buffer overflow");
	    buf[--pos] = (char) c;
	}
    
public voidunread(char[] cbuf, int off, int 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.

param
cbuf Character array
param
off Offset of first character to push back
param
len Number of characters to push back
exception
IOException If there is insufficient room in the pushback buffer, or if some other I/O error occurs

	synchronized (lock) {
	    ensureOpen();
	    if (len > pos)
		throw new IOException("Pushback buffer overflow");
	    pos -= len;
	    System.arraycopy(cbuf, off, buf, pos, len);
	}
    
public voidunread(char[] cbuf)
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.

param
cbuf Character array to push back
exception
IOException If there is insufficient room in the pushback buffer, or if some other I/O error occurs

	unread(cbuf, 0, cbuf.length);