Package com.google.common.io
Class ByteSink
-
- com.google.common.io.ByteSink
-
@GwtIncompatible public abstract class ByteSink extends
A destination to which bytes can be written, such as a file. Unlike an , 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
- Author:
- Colin Decker
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ByteSink()
Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description CharSink
charset)
Returns aCharSink
view of thisByteSink
that writes characters to this sink as bytes encoded with the given .openBufferedStream()
Opens a new buffered for writing to this sink.abstract
openStream()
Opens a new for writing to this sink.void
write(byte[] bytes)
Writes all the given bytes to this sink.long
input)
Writes all the bytes from the givenInputStream
to this sink.
-
-
-
Constructor Detail
-
ByteSink
protected ByteSink()
Constructor for use by subclasses.
-
-
Method Detail
-
asCharSink
public charset)
Returns aCharSink
view of thisByteSink
that writes characters to this sink as bytes encoded with the given .
-
openStream
public abstract openStream() throws
Opens a new for writing to this sink. This method returns a new, independent stream each time it is called.The caller is responsible for ensuring that the returned stream is closed.
- Throws:
- if an I/O error occurs while opening the stream
-
openBufferedStream
public openBufferedStream() throws
Opens a new buffered for writing to this sink. The returned stream is not required to be a 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 returns a new, independent stream each time it is called.The caller is responsible for ensuring that the returned stream is closed.
- Throws:
- if an I/O error occurs while opening the stream
- Since:
- 15.0 (in 14.0 with return type )
-
write
public void write(byte[] bytes) throws
Writes all the given bytes to this sink.- Throws:
- if an I/O occurs while writing to this sink
-
writeFrom
public long input) throws
Writes all the bytes from the givenInputStream
to this sink. Does not closeinput
.- Returns:
- the number of bytes written
- Throws:
- if an I/O occurs while reading from
input
or writing to this sink
-
-