Class CharStreams


  • @Beta
    public final class CharStreams
    extends Object
    Provides utility methods for working with character streams.

    All method parameters must be non-null unless documented otherwise.

    Some of the methods in this class take arguments with a generic type of Readable & Closeable. A Reader implements both of those interfaces. Similarly for Appendable & Closeable and Writer.

    Since:
    1.0
    • Method Detail

      • newReaderSupplier

        public static InputSupplier<StringReader> newReaderSupplier​(String value)
        Returns a factory that will supply instances of StringReader that read a string value.
        Parameters:
        value - the string to read
        Returns:
        the factory
      • write

        public static <W extends Appendable & Closeable> void write​(CharSequence from,
                                                                    OutputSupplier<W> to)
                                                             throws IOException
        Writes a character sequence (such as a string) to an appendable object from the given supplier.
        Parameters:
        from - the character sequence to write
        to - the output supplier
        Throws:
        IOException - if an I/O error occurs
      • copy

        public static <R extends Readable & Closeable> long copy​(InputSupplier<R> from,
                                                                 Appendable to)
                                                          throws IOException
        Opens a Readable object from the supplier, copies all characters to the Appendable object, and closes the input. Does not close or flush the output.
        Parameters:
        from - the input factory
        to - the object to write to
        Returns:
        the number of characters copied
        Throws:
        IOException - if an I/O error occurs
      • copy

        public static long copy​(Readable from,
                                Appendable to)
                         throws IOException
        Copies all characters between the Readable and Appendable objects. Does not close or flush either object.
        Parameters:
        from - the object to read from
        to - the object to write to
        Returns:
        the number of characters copied
        Throws:
        IOException - if an I/O error occurs
      • toString

        public static String toString​(Readable r)
                               throws IOException
        Reads all characters from a Readable object into a String. Does not close the Readable.
        Parameters:
        r - the object to read from
        Returns:
        a string containing all the characters
        Throws:
        IOException - if an I/O error occurs
      • readFirstLine

        public static <R extends Readable & CloseableString readFirstLine​(InputSupplier<R> supplier)
                                                                     throws IOException
        Reads the first line from a Readable & Closeable object supplied by a factory. The line does not include line-termination characters, but does include other leading and trailing whitespace.
        Parameters:
        supplier - the factory to read from
        Returns:
        the first line, or null if the reader is empty
        Throws:
        IOException - if an I/O error occurs
      • readLines

        public static <R extends Readable & CloseableList<String> readLines​(InputSupplier<R> supplier)
                                                                       throws IOException
        Reads all of the lines from a Readable & Closeable object supplied by a factory. The lines do not include line-termination characters, but do include other leading and trailing whitespace.
        Parameters:
        supplier - the factory to read from
        Returns:
        a mutable List containing all the lines
        Throws:
        IOException - if an I/O error occurs
      • readLines

        public static <T> T readLines​(Readable readable,
                                      LineProcessor<T> processor)
                               throws IOException
        Streams lines from a Readable object, stopping when the processor returns false or all lines have been read and returning the result produced by the processor. Does not close readable. Note that this method may not fully consume the contents of readable if the processor stops processing early.
        Throws:
        IOException - if an I/O error occurs
        Since:
        14.0
      • readLines

        public static <R extends Readable & Closeable,​T> T readLines​(InputSupplier<R> supplier,
                                                                           LineProcessor<T> callback)
                                                                    throws IOException
        Streams lines from a Readable and Closeable object supplied by a factory, stopping when our callback returns false, or we have read all of the lines.
        Parameters:
        supplier - the factory to read from
        callback - the LineProcessor to use to handle the lines
        Returns:
        the output of processing the lines
        Throws:
        IOException - if an I/O error occurs
      • join

        public static InputSupplier<Reader> join​(Iterable<? extends InputSupplier<? extends Reader>> suppliers)
        Joins multiple Reader suppliers into a single supplier. Reader returned from the supplier will contain the concatenated data from the readers of the underlying suppliers.

        Reading from the joined reader will throw a NullPointerException if any of the suppliers are null or return null.

        Only one underlying reader will be open at a time. Closing the joined reader will close the open underlying reader.

        Parameters:
        suppliers - the suppliers to concatenate
        Returns:
        a supplier that will return a reader containing the concatenated data
      • skipFully

        public static void skipFully​(Reader reader,
                                     long n)
                              throws IOException
        Discards n characters of data from the reader. This method will block until the full amount has been skipped. Does not close the reader.
        Parameters:
        reader - the reader to read from
        n - the number of characters to skip
        Throws:
        EOFException - if this stream reaches the end before skipping all the characters
        IOException - if an I/O error occurs
      • nullWriter

        public static Writer nullWriter()
        Returns a Writer that simply discards written chars.
        Since:
        15.0
      • asWriter

        public static Writer asWriter​(Appendable target)
        Returns a Writer that sends all output to the given Appendable target. Closing the writer will close the target if it is Closeable, and flushing the writer will flush the target if it is Flushable.
        Parameters:
        target - the object to which output will be sent
        Returns:
        a new Writer object, unless target is a Writer, in which case the target is returned
      • asCharSource

        public static CharSource asCharSource​(InputSupplier<? extends Readable> supplier)
        Returns a view of the given Readable supplier as a CharSource.

        This method is a temporary method provided for easing migration from suppliers to sources and sinks.

        Since:
        15.0
      • asCharSink

        public static CharSink asCharSink​(OutputSupplier<? extends Appendable> supplier)
        Returns a view of the given Appendable supplier as a CharSink.

        This method is a temporary method provided for easing migration from suppliers to sources and sinks.

        Since:
        15.0