Reader, a CharSource is not an open, stateful stream of characters that can be read and closed. Instead,
it is an immutable supplier of Reader instances.
CharSource provides two kinds of methods:
Several methods in this class, such as readLines(), break the contents of the source
into lines. Like BufferedReader, these methods break lines on any of \n, \r or \r\n, do not include the line separator in each line and do not consider there to
be an empty line at the end if the contents are terminated with a line separator.
Any ByteSource containing text encoded with a specific character
encoding may be viewed as a CharSource using ByteSource#asCharSource(Charset).
Note: In general, CharSource is intended to be used for "file-like" sources
that provide readers that are:
length() and read(), will either
block indefinitely or fail if the source creates an infinite reader.
length(), in some
implementations) will prevent further operations from completing as expected.
| Modifier and Type | Class and Description |
|---|---|
| private class | CharSource.AsByteSource
A byte source that reads chars from this source and encodes them as bytes using a charset. |
| private static class | |
| private static class | |
| private static class | |
| private static class | CharSource.StringCharSource
Subclass specialized for string instances. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public ByteSource | asByteSource(Charset charset)
Returns a |
| public static CharSource | Returns: aCharSource containing the concatenated datathe sources to concatenate sources)Concatenates multiple |
| public static CharSource | Returns: aCharSource containing the concatenated datathe sources to concatenate sources)Concatenates multiple |
| public static CharSource | Returns: aCharSource containing the concatenated datathe sources to concatenate sources)Concatenates multiple |
| public long | Returns: the number of characters copiedAppends the contents of this source to the given |
| public long | |
| private long | |
| public static CharSource | |
| public void | forEachLine(Consumer<? super String> action)
Reads all lines of text from this source, running the given |
| public boolean | |
| public long | length()
Returns the length of this source in chars, even if doing so requires opening and traversing an entire stream. |
| public Optional | lengthIfKnown()
Returns the size of this source in chars, if the size can be easily determined without actually opening the data stream. |
| public Stream | |
| public BufferedReader | |
| public abstract Reader | |
| public String | |
| public String | |
| public ImmutableList | |
| public <T extends @Nullable Object> T | readLines(LineProcessor<T> processor)
Reads lines of text from this source, processing each line as it is read using the given |
| public static CharSource |
| CharSource | back to summary |
|---|---|
| protected CharSource() Constructor for use by subclasses. | |
| asByteSource | back to summary |
|---|---|
| public ByteSource asByteSource(Charset charset) Returns a If
| |
| concat | back to summary |
|---|---|
| public static CharSource concat(Iterable<? extends CharSource> sources) Concatenates multiple Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
| |
| concat | back to summary |
|---|---|
| public static CharSource concat(Iterator<? extends CharSource> sources) Concatenates multiple Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream. Note The input
| |
| concat | back to summary |
|---|---|
| public static CharSource concat(CharSource... sources) Concatenates multiple Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
| |
| copyTo | back to summary |
|---|---|
| public long copyTo(Appendable appendable) throws IOException Appends the contents of this source to the given
| |
| copyTo | back to summary |
|---|---|
| public long copyTo(CharSink sink) throws IOException Copies the contents of this source to the given sink.
| |
| countBySkipping | back to summary |
|---|---|
| private long countBySkipping(Reader reader) throws IOException | |
| empty | back to summary |
|---|---|
| public static CharSource empty() Returns an immutable
| |
| forEachLine | back to summary |
|---|---|
| public void forEachLine(Consumer<? super String> action) throws IOException Reads all lines of text from this source, running the given Like
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() throws IOException Returns whether the source has zero chars. The default implementation first checks Note that, in cases where
| |
| length | back to summary |
|---|---|
| public long length() throws IOException 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 The default implementation calls Note that for sources that implement In either case, for mutable sources such as files, a subsequent read may return a different number of chars if the contents are changed.
| |
| lengthIfKnown | back to summary |
|---|---|
| public Optional 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 Additionally, for mutable sources such as
| |
| lines | back to summary |
|---|---|
| public Stream Opens a new 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
Like The caller is responsible for ensuring that the returned stream is closed. For example:
| |
| openBufferedStream | back to summary |
|---|---|
| public BufferedReader openBufferedStream() throws IOException Opens a new The caller is responsible for ensuring that the returned reader is closed.
| |
| openStream | back to summary |
|---|---|
| public abstract Reader openStream() throws IOException Opens a new The caller is responsible for ensuring that the returned reader is closed.
| |
| read | back to summary |
|---|---|
| public String read() throws IOException Reads the contents of this source as a string.
| |
| readFirstLine | back to summary |
|---|---|
| public String readFirstLine() throws IOException Reads the first line of this source as a string. Returns Like
| |
| readLines | back to summary |
|---|---|
| public ImmutableList Reads all the lines of this source as a list of strings. The returned list will be empty if this source is empty. Like
| |
| readLines | back to summary |
|---|---|
| public <T extends @Nullable Object> T readLines(LineProcessor<T> processor) throws IOException Reads lines of text from this source, processing each line as it is read using the given Like
| |
| wrap | back to summary |
|---|---|
| public static CharSource wrap(CharSequence charSequence) Returns a view of the given character sequence as a
| |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public CharSource | asCharSource(Charset charset)
Overrides com. CharSource view of this byte source that decodes bytes read from this source
as characters using the given Charset.
|
| public InputStream | openStream()
Implements abstract com. InputStream for reading from this source.
|
| public String |
| charset | back to summary |
|---|---|
| pack-priv final Charset charset | |
| AsByteSource | back to summary |
|---|---|
| pack-priv AsByteSource(Charset charset) | |
| asCharSource | back to summary |
|---|---|
| public CharSource asCharSource(Charset charset) Overrides com. Doc from com. Returns a If
| |
| openStream | back to summary |
|---|---|
| public InputStream openStream() throws IOException Implements abstract com. Doc from com. Opens a new The caller is responsible for ensuring that the returned stream is closed.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private static final Splitter | |
| protected final CharSequence |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isEmpty()
Overrides com. |
| public long | length()
Overrides com. |
| public Optional | lengthIfKnown()
Returns the size of this source in chars, if the size can be easily determined without actually
opening the data stream.
|
| public Stream | lines()
Overrides com. Stream for reading text one line at a time from this source.
|
| private Iterator | |
| public Reader | openStream()
Implements abstract com. Reader for reading from this source.
|
| public String | read()
Overrides com. |
| public String | readFirstLine()
Reads the first line of this source as a string.
|
| public ImmutableList | readLines()
Overrides com. |
| public <T extends @Nullable Object> T | readLines(LineProcessor<T> processor)
Overrides com. processor.
|
| public String |
| LINE_SPLITTER | back to summary |
|---|---|
| private static final Splitter LINE_SPLITTER | |
| seq | back to summary |
|---|---|
| protected final CharSequence seq | |
| CharSequenceCharSource | back to summary |
|---|---|
| protected CharSequenceCharSource(CharSequence seq) | |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Overrides com. Doc from com. Returns whether the source has zero chars. The default implementation first checks Note that, in cases where
| |
| length | back to summary |
|---|---|
| public long length() Overrides com. Doc from com. 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 The default implementation calls Note that for sources that implement In either case, for mutable sources such as files, a subsequent read may return a different number of chars if the contents are changed.
| |
| lengthIfKnown | back to summary |
|---|---|
| public Optional Overrides com. Doc from com. 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 Additionally, for mutable sources such as
| |
| lines | back to summary |
|---|---|
| public Stream Overrides com. Doc from com. Opens a new 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
Like The caller is responsible for ensuring that the returned stream is closed. For example:
| |
| linesIterator | back to summary |
|---|---|
| private Iterator 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(). | |
| openStream | back to summary |
|---|---|
| public Reader openStream() Implements abstract com. Doc from com. Opens a new The caller is responsible for ensuring that the returned reader is closed.
| |
| read | back to summary |
|---|---|
| public String read() Overrides com. Doc from com. Reads the contents of this source as a string.
| |
| readFirstLine | back to summary |
|---|---|
| public String readFirstLine() Overrides com. Doc from com. Reads the first line of this source as a string. Returns Like
| |
| readLines | back to summary |
|---|---|
| public ImmutableList Overrides com. Doc from com. Reads all the lines of this source as a list of strings. The returned list will be empty if this source is empty. Like
| |
| readLines | back to summary |
|---|---|
| public <T extends @Nullable Object> T readLines(LineProcessor<T> processor) throws IOException Overrides com. Doc from com. Reads lines of text from this source, processing each line as it is read using the given Like
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private final Iterable |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isEmpty()
Overrides com. |
| public long | length()
Overrides com. |
| public Optional | lengthIfKnown()
Returns the size of this source in chars, if the size can be easily determined without actually
opening the data stream.
|
| public Reader | openStream()
Implements abstract com. Reader for reading from this source.
|
| public String |
| sources | back to summary |
|---|---|
| private final Iterable<? extends CharSource> sources | |
| ConcatenatedCharSource | back to summary |
|---|---|
| pack-priv ConcatenatedCharSource(Iterable<? extends CharSource> sources) | |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() throws IOException Overrides com. Doc from com. Returns whether the source has zero chars. The default implementation first checks Note that, in cases where
| |
| length | back to summary |
|---|---|
| public long length() throws IOException Overrides com. Doc from com. 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 The default implementation calls Note that for sources that implement In either case, for mutable sources such as files, a subsequent read may return a different number of chars if the contents are changed.
| |
| lengthIfKnown | back to summary |
|---|---|
| public Optional Overrides com. Doc from com. 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 Additionally, for mutable sources such as
| |
| openStream | back to summary |
|---|---|
| public Reader openStream() throws IOException Implements abstract com. Doc from com. Opens a new The caller is responsible for ensuring that the returned reader is closed.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private static final CharSource. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public String | toString()
Overrides com. |
| INSTANCE | back to summary |
|---|---|
| private static final CharSource. | |
| EmptyCharSource | back to summary |
|---|---|
| private EmptyCharSource() | |
| toString | back to summary |
|---|---|
| public String toString() Overrides com. Doc from java. Returns a string representation of the object. | |
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.
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public long | copyTo(Appendable appendable)
Overrides com. Appendable (such as a Writer).
|
| public long | copyTo(CharSink sink)
Overrides com. |
| public Reader | openStream()
Overrides com. Reader for reading from this source.
|
| StringCharSource | back to summary |
|---|---|
| protected StringCharSource(String seq) | |
| copyTo | back to summary |
|---|---|
| public long copyTo(Appendable appendable) throws IOException Overrides com. Doc from com. Appends the contents of this source to the given
| |
| copyTo | back to summary |
|---|---|
| public long copyTo(CharSink sink) throws IOException Overrides com. Doc from com. Copies the contents of this source to the given sink.
| |
| openStream | back to summary |
|---|---|
| public Reader openStream() Overrides com. Doc from com. Opens a new The caller is responsible for ensuring that the returned reader is closed.
| |