|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.io.Reader tribble.io.StackedReader
public class StackedReader
Push-down stack of input reader streams. Manages a stack of Reader objects.
This is useful for implementing readers for source files that contain nested inclusions of other source files (a la the #include directive of C and C++).
Each file inclusion is achieved by pushing the included file as a new reader onto the reader stack. Subsequent input is then read from the reader on the top of the stack. Once the end of the input of that reader is reached, the reader is automatically closed and popped from the stack, and subsequent input is read from the previously pushed reader that is now the new top of the stack. Once all of the reader streams have been popped, the end of the entire stream is reached and no more input is available.
Copyright ©2008 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by
by the United States Department of State as a terrorist, or terrorist
government or agency, to use and distribute this source code provided
that the original copyright notice remains present and unaltered.
Field Summary | |
---|---|
static int |
END_OF_FILE
Special code indicating end-of-file (-2). |
static int |
END_OF_INPUT
Special code indicating end of the input stream (-1). |
(package private) static java.lang.String |
REV
|
Fields inherited from class java.io.Reader |
---|
lock |
Constructor Summary | |
---|---|
StackedReader()
Constructor. |
|
StackedReader(java.io.Reader in)
Constructor. |
|
StackedReader(java.io.Reader in,
boolean eof)
Constructor. |
|
StackedReader(java.io.Reader in,
java.lang.Object lock)
Constructor. |
|
StackedReader(java.io.Reader in,
java.lang.Object lock,
boolean eof)
Constructor. |
Method Summary | |
---|---|
void |
close()
Close the input stream and all of the currently saved (pushed) input streams in the stack. |
java.io.Reader |
getInput()
Retrieve the current input stream from the stack of readers. |
boolean |
isClosed()
Determine if the input stream is closed. |
void |
mark(int readAheadLimit)
This operation is not supported. |
boolean |
markSupported()
Determine whether the stream supports the mark() operation
(which it does not). |
void |
popInput()
Close the current input stream and remove (pop) it from the stack of readers, restoring the previously saved (pushed) stream. |
void |
pushInput(java.io.Reader in)
Insert (push) an input stream onto the stack of readers. |
int |
read()
Read a character from the current input stream. |
int |
read(char[] cbuf,
int off,
int len)
Read characters from the current input stream. |
boolean |
ready()
Determine if the stream is ready to be read. |
void |
reset()
This operation is not supported. |
Methods inherited from class java.io.Reader |
---|
read, read, skip |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
static final java.lang.String REV
public static final int END_OF_INPUT
public static final int END_OF_FILE
Constructor Detail |
---|
public StackedReader()
new StackedReader(null, null, false);
The internal stack of reader streams is empty until method
pushInput()
is called.
public StackedReader(java.io.Reader in)
new StackedReader(in, null, false);
public StackedReader(java.io.Reader in, boolean eof)
new StackedReader(in, null, eof);
public StackedReader(java.io.Reader in, java.lang.Object lock)
new StackedReader(in, lock, false);
public StackedReader(java.io.Reader in, java.lang.Object lock, boolean eof)
in
- Reader input stream.
lock
- An object used to synchronize critical operations on the reader.
This can be null, in which case critical sections will synchronize on the
reader object itself.
eof
- If true, the end of each reader stream is indicated by a special
END_OF_FILE
character code. If false, the end of each stream is
not indicated in any way, but is simply discarded.
java.lang.NullPointerException
- (unchecked)
Thrown if in is null.Method Detail |
---|
public java.io.Reader getInput()
public void pushInput(java.io.Reader in)
Once the end of the current input stream is reached, it will be closed, and the previously saved input stream is restored (popped) from the internal stack, and reading resumes from it.
in
- Input stream.
java.lang.NullPointerException
- (unchecked)
Thrown if in is null.public void popInput() throws java.io.IOException
Under normal operating conditions, this method never needs to be called by client classes.
java.io.IOException
- Thrown if an error occurs while attemping to close the input stream.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in class java.io.Reader
java.io.IOException
- Thrown if an error occurs while attemping to close any of the input
streams. Only the first exception thrown is re-thrown by this method, any
others are ignored.public boolean isClosed()
public int read() throws java.io.IOException
Note that this method is not synchronized.
read
in class java.io.Reader
END_OF_INPUT
if the end of the entire input stream has been
reached.
If the end of the current reader in the stack of readers is reached, and
the eof parameter was true when the input stream was created,
the special code END_OF_FILE
is returned; if eof
was false, the reader stream is closed and a character is read from the
previously saved (pushed) input stream, and no indication that the
end of an individual input stream was reached is returned.
java.io.IOException
- Thrown if an I/O (read) error occurs.public int read(char[] cbuf, int off, int len) throws java.io.IOException
Note that this method is not synchronized.
read
in class java.io.Reader
cbuf
- Character buffer to read into.
off
- Index of the first character in cbuf to read.
len
- Number of characters to read into cbuf
END_OF_INPUT
if the end of the entire input stream has been
reached and there are no more characters to read.
If the end of the current reader in the stack of readers is reached, and
the eof parameter was true when the input stream was created,
cbuf is filled with the characters read up to the end of the
stream, and the next read will return the special code
END_OF_FILE
; subsequent reads after that will read from the
previously saved (pushed) input stream.
If eof was false, the file is closed and characters are then read
from the previously saved (pushed) input stream, and no indication that
the end of an individual input stream was reached is returned.
java.io.IOException
- Thrown if an I/O (read) error occurs.public boolean ready() throws java.io.IOException
ready
in class java.io.Reader
read()
is guaranteed not to block
for input, otherwise false. Note that returning false does not
guarantee that the next read will block.
java.io.IOException
- Thrown if an I/O (read) error occurs.public boolean markSupported()
mark()
operation
(which it does not).
markSupported
in class java.io.Reader
public void mark(int readAheadLimit) throws java.io.IOException
mark
in class java.io.Reader
java.io.IOException
- Always thrown.public void reset() throws java.io.IOException
reset
in class java.io.Reader
java.io.IOException
- Always thrown.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |