Class CharStreams


  • 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

      • 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
      • 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 bytes
        IOException - if an I/O error occurs
      • limit

        public static Reader limit​(Reader in,
                                   long n)