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
StringReaderinstead 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 protectedStringCharSource(java.lang.String seq)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcopyTo(CharSink sink)Copies the contents of this source to the given sink.longcopyTo(java.lang.Appendable appendable)Appends the contents of this source to the givenAppendable(such as aWriter).java.io.ReaderopenStream()Opens a newReaderfor 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:CharSourceOpens a newReaderfor 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:
openStreamin classCharSource.CharSequenceCharSource
-
copyTo
public long copyTo(java.lang.Appendable appendable) throws java.io.IOExceptionDescription copied from class:CharSourceAppends the contents of this source to the givenAppendable(such as aWriter). Does not closeappendableif it isCloseable.- Overrides:
copyToin 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:CharSourceCopies the contents of this source to the given sink.- Overrides:
copyToin 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
-
-