Class CharStreams


  • @GwtIncompatible
    public final class CharStreams
    extends 
    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 implements both of those interfaces. Similarly for Appendable & Closeable and .

    Since:
    1.0
    Author:
    Chris Nokleberg, Bin Zhu, Colin Decker
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static  target)
      Returns a Writer that sends all output to the given target.
      static long  from,  to)
      Copies all characters between the and objects.
      static long  readable)
      Reads and discards data from the given Readable until the end of the stream is reached.
      static nullWriter()
      Returns a that simply discards written chars.
      static <>  r)
      Reads all of the lines from a object.
      static <T> T  readable, LineProcessor<T> processor)
      Streams lines from a object, stopping when the processor returns false or all lines have been read and returning the result produced by the processor.
      static void  reader, long n)
      Discards n characters of data from the reader.
      static  r)
      Reads all characters from a object into a .
      • Methods inherited from class java.lang.

        , , , , , , , , , ,
    • Method Detail

      • copy

        public static long  from,
                                 to)
                         throws 
        Copies all characters between the and 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:
        - if an I/O error occurs
      • toString

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

        @Beta
        
        public static <T> T  readable,
                                      LineProcessor<T> processor)
                               throws 
        Streams lines from a 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:
        - if an I/O error occurs
        Since:
        14.0
      • exhaust

        @Beta
        
        public static long  readable)
                            throws 
        Reads and discards data from the given Readable until the end of the stream is reached. Returns the total number of chars read. Does not close the stream.
        Throws:
        Since:
        20.0
      • skipFully

        @Beta
        public static void  reader,
                                     long n)
                              throws 
        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:
        - if this stream reaches the end before skipping all the characters
        - if an I/O error occurs
      • nullWriter

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

        @Beta
        public static   target)
        Returns a Writer that sends all output to the given target. Closing the writer will close the target if it is , and flushing the writer will flush the target if it is .
        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