Package com.google.common.io
Class CharSource.StringCharSource
- java.lang.Object
-
- com.google.common.io.CharSource
-
- com.google.common.io.CharSource.CharSequenceCharSource
-
- com.google.common.io.CharSource.StringCharSource
-
- Direct Known Subclasses:
CharSource.EmptyCharSource
- Enclosing class:
- CharSource
private static class CharSource.StringCharSource extends CharSource.CharSequenceCharSource
Subclass specialized for string instances.Since Strings are immutable and built into the jdk we can optimize some operations
- use
StringReader
instead ofCharSequenceReader
. It is faster since it can useString.getChars(int, int, char[], int)
instead of copying characters one by one withCharSequence.charAt(int)
. - use
Appendable.append(CharSequence)
incopyTo(Appendable)
andcopyTo(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.
-
-
Field Summary
-
Fields inherited from class com.google.common.io.CharSource.CharSequenceCharSource
seq
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
StringCharSource(java.lang.String seq)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
copyTo(CharSink sink)
Copies the contents of this source to the given sink.long
copyTo(java.lang.Appendable appendable)
Appends the contents of this source to the givenAppendable
(such as aWriter
).java.io.Reader
openStream()
Opens a newReader
for reading from this source.-
Methods inherited from class com.google.common.io.CharSource.CharSequenceCharSource
isEmpty, length, lengthIfKnown, lines, read, readFirstLine, readLines, readLines, toString
-
Methods inherited from class com.google.common.io.CharSource
asByteSource, concat, concat, concat, empty, forEachLine, openBufferedStream, wrap
-
-
-
-
Method Detail
-
openStream
public java.io.Reader openStream()
Description copied from class:CharSource
Opens a newReader
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.
- Overrides:
openStream
in classCharSource.CharSequenceCharSource
-
copyTo
public long copyTo(java.lang.Appendable appendable) throws java.io.IOException
Description copied from class:CharSource
Appends the contents of this source to the givenAppendable
(such as aWriter
). Does not closeappendable
if it isCloseable
.- Overrides:
copyTo
in classCharSource
- Returns:
- the number of characters copied
- Throws:
java.io.IOException
- if an I/O error occurs while reading from this source or writing toappendable
-
copyTo
public long copyTo(CharSink sink) throws java.io.IOException
Description copied from class:CharSource
Copies the contents of this source to the given sink.- Overrides:
copyTo
in classCharSource
- Returns:
- the number of characters copied
- Throws:
java.io.IOException
- if an I/O error occurs while reading from this source or writing tosink
-
-