Interface CharSource
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A readable source of characters, such as a text file or a string, that opens a new reader each time it is read.
Unlike a Reader, a source is not an open resource: it can be kept, passed around and read any number of
times, and each read method opens a reader and closes it before returning. Only openReader() and
lines() leave something for the caller to close.
The only abstract method is openReader(), so a lambda makes a source:
CharSource source = () -> CharReader.of(new InputStreamReader(System.in, StandardCharsets.UTF_8));
List<String> lines = CharSource.of(file, StandardCharsets.UTF_8).readLines();
Lines end at a line feed (\n), a carriage return (\r), or a carriage return followed by a line
feed, as with BufferedReader.readLine(). Lines never include their line terminator.
Implementations may override the other methods with faster ones, such as lengthIfKnown() for a source
that knows its length without reading it.
- Since:
- 0.1.1
-
Method Summary
Modifier and TypeMethodDescriptiondefault ByteSourceReturns a source of the bytes that encode these characters in the current default charset.default ByteSourceasByteSource(Charset charset) Returns a source of the bytes that encode these characters.static CharSourceconcat(CharSource... sources) Returns a source that reads several sources one after the other.static CharSourceconcat(Iterable<? extends CharSource> sources) Returns a source that reads several sources one after the other.default longCopies the characters of this source to a sink.default longcopyTo(Appendable out) Copies the characters of this source to an appendable, which is left open if it is a stream.static CharSourceempty()Returns a source with no characters.default voidforEachLine(Consumer<? super String> action) Reads the lines of this source one at a time, without keeping them.default booleanisEmpty()Tells whether this source has no characters, reading at most one character to find out.default longlength()Returns the number of characters in this source, reading them all iflengthIfKnown()does not know it.default OptionalLongReturns the number of characters in this source if it is known without reading them.lines()Opens a stream of the lines of this source, read as the stream is consumed.static CharSourceReturns a source that reads the text a URI points to, through the URL it converts to, encoded in the current default charset.static CharSourceReturns a source that reads the text a URI points to, through the URL it converts to.static CharSourceReturns a source that reads the text a URL points to, such as a resource fromClass.getResource(String), encoded in the current default charset.static CharSourceReturns a source that reads the text a URL points to, such as a resource fromClass.getResource(String).static CharSourceof(Path file, Charset charset, OpenOption... options) Returns a source that reads a text file.static CharSourceof(Path file, OpenOption... options) Returns a source that reads a text file encoded in the current default charset.Opens a new reader on the characters of this source.default Stringread()Reads all the characters of this source.Reads the first line of this source.Reads all the lines of this source.static CharSourcewrap(CharSequence chars) Returns a source that reads a character sequence.
-
Method Details
-
empty
-
wrap
Returns a source that reads a character sequence. The sequence is not copied: changes to it, as in aStringBuilder, show in the reads that follow. It must not change while a reader of this source is open.- Parameters:
chars- the sequence to read- Returns:
- a source of
chars
-
of
Returns a source that reads a text file encoded in the current default charset.- Parameters:
file- the file to readoptions- how to open the file, as forFiles.newInputStream(Path, OpenOption...)- Returns:
- a source of the content of
file - Since:
- 0.1.2
- See Also:
-
of
Returns a source that reads a text file.- Parameters:
file- the file to readcharset- the charset the file is encoded inoptions- how to open the file, as forFiles.newInputStream(Path, OpenOption...)- Returns:
- a source of the content of
file - See Also:
-
of
Returns a source that reads the text a URL points to, such as a resource fromClass.getResource(String), encoded in the current default charset.- Parameters:
url- the URL to read- Returns:
- a source of the content at
url - Since:
- 0.1.2
- See Also:
-
of
Returns a source that reads the text a URL points to, such as a resource fromClass.getResource(String). LikeByteSource.of(URL), it is meant for local resources: it opens the URL with no timeout.- Parameters:
url- the URL to readcharset- the charset the text is encoded in- Returns:
- a source of the content at
url - See Also:
-
of
Returns a source that reads the text a URI points to, through the URL it converts to, encoded in the current default charset.- Parameters:
uri- the URI to read- Returns:
- a source of the content at
uri - Throws:
IllegalArgumentException- ifuriis not absolute, or has a scheme no URL protocol handler supports- Since:
- 0.1.2
- See Also:
-
of
Returns a source that reads the text a URI points to, through the URL it converts to.- Parameters:
uri- the URI to readcharset- the charset the text is encoded in- Returns:
- a source of the content at
uri - Throws:
IllegalArgumentException- ifuriis not absolute, or has a scheme no URL protocol handler supports- See Also:
-
concat
Returns a source that reads several sources one after the other. Each source is opened only when the previous one is read to its end.- Parameters:
sources- the sources to read- Returns:
- a source of the content of
sources, in order
-
concat
Returns a source that reads several sources one after the other. Each source is opened only when the previous one is read to its end. The sources are copied: later changes tosourceshave no effect.- Parameters:
sources- the sources to read- Returns:
- a source of the content of
sources, in order
-
openReader
Opens a new reader on the characters of this source. The caller must close it. For APIs that take aReader,CharReader.asReader()reads through it.- Returns:
- a new reader
- Throws:
IOException- if the reader cannot be opened- Since:
- 0.2.0
-
isEmpty
Tells whether this source has no characters, reading at most one character to find out.- Returns:
- whether this source is empty
- Throws:
IOException- if this source cannot be read
-
lengthIfKnown
Returns the number of characters in this source if it is known without reading them. A source that changes may then read a different number of characters.- Returns:
- the number of characters, or nothing if it is unknown
-
length
Returns the number of characters in this source, reading them all iflengthIfKnown()does not know it.- Returns:
- the number of characters
- Throws:
IOException- if this source cannot be read
-
read
Reads all the characters of this source.- Returns:
- the characters of this source
- Throws:
IOException- if this source cannot be read
-
readFirstLine
Reads the first line of this source.- Returns:
- the first line, or nothing if this source is empty
- Throws:
IOException- if this source cannot be read
-
readLines
Reads all the lines of this source.- Returns:
- the lines of this source, in an unmodifiable list
- Throws:
IOException- if this source cannot be read
-
lines
Opens a stream of the lines of this source, read as the stream is consumed. The caller must close the stream, which closes what it reads from. A failure to read during a terminal operation is thrown as anUncheckedIOException.try (Stream<String> lines = source.lines()) { return lines.filter(line -> !line.isBlank()).count(); }- Returns:
- a new stream of the lines of this source; it is the caller's responsibility to close it after use
- Throws:
IOException- if this source cannot be opened
-
forEachLine
Reads the lines of this source one at a time, without keeping them.- Parameters:
action- what to do with each line- Throws:
IOException- if this source cannot be read
-
copyTo
Copies the characters of this source to an appendable, which is left open if it is a stream.- Parameters:
out- the appendable to write to- Returns:
- the number of characters copied
- Throws:
IOException- if this source cannot be read oroutcannot be written
-
copyTo
Copies the characters of this source to a sink.- Parameters:
sink- the sink to write to- Returns:
- the number of characters copied
- Throws:
IOException- if this source cannot be read orsinkcannot be written
-
asByteSource
Returns a source of the bytes that encode these characters in the current default charset. Reading it fails with aCharacterCodingExceptionon characters that charset cannot encode, or on a malformed surrogate pair, rather than replace them.- Returns:
- a source of the encoded bytes
- Since:
- 0.1.2
- See Also:
-
asByteSource
Returns a source of the bytes that encode these characters. Reading it fails with aCharacterCodingExceptionon characterscharsetcannot encode, or on a malformed surrogate pair, rather than replace them.- Parameters:
charset- the charset to encode the characters in- Returns:
- a source of the encoded bytes
- Throws:
IllegalArgumentException- ifcharsetdoes not support encoding
-