|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.Reader
lp.test.util.PartReader
public class PartReader
A reader that reads until a certain character is found. After that it ignores
everything until the next line separator and returns -1 (an end of input
sign). It can be used to read 2 sections in a single string (or stream or
file or whatever else that can be processed by some Reader
) separated
from each other by some character (a '$' for instance) as 2 separated things.
For example, the following code:
String s = "first section$\n" + "second section$\n" + "third section"; PartReader pr = new PartReader(new StringReader(s), '$'); int c; for (int i = 0; i < 3; i++) { System.out.print("would produce the following output:"); c = 0; while ((c = pr.read()) != -1) System.out.print((char) c); System.out.println(" "); }
<section number="1">first section</section> <section number="2">second section</section> <section number="3">third section</section>
Field Summary | |
---|---|
private Reader |
in
The underlying Reader instance. |
private char |
stopChar
The character that, when found, is handled as end of input. |
Fields inherited from class java.io.Reader |
---|
lock |
Constructor Summary | |
---|---|
PartReader(Reader in,
char stopChar)
Creates a new instance for the given Reader and with the given
stop character. |
Method Summary | |
---|---|
void |
close()
Closes the underlying Reader . |
private void |
ignoreUntilEOL()
Ignores all characters until (and including) the next end of line. |
int |
read()
Reads one character from the underlying reader. |
int |
read(char[] cbuf,
int off,
int len)
Reads len characters and puts them to cbuf beginning at
position off . |
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 |
---|
private final Reader in
Reader
instance.
private final char stopChar
Constructor Detail |
---|
public PartReader(Reader in, char stopChar)
Reader
and with the given
stop character.
in
- the underlying Reader
instancestopChar
- the stop characterMethod Detail |
---|
public void close() throws IOException
Reader
.
close
in interface Closeable
close
in class Reader
IOException
- if a I/O error occurs while closing the underlying
Reader
public int read() throws IOException
read
in class Reader
IOException
- if a I/O error occurs while reading from the
underlying Reader
public int read(char[] cbuf, int off, int len) throws IOException
len
characters and puts them to cbuf
beginning at
position off
. The stop character is handled differently—if
it is found, the rest of the line is ignored and no more characters are
read.
read
in class Reader
cbuf
- the character buffer where the characters are storedoff
- starting position in the bufferlen
- number of characters to read
IOException
- if a I/O error occurs while reading from the
underlying Reader
private void ignoreUntilEOL() throws IOException
Reader.markSupported()
, then both characters are ignored.
Otherwise only '\r' is ignored and '\n' stays unread.
IOException
- if a I/O error occurs while reading from the
underlying Reader
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |