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.

@FunctionalInterface public interface CharSource
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 Details

    • empty

      static CharSource empty()
      Returns a source with no characters.
      Returns:
      an empty source
    • wrap

      static CharSource wrap(CharSequence chars)
      Returns a source that reads a character sequence. The sequence is not copied: changes to it, as in a StringBuilder, 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

      static CharSource of(Path file, OpenOption... options)
      Returns a source that reads a text file encoded in the current default charset.
      Parameters:
      file - the file to read
      options - how to open the file, as for Files.newInputStream(Path, OpenOption...)
      Returns:
      a source of the content of file
      Since:
      0.1.2
      See Also:
    • of

      static CharSource of(Path file, Charset charset, OpenOption... options)
      Returns a source that reads a text file.
      Parameters:
      file - the file to read
      charset - the charset the file is encoded in
      options - how to open the file, as for Files.newInputStream(Path, OpenOption...)
      Returns:
      a source of the content of file
      See Also:
    • of

      static CharSource of(URL url)
      Returns a source that reads the text a URL points to, such as a resource from Class.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

      static CharSource of(URL url, Charset charset)
      Returns a source that reads the text a URL points to, such as a resource from Class.getResource(String). Like ByteSource.of(URL), it is meant for local resources: it opens the URL with no timeout.
      Parameters:
      url - the URL to read
      charset - the charset the text is encoded in
      Returns:
      a source of the content at url
      See Also:
    • of

      static CharSource of(URI uri)
      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 - if uri is not absolute, or has a scheme no URL protocol handler supports
      Since:
      0.1.2
      See Also:
    • of

      static CharSource of(URI uri, Charset charset)
      Returns a source that reads the text a URI points to, through the URL it converts to.
      Parameters:
      uri - the URI to read
      charset - the charset the text is encoded in
      Returns:
      a source of the content at uri
      Throws:
      IllegalArgumentException - if uri is not absolute, or has a scheme no URL protocol handler supports
      See Also:
    • concat

      static CharSource concat(CharSource... sources)
      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

      static CharSource concat(Iterable<? extends CharSource> sources)
      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 to sources have no effect.
      Parameters:
      sources - the sources to read
      Returns:
      a source of the content of sources, in order
    • openReader

      CharReader openReader() throws IOException
      Opens a new reader on the characters of this source. The caller must close it. For APIs that take a Reader, CharReader.asReader() reads through it.
      Returns:
      a new reader
      Throws:
      IOException - if the reader cannot be opened
      Since:
      0.2.0
    • isEmpty

      default boolean isEmpty() throws IOException
      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

      default OptionalLong 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

      default long length() throws IOException
      Returns the number of characters in this source, reading them all if lengthIfKnown() does not know it.
      Returns:
      the number of characters
      Throws:
      IOException - if this source cannot be read
    • read

      default String read() throws IOException
      Reads all the characters of this source.
      Returns:
      the characters of this source
      Throws:
      IOException - if this source cannot be read
    • readFirstLine

      default Optional<String> readFirstLine() throws IOException
      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

      default List<String> readLines() throws IOException
      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

      default Stream<String> lines() throws IOException
      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 an UncheckedIOException.
      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

      default void forEachLine(Consumer<? super String> action) throws IOException
      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

      default long copyTo(Appendable out) throws IOException
      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 or out cannot be written
    • copyTo

      default long copyTo(CharSink sink) throws IOException
      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 or sink cannot be written
    • asByteSource

      default ByteSource asByteSource()
      Returns a source of the bytes that encode these characters in the current default charset. Reading it fails with a CharacterCodingException on 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

      default ByteSource asByteSource(Charset charset)
      Returns a source of the bytes that encode these characters. Reading it fails with a CharacterCodingException on characters charset cannot 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 - if charset does not support encoding