Package totalcross.io
Class LineReader
- java.lang.Object
-
- totalcross.io.LineReader
-
- Direct Known Subclasses:
TokenReader
public class LineReader extends java.lang.ObjectUsed to read lines ending with \r\n (enter/linefeed) or \n (linefeed) from a stream. Consecutive newlines are skipped. This class does not work well with multi-byte characters when the second byte contains the delimiter or enter/linefeed.Here's a sample:
LineReader reader = new LineReader(new File("text.txt",File.READ_WRITE)); String line; while ((line = reader.readLine()) != null) { ... do whatever you want with the line. }Note that this class already uses a buffer for faster detection of the newline. Don't use LineReader with a BufferedStream, it's nonsense and it will throw a warning on the desktop.- Since:
- SuperWaba 5.12
-
-
Field Summary
Fields Modifier and Type Field Description booleandoTrimSet to true to apply a trim in the string that is returned.protected StreamfintmaxTriesThe number of times it tries to read more data if none is available.protected intofsprotected ByteArrayStreamreadBufbooleanreturnEmptyLinesSet to true to receive empty lines (\r\n\r\n returns "","").
-
Constructor Summary
Constructors Constructor Description LineReader(Stream f)Constructs a new LineReader and sets maxTries accordingly to the type of class: 10 if its a Socket or a PortConnector, 0 otherwise.LineReader(Stream f, byte[] buffer, int start, int len)Constructs a new LineReader and sets maxTries accordingly to the type of class: 10 if its a Socket or a PortConnector; 0, otherwise.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StreamgetStream()Returns the Stream attached to this LineReader.java.lang.StringreadLine()Returns the next line available on this stream or null if none.protected booleanreadMore()Read more bytes from the stream.protected voidreuse()Move the buffer to the beginning, in order to preserve the bytes that were not read yet.voidsetStream(Stream f)Change the initial Stream to the attached one, and fetches some data.
-
-
-
Field Detail
-
f
protected Stream f
-
readBuf
protected ByteArrayStream readBuf
-
ofs
protected int ofs
-
maxTries
public int maxTries
The number of times it tries to read more data if none is available. Defaults to 10 if the Stream is a Socket or a PortConnector; 0, otherwise.
-
doTrim
public boolean doTrim
Set to true to apply a trim in the string that is returned.- Since:
- TotalCross 1.23
-
returnEmptyLines
public boolean returnEmptyLines
Set to true to receive empty lines (\r\n\r\n returns "","").- Since:
- TotalCross 1.3
-
-
Constructor Detail
-
LineReader
public LineReader(Stream f) throws IOException
Constructs a new LineReader and sets maxTries accordingly to the type of class: 10 if its a Socket or a PortConnector, 0 otherwise.- Throws:
IOException
-
LineReader
public LineReader(Stream f, byte[] buffer, int start, int len) throws IOException
Constructs a new LineReader and sets maxTries accordingly to the type of class: 10 if its a Socket or a PortConnector; 0, otherwise. The given buffer contents are added to the internal buffer to start reading from them.- Throws:
IOException- Since:
- TotalCross 1.25
-
-
Method Detail
-
setStream
public void setStream(Stream f) throws IOException
Change the initial Stream to the attached one, and fetches some data. Reusing a LineReader throught this method can preserve memory.- Throws:
IOException- Since:
- TotalCross 1.23
-
getStream
public Stream getStream()
Returns the Stream attached to this LineReader.- Since:
- TotalCross 1.23
-
reuse
protected void reuse()
Move the buffer to the beginning, in order to preserve the bytes that were not read yet.
-
readMore
protected boolean readMore() throws IOExceptionRead more bytes from the stream. If there's no data immediately to be read, it causes the current thread to yield the current use of CPU before attempting a new read, up tomaxTriestimes.- Throws:
IOException
-
readLine
public java.lang.String readLine() throws IOExceptionReturns the next line available on this stream or null if none. Empty lines are skipped by default.- Throws:
IOException
-
-