@Beta @GwtIncompatible public final class CharStreams extends java.lang.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 | Class and Description |
---|---|
private static class |
CharStreams.NullWriter |
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_BUF_SIZE |
Modifier | Constructor and Description |
---|---|
private |
CharStreams() |
Modifier and Type | Method and Description |
---|---|
static java.io.Writer |
asWriter(java.lang.Appendable target)
Returns a Writer that sends all output to the given
Appendable target. |
static long |
copy(java.lang.Readable from,
java.lang.Appendable to)
Copies all characters between the
Readable and Appendable objects. |
(package private) static long |
copyReaderToBuilder(java.io.Reader from,
java.lang.StringBuilder to)
Copies all characters between the
Reader and StringBuilder objects. |
(package private) static long |
copyReaderToWriter(java.io.Reader from,
java.io.Writer to)
Copies all characters between the
Reader and Writer objects. |
(package private) static java.nio.CharBuffer |
createBuffer()
Creates a new
CharBuffer for buffering reads or writes. |
static long |
exhaust(java.lang.Readable readable)
Reads and discards data from the given
Readable until the end of the stream is reached. |
static java.io.Writer |
nullWriter()
Returns a
Writer that simply discards written chars. |
static java.util.List<java.lang.String> |
readLines(java.lang.Readable r)
Reads all of the lines from a
Readable object. |
static <T> T |
readLines(java.lang.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(java.io.Reader reader,
long n)
Discards
n characters of data from the reader. |
static java.lang.String |
toString(java.lang.Readable r)
Reads all characters from a
Readable object into a String . |
private static java.lang.StringBuilder |
toStringBuilder(java.lang.Readable r)
Reads all characters from a
Readable object into a new StringBuilder instance. |
private static final int DEFAULT_BUF_SIZE
static java.nio.CharBuffer createBuffer()
CharBuffer
for buffering reads or writes.public static long copy(java.lang.Readable from, java.lang.Appendable to) throws java.io.IOException
Readable
and Appendable
objects. Does not
close or flush either object.from
- the object to read fromto
- the object to write tojava.io.IOException
- if an I/O error occursstatic long copyReaderToBuilder(java.io.Reader from, java.lang.StringBuilder to) throws java.io.IOException
Reader
and StringBuilder
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.
from
- the object to read fromto
- the object to write tojava.io.IOException
- if an I/O error occursstatic long copyReaderToWriter(java.io.Reader from, java.io.Writer to) throws java.io.IOException
Reader
and Writer
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.
from
- the object to read fromto
- the object to write tojava.io.IOException
- if an I/O error occurspublic static java.lang.String toString(java.lang.Readable r) throws java.io.IOException
Readable
object into a String
. Does not close the
Readable
.r
- the object to read fromjava.io.IOException
- if an I/O error occursprivate static java.lang.StringBuilder toStringBuilder(java.lang.Readable r) throws java.io.IOException
Readable
object into a new StringBuilder
instance.
Does not close the Readable
.r
- the object to read fromStringBuilder
containing all the charactersjava.io.IOException
- if an I/O error occurspublic static java.util.List<java.lang.String> readLines(java.lang.Readable r) throws java.io.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 linesjava.io.IOException
- if an I/O error occurspublic static <T> T readLines(java.lang.Readable readable, LineProcessor<T> processor) throws java.io.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.java.io.IOException
- if an I/O error occurspublic static long exhaust(java.lang.Readable readable) throws java.io.IOException
Readable
until the end of the stream is reached.
Returns the total number of chars read. Does not close the stream.java.io.IOException
public static void skipFully(java.io.Reader reader, long n) throws java.io.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 skipjava.io.EOFException
- if this stream reaches the end before skipping all the charactersjava.io.IOException
- if an I/O error occurspublic static java.io.Writer nullWriter()
Writer
that simply discards written chars.public static java.io.Writer asWriter(java.lang.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 sent