Package totalcross.io

Class LineReader

  • Direct Known Subclasses:
    TokenReader

    public class LineReader
    extends java.lang.Object
    Used 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
      boolean doTrim
      Set to true to apply a trim in the string that is returned.
      protected Stream f  
      int maxTries
      The number of times it tries to read more data if none is available.
      protected int ofs  
      protected ByteArrayStream readBuf  
      boolean returnEmptyLines
      Set 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
      Stream getStream()
      Returns the Stream attached to this LineReader.
      java.lang.String readLine()
      Returns the next line available on this stream or null if none.
      protected boolean readMore()
      Read more bytes from the stream.
      protected void reuse()
      Move the buffer to the beginning, in order to preserve the bytes that were not read yet.
      void setStream​(Stream f)
      Change the initial Stream to the attached one, and fetches some data.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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 IOException
        Read 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 to maxTries times.
        Throws:
        IOException
      • readLine

        public java.lang.String readLine()
                                  throws IOException
        Returns the next line available on this stream or null if none. Empty lines are skipped by default.
        Throws:
        IOException