FileDocCategorySizeDatePackage
LineReader.javaAPI DocphoneME MR2 API (J2ME)6347Wed May 02 18:00:28 BST 2007com.sun.kvem.midp.pim

LineReader

public class LineReader extends InputStreamReader
Reader that knows how to read non-blank lines. Line may be concatenated according to section 2.1.3 of the vCard 2.1 specification; CRLF followed by an LWSP character is treated as only the LWSP character.

The line terminator is taken as CRLF.

Fields Summary
private final InputStream
in
Input stream.
private final Matcher
matcher
Matcher function.
Constructors Summary
public LineReader(InputStream in, String encoding, Matcher matcher)
Constructs a line reader handler.

param
in an InputStream that must support mark()
param
encoding character encoding of input stream
param
matcher filter function
throws
UnsupportedEncodingException if encoding is not available on the current platform


        this(new MarkableInputStream(in), encoding, matcher);
    
private LineReader(MarkableInputStream in, String encoding, Matcher matcher)
Constructs a line reader.

param
in an InputStream that must support mark()
param
encoding character encoding of input stream
param
matcher filter function
throws
UnsupportedEncodingException if encoding is not available on the current platform


        super(in, encoding);
        this.in = in;
        this.matcher = matcher;
    
Methods Summary
public voidmark(int lookahead)
Sets marker in input stream.

param
lookahead offset to peek ahead
throws
IOException if any read ahead error occurs

        in.mark(lookahead);
    
public booleanmarkSupported()
Checks if mark is supported.

return
true if mark is supported

        return true;
    
public java.lang.StringreadLine()
Reads a non-blank line.

return
a line of text (without line terminators) or null if no more lines are available
throws
IOException if a read error occurs

        StringBuffer sb = new StringBuffer();
        boolean lineIsOnlyWhiteSpace = true;
        boolean done = false;
        for (int i = read(); i != -1 && !done; ) {
            switch (i) {
                case '\r": {
                    // start of a new line. follow through and see if
                    // it is really a new line
                    i = read();
                    if (i != '\n") {
                        throw new IOException("Bad line terminator");
                    }
                    // fall through
                }
                // be generous and accept '\n' alone as a new line.
                // this is against the vCard/vCalendar specifications, but
                // appears to be common practice.
                case '\n": {
                    // lines with only whitespace are treated as empty lines
                    if (lineIsOnlyWhiteSpace) {
                        sb.setLength(0);
                    }
                    mark(1);
                    i = read();
                    reset();
                    switch (i) {
                        case ' ":
                        case '\t":
                            // append this line to the previous one
                            skip(1);
                            break;
                        default:
                            // end of the line
                            if (!lineIsOnlyWhiteSpace) {
                                // return this line
                                done = true;
                            } else {
                                skip(1);
                                // read another line and hope it contains
                                // more than white space
                            }
                            break;
                    }
                    break;
                }
                case ' ":
                case '\t":
                    sb.append((char) i);
                    i = read();
                    break;
                default:
                    sb.append((char) i);
                    if (matcher.match(sb)) {
                        return sb.toString().trim();
                    }
                    i = read();
                    lineIsOnlyWhiteSpace = false;
            }
        }
        if (lineIsOnlyWhiteSpace) {
            return null;
        } else {
            return sb.toString().trim();
        }
    
public voidreset()
Reset the line markers.

throws
IOException if an error occurs accessing the input stream

        in.reset();