Class CharStreams
- java.lang.Object
-
- com.google.common.io.CharStreams
-
@Beta public final class CharStreams extends java.lang.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
. AReader
implements both of those interfaces. Similarly forAppendable & Closeable
andWriter
.- Since:
- 1.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static CharSink
asCharSink(OutputSupplier<? extends java.lang.Appendable> supplier)
Returns a view of the givenAppendable
supplier as aCharSink
.static CharSource
asCharSource(InputSupplier<? extends java.lang.Readable> supplier)
Returns a view of the givenReadable
supplier as aCharSource
.static CharSource
asCharSource(java.lang.String string)
Deprecated.UseCharSource.wrap(CharSequence)
instead.static java.io.Writer
asWriter(java.lang.Appendable target)
Returns a Writer that sends all output to the givenAppendable
target.static <R extends java.lang.Readable & java.io.Closeable,W extends java.lang.Appendable & java.io.Closeable>
longcopy(InputSupplier<R> from, OutputSupplier<W> to)
OpensReadable
andAppendable
objects from the given factories, copies all characters between the two, and closes them.static <R extends java.lang.Readable & java.io.Closeable>
longcopy(InputSupplier<R> from, java.lang.Appendable to)
Opens aReadable
object from the supplier, copies all characters to theAppendable
object, and closes the input.static long
copy(java.lang.Readable from, java.lang.Appendable to)
Copies all characters between theReadable
andAppendable
objects.static InputSupplier<java.io.Reader>
join(InputSupplier<? extends java.io.Reader>... suppliers)
Varargs form ofjoin(Iterable)
.static InputSupplier<java.io.Reader>
join(java.lang.Iterable<? extends InputSupplier<? extends java.io.Reader>> suppliers)
Joins multipleReader
suppliers into a single supplier.static InputSupplier<java.io.InputStreamReader>
newReaderSupplier(InputSupplier<? extends java.io.InputStream> in, java.nio.charset.Charset charset)
Returns a factory that will supply instances ofInputStreamReader
, using the givenInputStream
factory and character set.static InputSupplier<java.io.StringReader>
newReaderSupplier(java.lang.String value)
Returns a factory that will supply instances ofStringReader
that read a string value.static OutputSupplier<java.io.OutputStreamWriter>
newWriterSupplier(OutputSupplier<? extends java.io.OutputStream> out, java.nio.charset.Charset charset)
Returns a factory that will supply instances ofOutputStreamWriter
, using the givenOutputStream
factory and character set.static java.io.Writer
nullWriter()
Returns aWriter
that simply discards written chars.static <R extends java.lang.Readable & java.io.Closeable>
java.lang.StringreadFirstLine(InputSupplier<R> supplier)
Reads the first line from aReadable
&Closeable
object supplied by a factory.static <R extends java.lang.Readable & java.io.Closeable>
java.util.List<java.lang.String>readLines(InputSupplier<R> supplier)
Reads all of the lines from aReadable
&Closeable
object supplied by a factory.static <R extends java.lang.Readable & java.io.Closeable,T>
TreadLines(InputSupplier<R> supplier, LineProcessor<T> callback)
Streams lines from aReadable
andCloseable
object supplied by a factory, stopping when our callback returns false, or we have read all of the lines.static java.util.List<java.lang.String>
readLines(java.lang.Readable r)
Reads all of the lines from aReadable
object.static <T> T
readLines(java.lang.Readable readable, LineProcessor<T> processor)
Streams lines from aReadable
object, stopping when the processor returnsfalse
or all lines have been read and returning the result produced by the processor.static void
skipFully(java.io.Reader reader, long n)
Discardsn
characters of data from the reader.static <R extends java.lang.Readable & java.io.Closeable>
java.lang.StringtoString(InputSupplier<R> supplier)
Returns the characters from aReadable
&Closeable
object supplied by a factory as aString
.static java.lang.String
toString(java.lang.Readable r)
Reads all characters from aReadable
object into aString
.static <W extends java.lang.Appendable & java.io.Closeable>
voidwrite(java.lang.CharSequence from, OutputSupplier<W> to)
Writes a character sequence (such as a string) to an appendable object from the given supplier.
-
-
-
Method Detail
-
newReaderSupplier
public static InputSupplier<java.io.StringReader> newReaderSupplier(java.lang.String value)
Returns a factory that will supply instances ofStringReader
that read a string value.- Parameters:
value
- the string to read- Returns:
- the factory
-
asCharSource
@Deprecated public static CharSource asCharSource(java.lang.String string)
Deprecated.UseCharSource.wrap(CharSequence)
instead. This method is scheduled to be removed in Guava 16.0.Returns aCharSource
that reads the given string value.- Since:
- 14.0
-
newReaderSupplier
public static InputSupplier<java.io.InputStreamReader> newReaderSupplier(InputSupplier<? extends java.io.InputStream> in, java.nio.charset.Charset charset)
Returns a factory that will supply instances ofInputStreamReader
, using the givenInputStream
factory and character set.- Parameters:
in
- the factory that will be used to open input streamscharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constants- Returns:
- the factory
-
newWriterSupplier
public static OutputSupplier<java.io.OutputStreamWriter> newWriterSupplier(OutputSupplier<? extends java.io.OutputStream> out, java.nio.charset.Charset charset)
Returns a factory that will supply instances ofOutputStreamWriter
, using the givenOutputStream
factory and character set.- Parameters:
out
- the factory that will be used to open output streamscharset
- the charset used to encode the output stream; seeCharsets
for helpful predefined constants- Returns:
- the factory
-
write
public static <W extends java.lang.Appendable & java.io.Closeable> void write(java.lang.CharSequence from, OutputSupplier<W> to) throws java.io.IOException
Writes a character sequence (such as a string) to an appendable object from the given supplier.- Parameters:
from
- the character sequence to writeto
- the output supplier- Throws:
java.io.IOException
- if an I/O error occurs
-
copy
public static <R extends java.lang.Readable & java.io.Closeable,W extends java.lang.Appendable & java.io.Closeable> long copy(InputSupplier<R> from, OutputSupplier<W> to) throws java.io.IOException
OpensReadable
andAppendable
objects from the given factories, copies all characters between the two, and closes them.- Parameters:
from
- the input factoryto
- the output factory- Returns:
- the number of characters copied
- Throws:
java.io.IOException
- if an I/O error occurs
-
copy
public static <R extends java.lang.Readable & java.io.Closeable> long copy(InputSupplier<R> from, java.lang.Appendable to) throws java.io.IOException
Opens aReadable
object from the supplier, copies all characters to theAppendable
object, and closes the input. Does not close or flush the output.- Parameters:
from
- the input factoryto
- the object to write to- Returns:
- the number of characters copied
- Throws:
java.io.IOException
- if an I/O error occurs
-
copy
public static long copy(java.lang.Readable from, java.lang.Appendable to) throws java.io.IOException
Copies all characters between theReadable
andAppendable
objects. Does not close or flush either object.- Parameters:
from
- the object to read fromto
- the object to write to- Returns:
- the number of characters copied
- Throws:
java.io.IOException
- if an I/O error occurs
-
toString
public static java.lang.String toString(java.lang.Readable r) throws java.io.IOException
Reads all characters from aReadable
object into aString
. Does not close theReadable
.- Parameters:
r
- the object to read from- Returns:
- a string containing all the characters
- Throws:
java.io.IOException
- if an I/O error occurs
-
toString
public static <R extends java.lang.Readable & java.io.Closeable> java.lang.String toString(InputSupplier<R> supplier) throws java.io.IOException
Returns the characters from aReadable
&Closeable
object supplied by a factory as aString
.- Parameters:
supplier
- the factory to read from- Returns:
- a string containing all the characters
- Throws:
java.io.IOException
- if an I/O error occurs
-
readFirstLine
public static <R extends java.lang.Readable & java.io.Closeable> java.lang.String readFirstLine(InputSupplier<R> supplier) throws java.io.IOException
Reads the first line from aReadable
&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:
java.io.IOException
- if an I/O error occurs
-
readLines
public static <R extends java.lang.Readable & java.io.Closeable> java.util.List<java.lang.String> readLines(InputSupplier<R> supplier) throws java.io.IOException
Reads all of the lines from aReadable
&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:
java.io.IOException
- if an I/O error occurs
-
readLines
public static java.util.List<java.lang.String> readLines(java.lang.Readable r) throws java.io.IOException
Reads all of the lines from aReadable
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 theFiles.readLines(java.io.File, java.nio.charset.Charset)
andResources.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:
java.io.IOException
- if an I/O error occurs
-
readLines
public static <T> T readLines(java.lang.Readable readable, LineProcessor<T> processor) throws java.io.IOException
Streams lines from aReadable
object, stopping when the processor returnsfalse
or all lines have been read and returning the result produced by the processor. Does not closereadable
. Note that this method may not fully consume the contents ofreadable
if the processor stops processing early.- Throws:
java.io.IOException
- if an I/O error occurs- Since:
- 14.0
-
readLines
public static <R extends java.lang.Readable & java.io.Closeable,T> T readLines(InputSupplier<R> supplier, LineProcessor<T> callback) throws java.io.IOException
Streams lines from aReadable
andCloseable
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 fromcallback
- the LineProcessor to use to handle the lines- Returns:
- the output of processing the lines
- Throws:
java.io.IOException
- if an I/O error occurs
-
join
public static InputSupplier<java.io.Reader> join(java.lang.Iterable<? extends InputSupplier<? extends java.io.Reader>> suppliers)
Joins multipleReader
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
-
join
public static InputSupplier<java.io.Reader> join(InputSupplier<? extends java.io.Reader>... suppliers)
Varargs form ofjoin(Iterable)
.
-
skipFully
public static void skipFully(java.io.Reader reader, long n) throws java.io.IOException
Discardsn
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 fromn
- the number of characters to skip- Throws:
java.io.EOFException
- if this stream reaches the end before skipping all the charactersjava.io.IOException
- if an I/O error occurs
-
nullWriter
public static java.io.Writer nullWriter()
Returns aWriter
that simply discards written chars.- Since:
- 15.0
-
asWriter
public static java.io.Writer asWriter(java.lang.Appendable target)
Returns a Writer that sends all output to the givenAppendable
target. Closing the writer will close the target if it isCloseable
, and flushing the writer will flush the target if it isFlushable
.- 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
-
asCharSource
public static CharSource asCharSource(InputSupplier<? extends java.lang.Readable> supplier)
Returns a view of the givenReadable
supplier as aCharSource
.This method is a temporary method provided for easing migration from suppliers to sources and sinks.
- Since:
- 15.0
-
asCharSink
public static CharSink asCharSink(OutputSupplier<? extends java.lang.Appendable> supplier)
Returns a view of the givenAppendable
supplier as aCharSink
.This method is a temporary method provided for easing migration from suppliers to sources and sinks.
- Since:
- 15.0
-
-