Package libcore.io
Class StrictLineReader
- java.lang.Object
-
- libcore.io.StrictLineReader
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class StrictLineReader extends Object implements Closeable
Buffers input from anInputStreamfor reading lines. This class is used for buffered reading of lines. For purposes of this class, a line ends with "\n" or "\r\n". End of input is reported by throwingEOFException. Unterminated line at end of input is invalid and will be ignored, the caller may usehasUnterminatedLine()to detect it after catching theEOFException. This class is intended for reading input that strictly consists of lines, such as line-based cache entries or cache journal. Unlike theBufferedReaderwhich in conjunction withInputStreamReaderprovides similar functionality, this class uses different end-of-input reporting and a more restrictive definition of a line. This class supports only charsets that encode '\r' and '\n' as a single byte with value 13 and 10, respectively, and the representation of no other character contains these values. We currently check in constructor that the charset is one of US-ASCII, UTF-8 and ISO-8859-1. The default charset is US_ASCII.
-
-
Constructor Summary
Constructors Constructor Description StrictLineReader(InputStream in)Constructs a newStrictLineReaderwith the default capacity and charset.StrictLineReader(InputStream in, int capacity)Constructs a newLineReaderwith the specified capacity and the default charset.StrictLineReader(InputStream in, int capacity, Charset charset)Constructs a newLineReaderwith the specified capacity and charset.StrictLineReader(InputStream in, Charset charset)Constructs a newLineReaderwith the specified charset and the default capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the reader by closing the underlyingInputStreamand marking this reader as closed.booleanhasUnterminatedLine()Check whether there was an unterminated line at end of input after the line reader reported end-of-input with EOFException.intreadInt()Read anintfrom a line containing its decimal representation.StringreadLine()Reads the next line.
-
-
-
Constructor Detail
-
StrictLineReader
public StrictLineReader(InputStream in)
Constructs a newStrictLineReaderwith the default capacity and charset.- Parameters:
in- theInputStreamto read data from.- Throws:
NullPointerException- ifinis null.
-
StrictLineReader
public StrictLineReader(InputStream in, int capacity)
Constructs a newLineReaderwith the specified capacity and the default charset.- Parameters:
in- theInputStreamto read data from.capacity- the capacity of the buffer.- Throws:
NullPointerException- ifinis null.IllegalArgumentException- for negative or zerocapacity.
-
StrictLineReader
public StrictLineReader(InputStream in, Charset charset)
Constructs a newLineReaderwith the specified charset and the default capacity.- Parameters:
in- theInputStreamto read data from.charset- the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 is supported.- Throws:
NullPointerException- ifinorcharsetis null.IllegalArgumentException- if the specified charset is not supported.
-
StrictLineReader
public StrictLineReader(InputStream in, int capacity, Charset charset)
Constructs a newLineReaderwith the specified capacity and charset.- Parameters:
in- theInputStreamto read data from.capacity- the capacity of the buffer.charset- the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 is supported.- Throws:
NullPointerException- ifinorcharsetis null.IllegalArgumentException- ifcapacityis negative or zero or the specified charset is not supported.
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses the reader by closing the underlyingInputStreamand marking this reader as closed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- for errors when closing the underlyingInputStream.
-
readLine
public String readLine() throws IOException
Reads the next line. A line ends with"\n"or"\r\n", this end of line marker is not included in the result.- Returns:
- the next line from the input.
- Throws:
IOException- for underlyingInputStreamerrors.EOFException- for the end of source stream.
-
readInt
public int readInt() throws IOExceptionRead anintfrom a line containing its decimal representation.- Returns:
- the value of the
intfrom the next line. - Throws:
IOException- for underlyingInputStreamerrors or conversion error.EOFException- for the end of source stream.
-
hasUnterminatedLine
public boolean hasUnterminatedLine()
Check whether there was an unterminated line at end of input after the line reader reported end-of-input with EOFException. The value is meaningless in any other situation.- Returns:
- true if there was an unterminated line at end of input.
-
-