lp.test.util
Class PartReader

java.lang.Object
  extended by java.io.Reader
      extended by lp.test.util.PartReader
All Implemented Interfaces:
Closeable, Readable

public class PartReader
extends Reader

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("
"); c = 0; while ((c = pr.read()) != -1) System.out.print((char) c); System.out.println("
"); }
would produce the following output:
<section number="1">first section</section>
<section number="2">second section</section>
<section number="3">third section</section>

Version:
1.0.0
Author:
Martin Slota

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

in

private final Reader in
The underlying Reader instance.


stopChar

private final char stopChar
The character that, when found, is handled as end of input.

Constructor Detail

PartReader

public PartReader(Reader in,
                  char stopChar)
Creates a new instance for the given Reader and with the given stop character.

Parameters:
in - the underlying Reader instance
stopChar - the stop character
Method Detail

close

public void close()
           throws IOException
Closes the underlying Reader.

Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException - if a I/O error occurs while closing the underlying Reader

read

public int read()
         throws IOException
Reads one character from the underlying reader. In case it is the stop character, ignores everything until the end of line and returns -1.

Overrides:
read in class Reader
Returns:
the character read or -1
Throws:
IOException - if a I/O error occurs while reading from the underlying Reader

read

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException
Reads 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.

Specified by:
read in class Reader
Parameters:
cbuf - the character buffer where the characters are stored
off - starting position in the buffer
len - number of characters to read
Returns:
number of characters actually read or -1 if the end of stream or the stop character has been found
Throws:
IOException - if a I/O error occurs while reading from the underlying Reader

ignoreUntilEOL

private void ignoreUntilEOL()
                     throws IOException
Ignores all characters until (and including) the next end of line. If the line ends with "\r\n" and the underlying reader supports marking (see Reader.markSupported(), then both characters are ignored. Otherwise only '\r' is ignored and '\n' stays unread.

Throws:
IOException - if a I/O error occurs while reading from the underlying Reader