java.io

Class PushbackReader

Implemented Interfaces:
AutoCloseable, Closeable, Readable

public class PushbackReader
extends FilterReader

This subclass of FilterReader provides the ability to unread data from a stream. It maintains an internal buffer of unread data that is supplied to the next read operation. This is conceptually similar to mark/reset functionality, except that in this case the position to reset the stream to does not need to be known in advance.

The default pushback buffer size one char, but this can be overridden by the creator of the stream.

Field Summary

Fields inherited from class java.io.FilterReader

in

Fields inherited from class java.io.Reader

lock

Constructor Summary

PushbackReader(Reader in)
This method initializes a PushbackReader to read from the specified subordinate Reader with a default pushback buffer size of 1.
PushbackReader(Reader in, int bufsize)
This method initializes a PushbackReader to read from the specified subordinate Reader with the specified buffer size

Method Summary

void
close()
This method closes the stream and frees any associated resources.
void
mark(int read_limit)
This method throws an exception when called since this class does not support mark/reset.
boolean
markSupported()
This method returns false to indicate that it does not support mark/reset functionality.
int
read()
This method reads an unsigned char from the input stream and returns it as an int in the range of 0-65535.
int
read(char[] buffer, int offset, int length)
This method read chars from a stream and stores them into a caller supplied buffer.
boolean
ready()
This method determines whether or not this stream is ready to be read.
void
reset()
This method always throws an IOException in this class because mark/reset functionality is not supported.
long
skip(long num_chars)
This method skips the specified number of chars in the stream.
void
unread(char[] buf)
This method pushes all of the chars in the passed char array into the pushback buffer.
void
unread(char[] buffer, int offset, int length)
This method pushed back chars from the passed in array into the pushback buffer.
void
unread(int b)
This method pushes a single char of data into the pushback buffer.

Methods inherited from class java.io.FilterReader

close, mark, markSupported, read, read, ready, reset, skip

Methods inherited from class java.io.Reader

close, mark, markSupported, read, read, read, read, ready, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

PushbackReader

public PushbackReader(Reader in)
This method initializes a PushbackReader to read from the specified subordinate Reader with a default pushback buffer size of 1.
Parameters:
in - The subordinate stream to read from

PushbackReader

public PushbackReader(Reader in,
                      int bufsize)
This method initializes a PushbackReader to read from the specified subordinate Reader with the specified buffer size
Parameters:
in - The subordinate Reader to read from
bufsize - The pushback buffer size to use

Method Details

close

public void close()
            throws IOException
This method closes the stream and frees any associated resources.
Specified by:
close in interface Closeable
close in interface AutoCloseable
Overrides:
close in interface FilterReader
Throws:
IOException - If an error occurs.

mark

public void mark(int read_limit)
            throws IOException
This method throws an exception when called since this class does not support mark/reset.
Overrides:
mark in interface FilterReader
Parameters:
read_limit - Not used.
Throws:
IOException - Always thrown to indicate mark/reset not supported.

markSupported

public boolean markSupported()
This method returns false to indicate that it does not support mark/reset functionality.
Overrides:
markSupported in interface FilterReader
Returns:
This method returns false to indicate that this class does not support mark/reset functionality

read

public int read()
            throws IOException
This method reads an unsigned char from the input stream and returns it as an int in the range of 0-65535. This method also will return -1 if the end of the stream has been reached. The char returned will be read from the pushback buffer, unless the buffer is empty, in which case the char will be read from the underlying stream.

This method will block until the char can be read.

Overrides:
read in interface FilterReader
Returns:
The char read or -1 if end of stream
Throws:
IOException - If an error occurs

read

public int read(char[] buffer,
                int offset,
                int length)
            throws IOException
This method read chars from a stream and stores them into a caller supplied buffer. It starts storing the data at index offset into the buffer and attempts to read len chars. This method can return before reading the number of chars requested. The actual number of chars read is returned as an int. A -1 is returned to indicate the end of the stream.

This method will block until some data can be read.

This method first reads chars from the pushback buffer in order to satisfy the read request. If the pushback buffer cannot provide all of the chars requested, the remaining chars are read from the underlying stream.

Overrides:
read in interface FilterReader
Parameters:
buffer - The array into which the chars read should be stored
offset - The offset into the array to start storing chars
length - The requested number of chars to read
Returns:
The actual number of chars read, or -1 if end of stream.
Throws:
IOException - If an error occurs.

ready

public boolean ready()
            throws IOException
This method determines whether or not this stream is ready to be read. If it returns false to indicate that the stream is not ready, any attempt to read from the stream could (but is not guaranteed to) block.

This stream is ready to read if there are either chars waiting to be read in the pushback buffer or if the underlying stream is ready to be read.

Overrides:
ready in interface FilterReader
Returns:
true if this stream is ready to be read, false otherwise
Throws:
IOException - If an error occurs

reset

public void reset()
            throws IOException
This method always throws an IOException in this class because mark/reset functionality is not supported.
Overrides:
reset in interface FilterReader
Throws:
IOException - Always thrown for this class

skip

public long skip(long num_chars)
            throws IOException
This method skips the specified number of chars in the stream. It returns the actual number of chars skipped, which may be less than the requested amount.

This method first discards chars from the buffer, then calls the skip method on the underlying Reader to skip additional chars if necessary.

Overrides:
skip in interface FilterReader
Parameters:
num_chars - The requested number of chars to skip
Returns:
The actual number of chars skipped.
Throws:
IOException - If an error occurs

unread

public void unread(char[] buf)
            throws IOException
This method pushes all of the chars in the passed char array into the pushback buffer. These chars are pushed in reverse order so that the next char read from the stream after this operation will be buf[0] followed by buf[1], etc.

If the pushback buffer cannot hold all of the requested chars, an exception is thrown.

Parameters:
buf - The char array to be pushed back
Throws:
IOException - If the pushback buffer is full

unread

public void unread(char[] buffer,
                   int offset,
                   int length)
            throws IOException
This method pushed back chars from the passed in array into the pushback buffer. The chars from buf[offset] to buf[offset + len] are pushed in reverse order so that the next char read from the stream after this operation will be buf[offset] followed by buf[offset + 1], etc.

If the pushback buffer cannot hold all of the requested chars, an exception is thrown.

Parameters:
buffer - The char array to be pushed back
offset - The index into the array where the chars to be push start
length - The number of chars to be pushed.
Throws:
IOException - If the pushback buffer is full

unread

public void unread(int b)
            throws IOException
This method pushes a single char of data into the pushback buffer. The char pushed back is the one that will be returned as the first char of the next read.

If the pushback buffer is full, this method throws an exception.

The argument to this method is an int. Only the low eight bits of this value are pushed back.

Parameters:
b - The char to be pushed back, passed as an int
Throws:
IOException - If the pushback buffer is full.

PushbackReader.java -- An character stream that can unread chars Copyright (C) 1998, 2000, 2001, 2003, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.