xtc.util
Class NestedReader

java.lang.Object
  extended by java.io.Reader
      extended by xtc.util.NestedReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class NestedReader
extends java.io.Reader

Implementation of a nested reader. A nested reader combines several streams into a single stream. It starts by reading from a main stream. Additional streams are added through the insert(Reader) and insert(Reader,NestedReader.EOFListener) methods and are consumed completely before returning to read from the previous stream. Note that inserted streams are automatically closed after having being consumed. Further note that closing a nested reader closes all streams currently associated with that nested reader.

Version:
$Revision: 1.5 $
Author:
Robert Grimm

Nested Class Summary
static interface NestedReader.EOFListener
          Event listener to provide notification a stream has reached its end.
 
Field Summary
protected  boolean closed
          Flag for whether this nested reader has been closed.
protected  NestedReader.EOFListener listener
          The corresponding end-of-file listener.
protected  java.util.LinkedList<NestedReader.EOFListener> listenerStack
          The stack of listeners, with the most recently added listener at the front.
protected  java.io.Reader reader
          The current character stream.
protected  java.util.LinkedList<java.io.Reader> readerStack
          The stack of readers, with the most recently added stream at the front.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
NestedReader(java.io.Reader in)
          Create a new nested reader.
 
Method Summary
 void close()
           
 void insert(java.io.Reader in)
          Insert the specified character stream.
 void insert(java.io.Reader in, NestedReader.EOFListener eof)
          Insert the specified character stream.
 java.io.Reader open(java.lang.String file)
          Open the specified file.
 int read()
           
 int read(char[] cbuf, int off, int len)
           
 
Methods inherited from class java.io.Reader
mark, markSupported, read, read, ready, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

closed

protected boolean closed
Flag for whether this nested reader has been closed.


reader

protected java.io.Reader reader
The current character stream.


listener

protected NestedReader.EOFListener listener
The corresponding end-of-file listener.


readerStack

protected java.util.LinkedList<java.io.Reader> readerStack
The stack of readers, with the most recently added stream at the front.


listenerStack

protected java.util.LinkedList<NestedReader.EOFListener> listenerStack
The stack of listeners, with the most recently added listener at the front.

Constructor Detail

NestedReader

public NestedReader(java.io.Reader in)
Create a new nested reader.

Parameters:
in - The main stream.
Method Detail

open

public java.io.Reader open(java.lang.String file)
                    throws java.io.IOException
Open the specified file. The implementation of this method simply creates a new file reader with the specified file name.

Parameters:
file - The file name.
Returns:
The corresponding character stream.
Throws:
java.io.IOException - Signals an I/O error.

insert

public void insert(java.io.Reader in)
            throws java.io.IOException
Insert the specified character stream. After reading all characters from the specified stream, this nested reader silently returns to reading characters from the current stream.

Parameters:
in - The stream.
Throws:
java.io.IOException - Signals an I/O error.

insert

public void insert(java.io.Reader in,
                   NestedReader.EOFListener eof)
            throws java.io.IOException
Insert the specified character stream. After reading all characters from the specified stream, but before returning to read characters from the current stream, this nested reader notifies the specified listener.

Parameters:
in - The stream.
eof - The listener to be notified when the specified stream has been consumed.
Throws:
java.io.IOException - Signals an I/O error.

read

public int read()
         throws java.io.IOException
Overrides:
read in class java.io.Reader
Throws:
java.io.IOException

read

public int read(char[] cbuf,
                int off,
                int len)
         throws java.io.IOException
Specified by:
read in class java.io.Reader
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Reader
Throws:
java.io.IOException


Copyright © 2012. All Rights Reserved.