Class CharStreams

java.lang.Object
com.google.common.io.CharStreams

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

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
  • Field Details

  • Constructor Details

    • CharStreams

      private CharStreams()
  • Method Details

    • createBuffer

      static CharBuffer createBuffer()
      Creates a new CharBuffer for buffering reads or writes.
    • 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
    • copyReaderToBuilder

      static long copyReaderToBuilder(Reader from, StringBuilder to) throws IOException
      Copies all characters between the Reader and StringBuilder objects. Does not close or flush the reader.

      This is identical to copy(Readable, Appendable) but optimized for these specific types. CharBuffer has poor performance when being written into or read out of so round tripping all the bytes through the buffer takes a long time. With these specialized types we can just use a char array.

      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
    • copyReaderToWriter

      static long copyReaderToWriter(Reader from, Writer to) throws IOException
      Copies all characters between the Reader and Writer objects. Does not close or flush the reader or writer.

      This is identical to copy(Readable, Appendable) but optimized for these specific types. CharBuffer has poor performance when being written into or read out of so round tripping all the bytes through the buffer takes a long time. With these specialized types we can just use a char array.

      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
    • toStringBuilder

      private static StringBuilder toStringBuilder(Readable r) throws IOException
      Reads all characters from a Readable object into a new StringBuilder instance. Does not close the Readable.
      Parameters:
      r - the object to read from
      Returns:
      a StringBuilder containing all the characters
      Throws:
      IOException - if an I/O error occurs
    • readLines

      public static List<String> readLines(Readable r) throws IOException
      Reads all of the lines from a Readable object. The lines do not include line-termination characters, but do include other leading and trailing whitespace.

      Does not close the Readable. If reading files or resources you should use the Files.readLines(java.io.File, java.nio.charset.Charset) and Resources.readLines(java.net.URL, java.nio.charset.Charset, com.google.common.io.LineProcessor<T>) methods.

      Parameters:
      r - the object 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
    • exhaust

      public static long exhaust(Readable readable) throws IOException
      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:
      IOException
      Since:
      20.0
    • 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