public abstract class CharSource extends Object
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.
| Modifier | Constructor and Description |
|---|---|
protected |
CharSource()
Constructor for use by subclasses.
|
| Modifier and Type | Method and Description |
|---|---|
static CharSource |
concat(CharSource
Concatenates multiple
CharSource instances into a single source.
|
static CharSource |
concat(Iterable
Concatenates multiple
CharSource instances into a single source.
|
static CharSource |
concat(Iterator
Concatenates multiple
CharSource instances into a single source.
|
long |
copyTo(Appendable
Appends the contents of this source to the given
Appendable (such as a
Writer).
|
long |
copyTo(CharSink
Copies the contents of this source to the given sink.
|
static CharSource |
empty()
Returns an immutable
CharSource that contains no characters.
|
boolean |
isEmpty()
Returns whether the source has zero chars.
|
BufferedReader |
openBufferedStream()
Opens a new
BufferedReader for reading from this source.
|
abstract Reader |
openStream()
Opens a new
Reader for reading from this source.
|
String |
read()
Reads the contents of this source as a string.
|
String |
readFirstLine()
Reads the first link of this source as a string.
|
ImmutableList |
readLines()
Reads all the lines of this source as a list of strings.
|
<T> T |
readLines(LineProcessor
Reads lines of text from this source, processing each line as it is read using the given
processor.
|
static CharSource |
wrap(CharSequence
Returns a view of the given character sequence as a
CharSource.
|
public abstract ReaderopenStream() throws IOException
Reader for 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.
IOException - if an I/O error occurs in the process of opening the reader
public BufferedReaderopenBufferedStream() throws IOException
BufferedReader for 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.
IOException - if an I/O error occurs in the process of opening the reader
public long copyTo(Appendableappendable) throws IOException
Appendable (such as a
Writer). Does not close
appendable if it is
Closeable.
IOException - if an I/O error occurs in the process of reading from this source or writing to
appendable
public long copyTo(CharSinksink) throws IOException
IOException - if an I/O error occurs in the process of reading from this source or writing to
sink
public Stringread() throws IOException
IOException - if an I/O error occurs in the process of reading from this source
public StringreadFirstLine() throws IOException
null if this source is empty.
Like BufferedReader, this method breaks lines on any of \n, \r or \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.
IOException - if an I/O error occurs in the process of reading from this source
public ImmutableList<String > readLines() throws IOException
Like BufferedReader, this method breaks lines on any of \n, \r or \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.
IOException - if an I/O error occurs in the process of reading from this source
@Beta public <T> T readLines(LineProcessor<T> processor) throws IOException
processor. Stops when all lines have been processed or the processor returns
false and returns the result produced by the processor.
Like BufferedReader, this method breaks lines on any of \n, \r or \r\n, does not include the line separator in the lines passed to the processor and does not consider there to be an extra empty line at the end if the content is terminated with a line separator.
IOException - if an I/O error occurs in the process of reading from this source or if
processor throws an
IOException
public boolean isEmpty()
throws IOException
IOException - if an I/O error occurs
public static CharSourceconcat(Iterable <? extends CharSource > sources)
CharSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.
Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
sources - the sources to concatenate
CharSource containing the concatenated data
public static CharSourceconcat(Iterator <? extends CharSource > sources)
CharSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.
Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
Note: The input Iterator will be copied to an ImmutableList when this method is called. This will fail if the iterator is infinite and may cause problems if the iterator eagerly fetches data for each source when iterated (rather than producing sources that only load data through their streams). Prefer using the concat(Iterable) overload if possible.
sources - the sources to concatenate
CharSource containing the concatenated data
NullPointerException - if any of
sources is
null
public static CharSourceconcat(CharSource ... sources)
CharSource instances into a single source. Streams returned from the source will contain the concatenated data from the streams of the underlying sources.
Only one underlying stream will be open at a time. Closing the concatenated stream will close the open underlying stream.
sources - the sources to concatenate
CharSource containing the concatenated data
NullPointerException - if any of
sources is
null
public static CharSourcewrap(CharSequence charSequence)
CharSource. The behavior of the returned
CharSource and any
Reader instances created by it is unspecified if the
charSequence is mutated while it is being read, so don't do that.
CharStreams.asCharSource(String))
public static CharSourceempty()
CharSource that contains no characters.