@Beta public final class CharStreams extends Object
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
.
Modifier and Type | Method and Description |
---|---|
static CharSink |
asCharSink(OutputSupplier<? extends Appendable> supplier)
Returns a view of the given
Appendable supplier as a
CharSink . |
static CharSource |
asCharSource(InputSupplier<? extends Readable> supplier)
Returns a view of the given
Readable supplier as a
CharSource . |
static CharSource |
asCharSource(String string)
Deprecated.
Use
CharSource.wrap(CharSequence) instead. This method
is scheduled to be removed in Guava 16.0. |
static Writer |
asWriter(Appendable target)
Returns a Writer that sends all output to the given
Appendable
target. |
static <R extends Readable & Closeable> |
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 <R extends Readable & Closeable,W extends Appendable & Closeable> |
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 long |
copy(Readable from,
Appendable to)
Copies all characters between the
Readable and Appendable
objects. |
static InputSupplier<Reader> |
join(InputSupplier<? extends Reader>... suppliers)
Varargs form of
join(Iterable) . |
static InputSupplier<Reader> |
join(Iterable<? extends InputSupplier<? extends Reader>> suppliers)
Joins multiple
Reader suppliers into a single supplier. |
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 Writer |
nullWriter()
Returns a
Writer that simply discards written chars. |
static <R extends Readable & Closeable> |
readFirstLine(InputSupplier<R> supplier)
|
static <R extends Readable & Closeable> |
readLines(InputSupplier<R> supplier)
|
static <R extends Readable & Closeable,T> |
readLines(InputSupplier<R> supplier,
LineProcessor<T> callback)
|
static List<String> |
readLines(Readable r)
Reads all of the lines from a
Readable object. |
static <T> T |
readLines(Readable readable,
LineProcessor<T> processor)
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. |
static void |
skipFully(Reader reader,
long n)
Discards
n characters of data from the reader. |
static <R extends Readable & Closeable> |
toString(InputSupplier<R> supplier)
|
static String |
toString(Readable r)
|
static <W extends Appendable & Closeable> |
write(CharSequence from,
OutputSupplier<W> to)
Writes a character sequence (such as a string) to an appendable
object from the given supplier.
|
public static InputSupplier<StringReader> newReaderSupplier(String value)
StringReader
that
read a string value.value
- the string to read@Deprecated public static CharSource asCharSource(String string)
CharSource.wrap(CharSequence)
instead. This method
is scheduled to be removed in Guava 16.0.CharSource
that reads the given string value.public static InputSupplier<InputStreamReader> newReaderSupplier(InputSupplier<? extends InputStream> in, Charset charset)
InputStreamReader
,
using the given InputStream
factory and character set.in
- the factory that will be used to open input streamscharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constantspublic static OutputSupplier<OutputStreamWriter> newWriterSupplier(OutputSupplier<? extends OutputStream> out, Charset charset)
OutputStreamWriter
,
using the given OutputStream
factory and character set.out
- the factory that will be used to open output streamscharset
- the charset used to encode the output stream; see Charsets
for helpful predefined constantspublic static <W extends Appendable & Closeable> void write(CharSequence from, OutputSupplier<W> to) throws IOException
from
- the character sequence to writeto
- the output supplierIOException
- if an I/O error occurspublic static <R extends Readable & Closeable,W extends Appendable & Closeable> long copy(InputSupplier<R> from, OutputSupplier<W> to) throws IOException
Readable
and Appendable
objects from the
given factories, copies all characters between the two, and closes
them.from
- the input factoryto
- the output factoryIOException
- if an I/O error occurspublic static <R extends Readable & Closeable> long copy(InputSupplier<R> from, Appendable to) throws IOException
Readable
object from the supplier, copies all characters
to the Appendable
object, and closes the input. Does not close
or flush the output.from
- the input factoryto
- the object to write toIOException
- if an I/O error occurspublic static long copy(Readable from, Appendable to) throws IOException
Readable
and Appendable
objects. Does not close or flush either object.from
- the object to read fromto
- the object to write toIOException
- if an I/O error occurspublic static String toString(Readable r) throws IOException
r
- the object to read fromIOException
- if an I/O error occurspublic static <R extends Readable & Closeable> String toString(InputSupplier<R> supplier) throws IOException
supplier
- the factory to read fromIOException
- if an I/O error occurspublic static <R extends Readable & Closeable> String readFirstLine(InputSupplier<R> supplier) throws IOException
Readable
& Closeable
object
supplied by a factory. The line does not include line-termination
characters, but does include other leading and trailing whitespace.supplier
- the factory to read fromIOException
- if an I/O error occurspublic static <R extends Readable & Closeable> List<String> readLines(InputSupplier<R> supplier) throws IOException
Readable
& Closeable
object
supplied by a factory. The lines do not include line-termination
characters, but do include other leading and trailing whitespace.supplier
- the factory to read fromList
containing all the linesIOException
- if an I/O error occurspublic static List<String> readLines(Readable r) throws IOException
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.
r
- the object to read fromList
containing all the linesIOException
- if an I/O error occurspublic static <T> T readLines(Readable readable, LineProcessor<T> processor) throws IOException
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.IOException
- if an I/O error occurspublic static <R extends Readable & Closeable,T> T readLines(InputSupplier<R> supplier, LineProcessor<T> callback) throws IOException
Readable
and Closeable
object
supplied by a factory, stopping when our callback returns false, or we
have read all of the lines.supplier
- the factory to read fromcallback
- the LineProcessor to use to handle the linesIOException
- if an I/O error occurspublic static InputSupplier<Reader> join(Iterable<? extends InputSupplier<? extends Reader>> suppliers)
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.
suppliers
- the suppliers to concatenatepublic static InputSupplier<Reader> join(InputSupplier<? extends Reader>... suppliers)
join(Iterable)
.public static void skipFully(Reader reader, long n) throws IOException
n
characters of data from the reader. This method
will block until the full amount has been skipped. Does not close the
reader.reader
- the reader to read fromn
- the number of characters to skipEOFException
- if this stream reaches the end before skipping all
the charactersIOException
- if an I/O error occurspublic static Writer nullWriter()
Writer
that simply discards written chars.public static Writer asWriter(Appendable target)
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
.target
- the object to which output will be sentpublic static CharSource asCharSource(InputSupplier<? extends Readable> supplier)
Readable
supplier as a
CharSource
.
This method is a temporary method provided for easing migration from suppliers to sources and sinks.
public static CharSink asCharSink(OutputSupplier<? extends Appendable> supplier)
Appendable
supplier as a
CharSink
.
This method is a temporary method provided for easing migration from suppliers to sources and sinks.
Copyright © 2010-2013. All Rights Reserved.