Package com.google.common.io
Class CharSink
- java.lang.Object
-
- com.google.common.io.CharSink
-
- All Implemented Interfaces:
OutputSupplier<java.io.Writer>
public abstract class CharSink extends java.lang.Object implements OutputSupplier<java.io.Writer>
A destination to which characters can be written, such as a text file. Unlike aWriter, aCharSinkis not an open, stateful stream that can be written to and closed. Instead, it is an immutable supplier ofWriterinstances.CharSinkprovides two kinds of methods:- Methods that return a writer: These methods should return a new, independent instance each time they are called. The caller is responsible for ensuring that the returned writer is closed.
- Convenience methods: These are implementations of common operations that are typically implemented by opening a writer using one of the methods in the first category, doing something and finally closing the writer that was opened.
Any
ByteSinkmay be viewed as aCharSinkwith a specific character encoding usingByteSink.asCharSink(Charset). Characters written to the resultingCharSinkwill written to theByteSinkas encoded bytes.- Since:
- 14.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedCharSink()Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.io.WritergetOutput()Deprecated.This method is only provided for temporary compatibility with theOutputSupplierinterface and should not be called directly.java.io.WriteropenBufferedStream()Opens a new bufferedWriterfor writing to this sink.abstract java.io.WriteropenStream()Opens a newWriterfor writing to this sink.voidwrite(java.lang.CharSequence charSequence)Writes the given character sequence to this sink.longwriteFrom(java.lang.Readable readable)Writes all the text from the givenReadable(such as aReader) to this sink.voidwriteLines(java.lang.Iterable<? extends java.lang.CharSequence> lines)Writes the given lines of text to this sink with each line (including the last) terminated with the operating system's default line separator.voidwriteLines(java.lang.Iterable<? extends java.lang.CharSequence> lines, java.lang.String lineSeparator)Writes the given lines of text to this sink with each line (including the last) terminated with the given line separator.
-
-
-
Method Detail
-
openStream
public abstract java.io.Writer openStream() throws java.io.IOExceptionOpens a newWriterfor writing to this sink. This method should return a new, independent writer each time it is called.The caller is responsible for ensuring that the returned writer is closed.
- Throws:
java.io.IOException- if an I/O error occurs in the process of opening the writer
-
getOutput
@Deprecated public final java.io.Writer getOutput() throws java.io.IOExceptionDeprecated.This method is only provided for temporary compatibility with theOutputSupplierinterface and should not be called directly. UseopenStream()instead. This method is scheduled for removal in Guava 18.0.This method is a temporary method provided for easing migration from suppliers to sources and sinks.- Specified by:
getOutputin interfaceOutputSupplier<java.io.Writer>- Throws:
java.io.IOException- Since:
- 15.0
-
openBufferedStream
public java.io.Writer openBufferedStream() throws java.io.IOExceptionOpens a new bufferedWriterfor writing to this sink. The returned stream is not required to be aBufferedWriterin order to allow implementations to simply delegate toopenStream()when the stream returned by that method does not benefit from additional buffering. This method should return a new, independent writer each time it is called.The caller is responsible for ensuring that the returned writer is closed.
- Throws:
java.io.IOException- if an I/O error occurs in the process of opening the writer- Since:
- 15.0 (in 14.0 with return type
BufferedWriter)
-
write
public void write(java.lang.CharSequence charSequence) throws java.io.IOExceptionWrites the given character sequence to this sink.- Throws:
java.io.IOException- if an I/O error in the process of writing to this sink
-
writeLines
public void writeLines(java.lang.Iterable<? extends java.lang.CharSequence> lines) throws java.io.IOExceptionWrites the given lines of text to this sink with each line (including the last) terminated with the operating system's default line separator. This method is equivalent towriteLines(lines, System.getProperty("line.separator")).- Throws:
java.io.IOException- if an I/O error occurs in the process of writing to this sink
-
writeLines
public void writeLines(java.lang.Iterable<? extends java.lang.CharSequence> lines, java.lang.String lineSeparator) throws java.io.IOExceptionWrites the given lines of text to this sink with each line (including the last) terminated with the given line separator.- Throws:
java.io.IOException- if an I/O error occurs in the process of writing to this sink
-
writeFrom
public long writeFrom(java.lang.Readable readable) throws java.io.IOExceptionWrites all the text from the givenReadable(such as aReader) to this sink. Does not closereadableif it isCloseable.- Throws:
java.io.IOException- if an I/O error occurs in the process of reading fromreadableor writing to this sink
-
-