Class CharStreams
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.
- Since:
- 1.0
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic WriterasWriter(Appendable target) Returns a Writer that sends all output to the givenAppendabletarget.static longcopy(Readable from, Appendable to) Copies all characters between theReadableandAppendableobjects.(package private) static longcopyReaderToBuilder(Reader from, StringBuilder to) Copies all characters between theReaderandStringBuilderobjects.(package private) static longcopyReaderToWriter(Reader from, Writer to) (package private) static CharBufferCreates a newCharBufferfor buffering reads or writes.static longReads and discards data from the givenReadableuntil the end of the stream is reached.static WriterReturns aWriterthat simply discards written chars.Reads all of the lines from aReadableobject.static <T> TreadLines(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 voidDiscardsncharacters of data from the reader.static Stringprivate static StringBuilderReads all characters from aReadableobject into a newStringBuilderinstance.
-
Field Details
-
DEFAULT_BUF_SIZE
private static final int DEFAULT_BUF_SIZE- See Also:
-
-
Constructor Details
-
CharStreams
private CharStreams()
-
-
Method Details
-
createBuffer
Creates a newCharBufferfor buffering reads or writes. -
copy
Copies 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:
IOException- if an I/O error occurs
-
copyReaderToBuilder
Copies all characters between theReaderandStringBuilderobjects. 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:
IOException- if an I/O error occurs
-
copyReaderToWriter
Copies all characters between theReaderandWriterobjects. 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:
IOException- if an I/O error occurs
-
toString
- Parameters:
r- the object to read from- Returns:
- a string containing all the characters
- Throws:
IOException- if an I/O error occurs
-
toStringBuilder
Reads all characters from aReadableobject into a newStringBuilderinstance. Does not close theReadable.- Parameters:
r- the object to read from- Returns:
- a
StringBuildercontaining all the characters - Throws:
IOException- if an I/O error occurs
-
readLines
Reads 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:
IOException- if an I/O error occurs
-
readLines
Streams 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:
IOException- if an I/O error occurs- Since:
- 14.0
-
exhaust
Reads and discards data from the givenReadableuntil the end of the stream is reached. Returns the total number of chars read. Does not close the stream.- Throws:
IOException- Since:
- 20.0
-
skipFully
Discardsncharacters 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:
EOFException- if this stream reaches the end before skipping all the charactersIOException- if an I/O error occurs
-
nullWriter
Returns aWriterthat simply discards written chars.- Since:
- 15.0
-
asWriter
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
-