Package com.google.common.io
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. AReaderimplements both of those interfaces. Similarly forAppendable & CloseableandWriter.- Since:
- 1.0
- Author:
- Chris Nokleberg, Bin Zhu, Colin Decker
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static CharSinkasCharSink(OutputSupplier<? extends java.lang.Appendable> supplier)Deprecated.Convert allOutputSupplier<? extends Appendable>implementations to extendCharSinkor provide a method for viewing the object as aCharSink.static CharSourceasCharSource(InputSupplier<? extends java.lang.Readable> supplier)Deprecated.Convert allInputSupplier<? extends Readable>implementations to extendCharSourceor provide a method for viewing the object as aCharSource.static java.io.WriterasWriter(java.lang.Appendable target)Returns a Writer that sends all output to the givenAppendabletarget.static longcopy(java.lang.Readable from, java.lang.Appendable to)Copies all characters between theReadableandAppendableobjects.static InputSupplier<java.io.InputStreamReader>newReaderSupplier(InputSupplier<? extends java.io.InputStream> in, java.nio.charset.Charset charset)Deprecated.UseByteSource.asCharSource(Charset)instead.static InputSupplier<java.io.StringReader>newReaderSupplier(java.lang.String value)Deprecated.UseCharSource.wrap(CharSequence)instead.static OutputSupplier<java.io.OutputStreamWriter>newWriterSupplier(OutputSupplier<? extends java.io.OutputStream> out, java.nio.charset.Charset charset)Deprecated.UseByteSink.asCharSink(Charset)instead.static java.io.WriternullWriter()Returns aWriterthat simply discards written chars.static <R extends java.lang.Readable & java.io.Closeable>
java.lang.StringreadFirstLine(InputSupplier<R> supplier)Deprecated.UseCharSource.readFirstLine()instead.static <R extends java.lang.Readable & java.io.Closeable>
java.util.List<java.lang.String>readLines(InputSupplier<R> supplier)Deprecated.UseCharSource.readLines()instead, but note that it returns anImmutableList.static java.util.List<java.lang.String>readLines(java.lang.Readable r)Reads all of the lines from aReadableobject.static <T> TreadLines(java.lang.Readable readable, LineProcessor<T> processor)Streams lines from aReadableobject, stopping when the processor returnsfalseor all lines have been read and returning the result produced by the processor.static voidskipFully(java.io.Reader reader, long n)Discardsncharacters of data from the reader.static java.lang.StringtoString(java.lang.Readable r)Reads all characters from aReadableobject into aString.
-
-
-
Method Detail
-
newReaderSupplier
@Deprecated public static InputSupplier<java.io.StringReader> newReaderSupplier(java.lang.String value)
Deprecated.UseCharSource.wrap(CharSequence)instead. This method is scheduled for removal in Guava 18.0.Returns a factory that will supply instances ofStringReaderthat read a string value.- Parameters:
value- the string to read- Returns:
- the factory
-
newReaderSupplier
@Deprecated public static InputSupplier<java.io.InputStreamReader> newReaderSupplier(InputSupplier<? extends java.io.InputStream> in, java.nio.charset.Charset charset)
Deprecated.UseByteSource.asCharSource(Charset)instead. This method is scheduled for removal in Guava 18.0.Returns a factory that will supply instances ofInputStreamReader, using the givenInputStreamfactory and character set.- Parameters:
in- the factory that will be used to open input streamscharset- the charset used to decode the input stream; seeCharsetsfor helpful predefined constants- Returns:
- the factory
-
newWriterSupplier
@Deprecated public static OutputSupplier<java.io.OutputStreamWriter> newWriterSupplier(OutputSupplier<? extends java.io.OutputStream> out, java.nio.charset.Charset charset)
Deprecated.UseByteSink.asCharSink(Charset)instead. This method is scheduled for removal in Guava 18.0.Returns a factory that will supply instances ofOutputStreamWriter, using the givenOutputStreamfactory and character set.- Parameters:
out- the factory that will be used to open output streamscharset- the charset used to encode the output stream; seeCharsetsfor helpful predefined constants- Returns:
- the factory
-
copy
public static long copy(java.lang.Readable from, java.lang.Appendable to) throws java.io.IOExceptionCopies all characters between theReadableandAppendableobjects. 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.IOExceptionReads all characters from aReadableobject 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
-
readFirstLine
@Deprecated public static <R extends java.lang.Readable & java.io.Closeable> java.lang.String readFirstLine(InputSupplier<R> supplier) throws java.io.IOException
Deprecated.UseCharSource.readFirstLine()instead. This method is scheduled for removal in Guava 18.0.Reads the first line from aReadable&Closeableobject 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
@Deprecated public static <R extends java.lang.Readable & java.io.Closeable> java.util.List<java.lang.String> readLines(InputSupplier<R> supplier) throws java.io.IOException
Deprecated.UseCharSource.readLines()instead, but note that it returns anImmutableList. This method is scheduled for removal in Guava 18.0.Reads all of the lines from aReadable&Closeableobject 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
Listcontaining 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.IOExceptionReads all of the lines from aReadableobject. 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
Listcontaining 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.IOExceptionStreams lines from aReadableobject, stopping when the processor returnsfalseor 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 ofreadableif the processor stops processing early.- Throws:
java.io.IOException- if an I/O error occurs- Since:
- 14.0
-
skipFully
public static void skipFully(java.io.Reader reader, long n) throws java.io.IOExceptionDiscardsncharacters 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 aWriterthat 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 givenAppendabletarget. 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
@Deprecated public static CharSource asCharSource(InputSupplier<? extends java.lang.Readable> supplier)
Deprecated.Convert allInputSupplier<? extends Readable>implementations to extendCharSourceor provide a method for viewing the object as aCharSource. This method is scheduled for removal in Guava 18.0.Returns a view of the givenReadablesupplier as aCharSource.This method is a temporary method provided for easing migration from suppliers to sources and sinks.
- Since:
- 15.0
-
asCharSink
@Deprecated public static CharSink asCharSink(OutputSupplier<? extends java.lang.Appendable> supplier)
Deprecated.Convert allOutputSupplier<? extends Appendable>implementations to extendCharSinkor provide a method for viewing the object as aCharSink. This method is scheduled for removal in Guava 18.0.Returns a view of the givenAppendablesupplier as aCharSink.This method is a temporary method provided for easing migration from suppliers to sources and sinks.
- Since:
- 15.0
-
-