Class CharSource.CharSequenceCharSource
- java.lang.Object
-
- com.google.common.io.CharSource
-
- com.google.common.io.CharSource.CharSequenceCharSource
-
- Direct Known Subclasses:
CharSource.EmptyCharSource
- Enclosing class:
- CharSource
private static class CharSource.CharSequenceCharSource extends CharSource
-
-
Field Summary
Fields Modifier and Type Field Description private static SplitterLINE_SPLITTERprivate java.lang.CharSequenceseq
-
Constructor Summary
Constructors Modifier Constructor Description protectedCharSequenceCharSource(java.lang.CharSequence seq)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanisEmpty()Returns whether the source has zero chars.longlength()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.private java.lang.Iterable<java.lang.String>lines()Returns an iterable over the lines in the string.java.io.ReaderopenStream()Opens a newReaderfor reading from this source.java.lang.Stringread()Reads the contents of this source as a string.java.lang.StringreadFirstLine()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> TreadLines(LineProcessor<T> processor)Reads lines of text from this source, processing each line as it is read using the givenprocessor.java.lang.StringtoString()-
Methods inherited from class com.google.common.io.CharSource
asByteSource, concat, concat, concat, copyTo, copyTo, empty, openBufferedStream, wrap
-
-
-
-
Field Detail
-
LINE_SPLITTER
private static final Splitter LINE_SPLITTER
-
seq
private final java.lang.CharSequence seq
-
-
Method Detail
-
openStream
public java.io.Reader openStream()
Description copied from class:CharSourceOpens a newReaderfor reading from this source. This method should return a new, independent reader each time it is called.The caller is responsible for ensuring that the returned reader is closed.
- Specified by:
openStreamin classCharSource
-
read
public java.lang.String read()
Description copied from class:CharSourceReads the contents of this source as a string.- Overrides:
readin classCharSource
-
isEmpty
public boolean isEmpty()
Description copied from class:CharSourceReturns whether the source has zero chars. The default implementation returns true ifCharSource.lengthIfKnown()returns zero, falling back to opening a stream and checking for EOF if the length is not known.Note that, in cases where
lengthIfKnownreturns zero, it is possible that chars are actually available for reading. This means that a source may returntruefromisEmpty()despite having readable content.- Overrides:
isEmptyin classCharSource
-
length
public long length()
Description copied from class:CharSourceReturns the length of this source in chars, even if doing so requires opening and traversing an entire stream. To avoid a potentially expensive operation, seeCharSource.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,skipto 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:
lengthin classCharSource
-
lengthIfKnown
public Optional<java.lang.Long> lengthIfKnown()
Description copied from class:CharSourceReturns 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 aCharSequence, 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:
lengthIfKnownin classCharSource
-
lines
private java.lang.Iterable<java.lang.String> lines()
Returns an iterable 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().
-
readFirstLine
public java.lang.String readFirstLine()
Description copied from class:CharSourceReads the first line of this source as a string. Returnsnullif this source is empty.Like
BufferedReader, this method breaks lines on any of\n,\ror\r\n, does not include the line separator in the returned line and does not consider there to be an extra empty line at the end if the content is terminated with a line separator.- Overrides:
readFirstLinein classCharSource
-
readLines
public ImmutableList<java.lang.String> readLines()
Description copied from class:CharSourceReads all the lines of this source as a list of strings. The returned list will be empty if this source is empty.Like
BufferedReader, this method breaks lines on any of\n,\ror\r\n, does not include the line separator in the returned lines and does not consider there to be an extra empty line at the end if the content is terminated with a line separator.- Overrides:
readLinesin classCharSource
-
readLines
public <T> T readLines(LineProcessor<T> processor) throws java.io.IOException
Description copied from class:CharSourceReads lines of text from this source, processing each line as it is read using the givenprocessor. Stops when all lines have been processed or the processor returnsfalseand returns the result produced by the processor.Like
BufferedReader, this method breaks lines on any of\n,\ror\r\n, does not include the line separator in the lines passed to theprocessorand does not consider there to be an extra empty line at the end if the content is terminated with a line separator.- Overrides:
readLinesin classCharSource- Throws:
java.io.IOException- if an I/O error occurs in the process of reading from this source or ifprocessorthrows anIOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-