Package com.google.common.io
Class ByteSink
- java.lang.Object
-
- com.google.common.io.ByteSink
-
- All Implemented Interfaces:
OutputSupplier<java.io.OutputStream>
public abstract class ByteSink extends java.lang.Object implements OutputSupplier<java.io.OutputStream>
A destination to which bytes can be written, such as a file. Unlike anOutputStream
, aByteSink
is not an open, stateful stream that can be written to and closed. Instead, it is an immutable supplier ofOutputStream
instances.ByteSink
provides two kinds of methods:- Methods that return a stream: These methods should return a new, independent instance each time they are called. The caller is responsible for ensuring that the returned stream is closed.
- Convenience methods: These are implementations of common operations that are typically implemented by opening a stream using one of the methods in the first category, doing something and finally closing the stream or channel that was opened.
- Since:
- 14.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ByteSink()
Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CharSink
asCharSink(java.nio.charset.Charset charset)
Returns aCharSink
view of thisByteSink
that writes characters to this sink as bytes encoded with the givencharset
.java.io.OutputStream
getOutput()
Deprecated.This method is only provided for temporary compatibility with theOutputSupplier
interface and should not be called directly.java.io.OutputStream
openBufferedStream()
Opens a new bufferedOutputStream
for writing to this sink.abstract java.io.OutputStream
openStream()
Opens a newOutputStream
for writing to this sink.void
write(byte[] bytes)
Writes all the given bytes to this sink.long
writeFrom(java.io.InputStream input)
Writes all the bytes from the givenInputStream
to this sink.
-
-
-
Method Detail
-
asCharSink
public CharSink asCharSink(java.nio.charset.Charset charset)
Returns aCharSink
view of thisByteSink
that writes characters to this sink as bytes encoded with the givencharset
.
-
openStream
public abstract java.io.OutputStream openStream() throws java.io.IOException
Opens a newOutputStream
for writing to this sink. This method should return a new, independent stream each time it is called.The caller is responsible for ensuring that the returned stream is closed.
- Throws:
java.io.IOException
- if an I/O error occurs in the process of opening the stream
-
getOutput
@Deprecated public final java.io.OutputStream getOutput() throws java.io.IOException
Deprecated.This method is only provided for temporary compatibility with theOutputSupplier
interface 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:
getOutput
in interfaceOutputSupplier<java.io.OutputStream>
- Throws:
java.io.IOException
- Since:
- 15.0
-
openBufferedStream
public java.io.OutputStream openBufferedStream() throws java.io.IOException
Opens a new bufferedOutputStream
for writing to this sink. The returned stream is not required to be aBufferedOutputStream
in order to allow implementations to simply delegate toopenStream()
when the stream returned by that method does not benefit from additional buffering (for example, aByteArrayOutputStream
). This method should return a new, independent stream each time it is called.The caller is responsible for ensuring that the returned stream is closed.
- Throws:
java.io.IOException
- if an I/O error occurs in the process of opening the stream- Since:
- 15.0 (in 14.0 with return type
BufferedOutputStream
)
-
write
public void write(byte[] bytes) throws java.io.IOException
Writes all the given bytes to this sink.- Throws:
java.io.IOException
- if an I/O occurs in the process of writing to this sink
-
writeFrom
public long writeFrom(java.io.InputStream input) throws java.io.IOException
Writes all the bytes from the givenInputStream
to this sink. Does not closeinput
.- Throws:
java.io.IOException
- if an I/O occurs in the process of reading frominput
or writing to this sink
-
-