Packages  This Package  Prev  Next  Index  

§2.19 Class PushbackInputStream

public  class  java.io.PushbackInputStream
    extends  java.io.FilterInputStream  (I-§2.11)
{
        // Fields
    protected int pushBack;	§2.19.1

        // Constructors
    public PushbackInputStream(InputStream  in);	§2.19.2

        // Methods
    public int available();	§2.19.3
    public boolean markSupported();	§2.19.4
    public int read();	§2.19.5
    public int read(byte  bytes[], int  offset, int length);	§2.19.6
    public void unread(int  ch);	§2.19.7
}
This class is an input stream filter that provides a one-byte push back buffer. This feature allows an application to "unread" the last character that it read. The next time that a read is performed on the input stream filter, the "unread" character is re-read.

This functionality is useful in situations where it is useful for a fragment of code to read an indefinite number of data bytes that are delimited by particular byte values; after reading the terminating byte, the code fragment can "unread" it, so that the next read operation on the input stream will re-read the byte that was pushed back.


Fields

pushBack

protected int pushBack
A character that has been "unread" and that will be the next byte read. The value -1 indicates no character in the buffer.

Constructors

PushbackInputStream

public PushbackInputStream(InputStream in)
Constructs a new pushback input stream that reads its input from the specified input stream.
Parameters:
in - the underlying input stream

Methods

available

public int available() throws IOException
Determines the number of bytes that can be read from this input stream without blocking.
The available method of PushbackInputStream calls the available method of its underlying input stream (I-§2.11.1); it returns that value if there is no character that has been pushed back, or that value plus one if there is a character that has been pushed back.

Returns:
the number of bytes that can be read from the input stream without blocking.
Overrides:
available in class FilterInputStream (I-§2.11.3).
Throws
IOException (I-§2.29)
If an I/O error occurs.

markSupported

public boolean markSupported()
Determines if the input stream supports the mark (I-§2.13.4) and reset (I-§2.13.9) methods. The markSupported method of PushbackInputStream always returns false.

Returns:
true if this stream type supports the mark and and reset methods; false otherwise.
Overrides:
markSupported in class FilterInputStream (I-§2.11.6).

read

public int read() throws IOException
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until either input data is available, the end of the stream is detected, or an exception is thrown.
The read method of PushbackInputStream returns the just pushed-back character, if there is one, and otherwise calls the the read method of its underlying input stream (I-§2.13.6) and returns whatever value that method returns.

Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws
IOException (I-§2.29)
If an I/O error occurs.
Overrides:
read in class FilterInputStream (I-§2.11.7).

read

public int read(byte bytes[], int offset, int length) throws IOException
Reads up to len bytes of data from this input stream into an array of bytes. This method blocks until at least one byte of input is available.
Parameters:
b - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes read
Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
Throws
IOException (I-§2.29)
If an I/O error occurs.
Overrides:
read in class FilterInputStream (I-§2.11.9).

unread

public void unread(int ch) throws IOException
Pushes back a character so that it is read again by the next call to the read method on this input stream.
Parameters:
ch - the character to push back.
Throws
IOException (I-§2.29)
If the application attempts to push back a character before the previously pushed back character has been read.

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com