Top Description Inners Fields Constructors Methods
com.google.common.io

public final Class CharStreams

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, .Preconditions.checkPositionIndexes

Provides utility methods for working with character streams.

Some of the methods in this class take arguments with a generic type of Readable & Closeable. A java.io.Reader implements both of those interfaces. Similarly for Appendable & Closeable and java.io.Writer.

Authors
Chris Nokleberg, Bin Zhu, Colin Decker
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Writer

Returns:

a new Writer object, unless target is a Writer, in which case the target is returned
asWriter
(Appendable
the object to which output will be sent
target
)

Returns a Writer that sends all output to the given Appendable target.

public static long

Returns:

the number of characters copied
copy
(Readable
the object to read from
from
,
Appendable
the object to write to
to
)

Copies all characters between the Readable and Appendable objects.

pack-priv static long

Returns:

the number of characters copied
copyReaderToBuilder
(Reader
the object to read from
from
,
StringBuilder
the object to write to
to
)

Copies all characters between the Reader and StringBuilder objects.

pack-priv static long

Returns:

the number of characters copied
copyReaderToWriter
(Reader
the object to read from
from
,
Writer
the object to write to
to
)

Copies all characters between the Reader and Writer objects.

pack-priv static CharBuffer
createBuffer()

Creates a new CharBuffer for buffering reads or writes.

public static long
exhaust(Readable readable)

Reads and discards data from the given Readable until the end of the stream is reached.

public static Writer
nullWriter()

Returns a Writer that simply discards written chars.

public static List<String>

Returns:

a mutable List containing all the lines
readLines
(Readable
the object to read from
r
)

Reads all of the lines from a Readable object.

public static <T extends @Nullable Object> 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.

public static void
skipFully(Reader
the reader to read from
reader
,
long
the number of characters to skip
n
)

Discards n characters of data from the reader.

public static String

Returns:

a string containing all the characters
toString
(Readable
the object to read from
r
)

Reads all characters from a Readable object into a String.

private static StringBuilder

Returns:

a StringBuilder containing all the characters
toStringBuilder
(Readable
the object to read from
r
)

Reads all characters from a Readable object into a new StringBuilder instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_BUF_SIZEback to summary
private static final int DEFAULT_BUF_SIZE

Constructor Detail

CharStreamsback to summary
private CharStreams()

Method Detail

asWriterback to summary
public static Writer asWriter(Appendable target)

Returns a Writer that sends all output to the given Appendable target. Closing the writer will close the target if it is Closeable, and flushing the writer will flush the target if it is java.io.Flushable.

Parameters
target:Appendable

the object to which output will be sent

Returns:Writer

a new Writer object, unless target is a Writer, in which case the target is returned

copyback to summary
public static long copy(Readable from, Appendable to) throws IOException

Copies all characters between the Readable and Appendable objects. Does not close or flush either object.

Parameters
from:Readable

the object to read from

to:Appendable

the object to write to

Returns:long

the number of characters copied

Annotations
@CanIgnoreReturnValue
Exceptions
IOException:
if an I/O error occurs
copyReaderToBuilderback to summary
pack-priv static long copyReaderToBuilder(Reader from, StringBuilder to) throws IOException

Copies all characters between the 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.

Parameters
from:Reader

the object to read from

to:StringBuilder

the object to write to

Returns:long

the number of characters copied

Annotations
@CanIgnoreReturnValue
Exceptions
IOException:
if an I/O error occurs
copyReaderToWriterback to summary
pack-priv static long copyReaderToWriter(Reader from, Writer to) throws IOException

Copies all characters between the 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.

Parameters
from:Reader

the object to read from

to:Writer

the object to write to

Returns:long

the number of characters copied

Annotations
@CanIgnoreReturnValue
Exceptions
IOException:
if an I/O error occurs
createBufferback to summary
pack-priv static CharBuffer createBuffer()

Creates a new CharBuffer for buffering reads or writes.

exhaustback to summary
public static long exhaust(Readable readable) throws IOException

Reads and discards data from the given Readable until the end of the stream is reached. Returns the total number of chars read. Does not close the stream.

Annotations
@CanIgnoreReturnValue
Since
20.0
nullWriterback to summary
public static Writer nullWriter()

Returns a Writer that simply discards written chars.

Since
15.0
readLinesback to summary
public static List<String> readLines(Readable r) throws IOException

Reads all of the lines from a 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 and Resources#readLines methods.

Parameters
r:Readable

the object to read from

Returns:List<String>

a mutable List containing all the lines

Exceptions
IOException:
if an I/O error occurs
readLinesback to summary
public static <T extends @Nullable Object> T readLines(Readable readable, LineProcessor<T> processor) throws IOException

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. Does not close readable. Note that this method may not fully consume the contents of readable if the processor stops processing early.

Annotations
@CanIgnoreReturnValue
@ParametricNullness
Exceptions
IOException:
if an I/O error occurs
Since
14.0
skipFullyback to summary
public static void skipFully(Reader reader, long n) throws IOException

Discards n characters of data from the reader. This method will block until the full amount has been skipped. Does not close the reader.

Parameters
reader:Reader

the reader to read from

n:long

the number of characters to skip

Exceptions
IOException:
if an I/O error occurs
EOFException:
if this stream reaches the end before skipping all the characters
toStringback to summary
public static String toString(Readable r) throws IOException

Reads all characters from a Readable object into a String. Does not close the Readable.

Parameters
r:Readable

the object to read from

Returns:String

a string containing all the characters

Exceptions
IOException:
if an I/O error occurs
toStringBuilderback to summary
private static StringBuilder toStringBuilder(Readable r) throws IOException

Reads all characters from a Readable object into a new StringBuilder instance. Does not close the Readable.

Parameters
r:Readable

the object to read from

Returns:StringBuilder

a StringBuilder containing all the characters

Exceptions
IOException:
if an I/O error occurs
com.google.common.io back to summary

private final Class CharStreams.NullWriter

extends Writer
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final CharStreams.NullWriter
Inherited from java.io.Writer:
lock

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Writer
append(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
csq
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this writer.
public Writer
append(CharSequence
The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
csq
,
int
The index of the first character in the subsequence
start
,
int
The index of the character following the last character in the subsequence
end
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this writer.
public Writer
append(char
The 16-bit character to append
c
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character to this writer.
public void
close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Closes the stream, flushing it first.
public void
flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flushes the stream.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public void
write(int
int specifying a character to be written
c
)

Overrides java.io.Writer.write.

Writes a single character.
public void
write(char[]
Array of characters to be written
cbuf
)

Overrides java.io.Writer.write.

Writes an array of characters.
public void
write(char[]
Array of characters
cbuf
,
int
Offset from which to start writing characters
off
,
int
Number of characters to write
len
)

Implements abstract java.io.Writer.write.

Writes a portion of an array of characters.
public void
write(String
String to be written
str
)

Overrides java.io.Writer.write.

Writes a string.
public void
write(String
A String
str
,
int
Offset from which to start writing characters
off
,
int
Number of characters to write
len
)

Overrides java.io.Writer.write.

Writes a portion of a string.
Inherited from java.io.Writer:
nullWriter

Field Detail

INSTANCEback to summary
private static final CharStreams.NullWriter INSTANCE

Constructor Detail

NullWriterback to summary
private NullWriter()

Method Detail

appendback to summary
public Writer append(CharSequence csq)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Doc from java.io.Writer.append.

Appends the specified character sequence to this writer.

An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation

out.write(csq.toString())
out.write(csq.toString())

Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.

Parameters
csq:CharSequence

The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.

Returns:Writer

This writer

Annotations
@Override
appendback to summary
public Writer append(CharSequence csq, int start, int end)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Doc from java.io.Writer.append.

Appends a subsequence of the specified character sequence to this writer. Appendable.

An invocation of this method of the form out.append(csq, start, end) when csq is not null behaves in exactly the same way as the invocation

out.write(csq.subSequence(start, end).toString())
out.write(csq.subSequence(start, end).toString())
Parameters
csq:CharSequence

The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".

start:int

The index of the first character in the subsequence

end:int

The index of the character following the last character in the subsequence

Returns:Writer

This writer

Annotations
@Override
appendback to summary
public Writer append(char c)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Doc from java.io.Writer.append.

Appends the specified character to this writer.

An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation

out.write(c)
out.write(c)
Parameters
c:char

The 16-bit character to append

Returns:Writer

This writer

Annotations
@Override
closeback to summary
public void close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Doc from java.io.Writer.close.

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Annotations
@Override
flushback to summary
public void flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Doc from java.io.Writer.flush.

Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
writeback to summary
public void write(int c)

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

Subclasses that intend to support efficient single-character output should override this method.

Parameters
c:int

int specifying a character to be written

Annotations
@Override
writeback to summary
public void write(char[] cbuf)

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

Writes an array of characters.

Parameters
cbuf:char[]

Array of characters to be written

Annotations
@Override
writeback to summary
public void write(char[] cbuf, int off, int len)

Implements abstract java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a portion of an array of characters.

Parameters
cbuf:char[]

Array of characters

off:int

Offset from which to start writing characters

len:int

Number of characters to write

Annotations
@Override
writeback to summary
public void write(String str)

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a string.

Parameters
str:String

String to be written

Annotations
@Override
writeback to summary
public void write(String str, int off, int len)

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a portion of a string.

Parameters
str:String

A String

off:int

Offset from which to start writing characters

len:int

Number of characters to write

Annotations
@Override