Class CharSource.CharSequenceCharSource

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static Splitter LINE_SPLITTER  
      protected java.lang.CharSequence seq  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected CharSequenceCharSource​(java.lang.CharSequence seq)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isEmpty()
      Returns whether the source has zero chars.
      long length()
      Returns the length of this source in chars, even if doing so requires opening and traversing an entire stream.
      Optional<java.lang.Long> lengthIfKnown()
      Returns the size of this source in chars, if the size can be easily determined without actually opening the data stream.
      java.util.stream.Stream<java.lang.String> lines()
      Opens a new Stream for reading text one line at a time from this source.
      private java.util.Iterator<java.lang.String> linesIterator()
      Returns an iterator over the lines in the string.
      java.io.Reader openStream()
      Opens a new Reader for reading from this source.
      java.lang.String read()
      Reads the contents of this source as a string.
      java.lang.String readFirstLine()
      Reads the first line of this source as a string.
      ImmutableList<java.lang.String> readLines()
      Reads all the lines of this source as a list of strings.
      <T> T readLines​(LineProcessor<T> processor)
      Reads lines of text from this source, processing each line as it is read using the given processor.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • LINE_SPLITTER

        private static final Splitter LINE_SPLITTER
      • seq

        protected final java.lang.CharSequence seq
    • Constructor Detail

      • CharSequenceCharSource

        protected CharSequenceCharSource​(java.lang.CharSequence seq)
    • Method Detail

      • openStream

        public java.io.Reader openStream()
        Description copied from class: CharSource
        Opens a new Reader for reading from this source. This method returns a new, independent reader each time it is called.

        The caller is responsible for ensuring that the returned reader is closed.

        Specified by:
        openStream in class CharSource
      • read

        public java.lang.String read()
        Description copied from class: CharSource
        Reads the contents of this source as a string.
        Overrides:
        read in class CharSource
      • isEmpty

        public boolean isEmpty()
        Description copied from class: CharSource
        Returns whether the source has zero chars. The default implementation first checks CharSource.lengthIfKnown(), returning true if it's known to be zero and false if it's known to be non-zero. If the length is not known, it falls back to opening a stream and checking for EOF.

        Note that, in cases where lengthIfKnown returns zero, it is possible that chars are actually available for reading. This means that a source may return true from isEmpty() despite having readable content.

        Overrides:
        isEmpty in class CharSource
      • length

        public long length()
        Description copied from class: CharSource
        Returns the length of this source in chars, even if doing so requires opening and traversing an entire stream. To avoid a potentially expensive operation, see CharSource.lengthIfKnown().

        The default implementation calls CharSource.lengthIfKnown() and returns the value if present. If absent, it will fall back to a heavyweight operation that will open a stream, skip to the end of the stream, and return the total number of chars that were skipped.

        Note that for sources that implement CharSource.lengthIfKnown() to provide a more efficient implementation, it is possible that this method will return a different number of chars than would be returned by reading all of the chars.

        In either case, for mutable sources such as files, a subsequent read may return a different number of chars if the contents are changed.

        Overrides:
        length in class CharSource
      • lengthIfKnown

        public Optional<java.lang.Long> lengthIfKnown()
        Description copied from class: CharSource
        Returns the size of this source in chars, if the size can be easily determined without actually opening the data stream.

        The default implementation returns Optional.absent(). Some sources, such as a CharSequence, may return a non-absent value. Note that in such cases, it is possible that this method will return a different number of chars than would be returned by reading all of the chars.

        Additionally, for mutable sources such as StringBuilders, a subsequent read may return a different number of chars if the contents are changed.

        Overrides:
        lengthIfKnown in class CharSource
      • linesIterator

        private java.util.Iterator<java.lang.String> linesIterator()
        Returns an iterator over the lines in the string. If the string ends in a newline, a final empty string is not included, to match the behavior of BufferedReader/LineReader.readLine().
      • lines

        public java.util.stream.Stream<java.lang.String> lines()
        Description copied from class: CharSource
        Opens a new Stream for reading text one line at a time from this source. This method returns a new, independent stream each time it is called.

        The returned stream is lazy and only reads from the source in the terminal operation. If an I/O error occurs while the stream is reading from the source or when the stream is closed, an UncheckedIOException is thrown.

        Like BufferedReader.readLine(), this method considers a line to be a sequence of text that is terminated by (but does not include) one of \r\n, \r or \n. If the source's content does not end in a line termination sequence, it is treated as if it does.

        The caller is responsible for ensuring that the returned stream is closed. For example:

        
         try (Stream<String> lines = source.lines()) {
           lines.map(...)
              .filter(...)
              .forEach(...);
         }
         
        Overrides:
        lines in class CharSource
      • readFirstLine

        public java.lang.String readFirstLine()
        Description copied from class: CharSource
        Reads the first line of this source as a string. Returns null if this source is empty.

        Like BufferedReader.readLine(), this method considers a line to be a sequence of text that is terminated by (but does not include) one of \r\n, \r or \n. If the source's content does not end in a line termination sequence, it is treated as if it does.

        Overrides:
        readFirstLine in class CharSource
      • readLines

        public ImmutableList<java.lang.String> readLines()
        Description copied from class: CharSource
        Reads all the lines of this source as a list of strings. The returned list will be empty if this source is empty.

        Like BufferedReader.readLine(), this method considers a line to be a sequence of text that is terminated by (but does not include) one of \r\n, \r or \n. If the source's content does not end in a line termination sequence, it is treated as if it does.

        Overrides:
        readLines in class CharSource
      • readLines

        public <T> T readLines​(LineProcessor<T> processor)
                        throws java.io.IOException
        Description copied from class: CharSource
        Reads lines of text from this source, processing each line as it is read using the given processor. Stops when all lines have been processed or the processor returns false and returns the result produced by the processor.

        Like BufferedReader.readLine(), this method considers a line to be a sequence of text that is terminated by (but does not include) one of \r\n, \r or \n. If the source's content does not end in a line termination sequence, it is treated as if it does.

        Overrides:
        readLines in class CharSource
        Throws:
        java.io.IOException - if an I/O error occurs while reading from this source or if processor throws an IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object