private static class CharSource.StringCharSource extends CharSource.CharSequenceCharSource
Since Strings are immutable and built into the jdk we can optimize some operations
StringReader
instead of CharSequenceReader
. It is faster since it can
use String.getChars(int, int, char[], int)
instead of copying characters one by
one with CharSequence.charAt(int)
.
Appendable.append(CharSequence)
in copyTo(Appendable)
and copyTo(CharSink)
. We know this is correct since strings are immutable and so the length
can't change, and it is faster because many writers and appendables are optimized for
appending string instances.
seq
Modifier | Constructor and Description |
---|---|
protected |
StringCharSource(java.lang.String seq) |
Modifier and Type | Method and Description |
---|---|
long |
copyTo(java.lang.Appendable appendable)
Appends the contents of this source to the given
Appendable (such as a Writer ). |
long |
copyTo(CharSink sink)
Copies the contents of this source to the given sink.
|
java.io.Reader |
openStream()
Opens a new
Reader for reading from this source. |
isEmpty, length, lengthIfKnown, lines, read, readFirstLine, readLines, readLines, toString
asByteSource, concat, concat, concat, empty, forEachLine, openBufferedStream, wrap
public java.io.Reader openStream()
CharSource
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.
openStream
in class CharSource.CharSequenceCharSource
public long copyTo(java.lang.Appendable appendable) throws java.io.IOException
CharSource
Appendable
(such as a Writer
).
Does not close appendable
if it is Closeable
.copyTo
in class CharSource
java.io.IOException
- if an I/O error occurs while reading from this source or writing to appendable
public long copyTo(CharSink sink) throws java.io.IOException
CharSource
copyTo
in class CharSource
java.io.IOException
- if an I/O error occurs while reading from this source or writing to sink