Class CharStreams
- java.lang.Object
-
- com.google.common.io.CharStreams
-
@Beta @GwtIncompatible 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
CharStreams.NullWriter
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_BUF_SIZE
-
Constructor Summary
Constructors Modifier Constructor Description private
CharStreams()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.io.Writer
asWriter(java.lang.Appendable target)
Returns a Writer that sends all output to the givenAppendable
target.static long
copy(java.lang.Readable from, java.lang.Appendable to)
Copies all characters between theReadable
andAppendable
objects.(package private) static long
copyReaderToBuilder(java.io.Reader from, java.lang.StringBuilder to)
Copies all characters between theReader
andStringBuilder
objects.(package private) static long
copyReaderToWriter(java.io.Reader from, java.io.Writer to)
Copies all characters between theReader
andWriter
objects.(package private) static java.nio.CharBuffer
createBuffer()
Creates a newCharBuffer
for buffering reads or writes.static long
exhaust(java.lang.Readable readable)
Reads and discards data from the givenReadable
until the end of the stream is reached.static java.io.Writer
nullWriter()
Returns aWriter
that simply discards written chars.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 java.lang.String
toString(java.lang.Readable r)
Reads all characters from aReadable
object into aString
.private static java.lang.StringBuilder
toStringBuilder(java.lang.Readable r)
Reads all characters from aReadable
object into a newStringBuilder
instance.
-
-
-
Field Detail
-
DEFAULT_BUF_SIZE
private static final int DEFAULT_BUF_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
createBuffer
static java.nio.CharBuffer createBuffer()
Creates a newCharBuffer
for buffering reads or writes.
-
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
-
copyReaderToBuilder
static long copyReaderToBuilder(java.io.Reader from, java.lang.StringBuilder to) throws java.io.IOException
Copies all characters between theReader
andStringBuilder
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 fromto
- the object to write to- Returns:
- the number of characters copied
- Throws:
java.io.IOException
- if an I/O error occurs
-
copyReaderToWriter
static long copyReaderToWriter(java.io.Reader from, java.io.Writer to) throws java.io.IOException
Copies all characters between theReader
andWriter
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 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
-
toStringBuilder
private static java.lang.StringBuilder toStringBuilder(java.lang.Readable r) throws java.io.IOException
Reads all characters from aReadable
object into a newStringBuilder
instance. Does not close theReadable
.- Parameters:
r
- the object to read from- Returns:
- a
StringBuilder
containing all the characters - 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
-
exhaust
public static long exhaust(java.lang.Readable readable) throws java.io.IOException
Reads and discards data from the givenReadable
until the end of the stream is reached. Returns the total number of chars read. Does not close the stream.- Throws:
java.io.IOException
- Since:
- 20.0
-
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
-
-