public final class

Characters

extends Object
java.lang.Object
   ↳ com.google.gdata.util.common.io.Characters

Class Overview

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 java.io.Reader implements both of those interfaces. Similarly for Appendable & Closeable and java.io.Writer.

Summary

Constants
int BUF_SIZE
Public Methods
static Writer asWriter(Appendable target)
Returns a Writer that sends all output to the given Appendable target.
static long copy(Readable from, Appendable to)
Copies all characters between the Readable and Appendable objects.
static <R extends Readable & Closeable, W extends Appendable & Closeable> long copy(InputSupplier<R> from, OutputSupplier<W> to)
Opens Readable and Appendable objects from the given factories, copies all characters between the two, and closes them.
static <R extends Readable & Closeable> long copy(InputSupplier<R> from, Appendable to)
Opens a Readable object from the supplier, copies all characters to the Appendable object, and closes the input.
static InputSupplier<Reader> join(Iterable<? extends InputSupplier<? extends Reader>> suppliers)
Joins multiple Reader suppliers into a single supplier.
static InputSupplier<Reader> join(InputSupplier...<? extends Reader> suppliers)
Varargs form of join(Iterable).
static InputSupplier<InputStreamReader> newReaderSupplier(InputSupplier<? extends InputStream> in, Charset charset)
Returns a factory that will supply instances of InputStreamReader, using the given InputStream factory and character set.
static InputSupplier<StringReader> newReaderSupplier(String value)
Returns a factory that will supply instances of StringReader that read a string value.
static OutputSupplier<OutputStreamWriter> newWriterSupplier(OutputSupplier<? extends OutputStream> out, Charset charset)
Returns a factory that will supply instances of OutputStreamWriter, using the given OutputStream factory and character set.
static <R extends Readable & Closeable> String readFirstLine(InputSupplier<R> supplier)
Reads the first line from a Readable & Closeable object supplied by a factory.
static <R extends Readable & Closeable> List<String> readLines(InputSupplier<R> supplier)
Reads all of the lines from a Readable & Closeable object supplied by a factory.
static List<String> readLines(Readable r)
Reads all of the lines from a Readable object.
static <R extends Readable & Closeable, T> T readLines(InputSupplier<R> supplier, LineProcessor<T> callback)
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.
static void skipFully(Reader reader, long n)
Discards n characters of data from the reader.
static <R extends Readable & Closeable> String toString(InputSupplier<R> supplier)
Returns the characters from a Readable & Closeable object supplied by a factory as a String.
static String toString(Readable r)
Reads all characters from a Readable object into a String.
static <W extends Appendable & Closeable> void write(CharSequence from, OutputSupplier<W> to)
Writes a character sequence (such as a string) to an appendable object from the given supplier.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

private static final int BUF_SIZE

Constant Value: 2048 (0x00000800)

Public Methods

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 java.io.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

public static long copy (Readable from, Appendable to)

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

public static long copy (InputSupplier<R> from, OutputSupplier<W> to)

Opens Readable and Appendable objects from the given factories, copies all characters between the two, and closes them.

Parameters
from The input factory
to The output factory
Returns
  • the number of characters copied
Throws
IOException if an I/O error occurs

public static long copy (InputSupplier<R> from, Appendable to)

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

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

public static InputSupplier<Reader> join (InputSupplier...<? extends Reader> suppliers)

Varargs form of join(Iterable).

Parameters
suppliers

public static InputSupplier<InputStreamReader> newReaderSupplier (InputSupplier<? extends InputStream> in, Charset charset)

Returns a factory that will supply instances of InputStreamReader, using the given InputStream factory and character set.

Parameters
in The factory that will be used to open input streams
charset The character set used to decode the input stream
Returns
  • the factory

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

public static OutputSupplier<OutputStreamWriter> newWriterSupplier (OutputSupplier<? extends OutputStream> out, Charset charset)

Returns a factory that will supply instances of OutputStreamWriter, using the given OutputStream factory and character set.

Parameters
out The factory that will be used to open output streams
charset The character set used to encode the output stream
Returns
  • the factory

public static String readFirstLine (InputSupplier<R> supplier)

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

public static List<String> readLines (InputSupplier<R> supplier)

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

public static List<String> readLines (Readable r)

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 and Resources#readLines methods.

Parameters
r The object to read from
Returns
  • a mutable List containing all the lines
Throws
IOException if an I/O error occurs

public static T readLines (InputSupplier<R> supplier, LineProcessor<T> callback)

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

public static void skipFully (Reader reader, long n)

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

public static String toString (InputSupplier<R> supplier)

Returns the characters from a Readable & Closeable object supplied by a factory as a String.

Parameters
supplier The factory to read from
Returns
  • a string containing all the characters
Throws
IOException if an I/O error occurs

public static String toString (Readable r)

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

public static void write (CharSequence from, OutputSupplier<W> to)

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