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

public abstract Class ByteSink

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.io.Files.FileByteSink, com.google.common.io.MoreFiles.PathByteSink
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

A destination to which bytes can be written, such as a file. Unlike an OutputStream, a ByteSink is not an open, stateful stream that can be written to and closed. Instead, it is an immutable supplier of OutputStream instances.

ByteSink provides two kinds of methods:

Author
Colin Decker
Since
14.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
ByteSink.AsCharSink

A char sink that encodes written characters with a charset and writes resulting bytes to this byte sink.

Constructor Summary

AccessConstructor and Description
protected
ByteSink()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public CharSink
asCharSink(Charset charset)

Returns a CharSink view of this ByteSink that writes characters to this sink as bytes encoded with the given charset.

public OutputStream
openBufferedStream()

Opens a new buffered OutputStream for writing to this sink.

public abstract OutputStream
openStream()

Opens a new OutputStream for writing to this sink.

public void
write(byte[] bytes)

Writes all the given bytes to this sink.

public long

Returns:

the number of bytes written
writeFrom
(InputStream input)

Writes all the bytes from the given InputStream to this sink.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ByteSinkback to summary
protected ByteSink()

Constructor for use by subclasses.

Method Detail

asCharSinkback to summary
public CharSink asCharSink(Charset charset)

Returns a CharSink view of this ByteSink that writes characters to this sink as bytes encoded with the given charset.

openBufferedStreamback to summary
public OutputStream openBufferedStream() throws IOException

Opens a new buffered OutputStream for writing to this sink. The returned stream is not required to be a BufferedOutputStream in order to allow implementations to simply delegate to openStream() when the stream returned by that method does not benefit from additional buffering (for example, a ByteArrayOutputStream). This method returns a new, independent stream each time it is called.

The caller is responsible for ensuring that the returned stream is closed.

Exceptions
IOException:
if an I/O error occurs while opening the stream
Since
15.0 (in 14.0 with return type BufferedOutputStream)
openStreamback to summary
public abstract OutputStream openStream() throws IOException

Opens a new OutputStream 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.

Exceptions
IOException:
if an I/O error occurs while opening the stream
writeback to summary
public void write(byte[] bytes) throws IOException

Writes all the given bytes to this sink.

Exceptions
IOException:
if an I/O occurs while writing to this sink
writeFromback to summary
public long writeFrom(InputStream input) throws IOException

Writes all the bytes from the given InputStream to this sink. Does not close input.

Returns:long

the number of bytes written

Annotations
@CanIgnoreReturnValue
Exceptions
IOException:
if an I/O occurs while reading from input or writing to this sink
com.google.common.io back to summary

private final Class ByteSink.AsCharSink

extends CharSink
Class Inheritance

A char sink that encodes written characters with a charset and writes resulting bytes to this byte sink.

Field Summary

Modifier and TypeField and Description
private final Charset

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Writer
openStream()

Implements abstract com.google.common.io.CharSink.openStream.

Opens a new Writer for writing to this sink.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from com.google.common.io.CharSink:
openBufferedStreamwritewriteFromwriteLineswriteLineswriteLineswriteLines

Field Detail

charsetback to summary
private final Charset charset

Constructor Detail

AsCharSinkback to summary
private AsCharSink(Charset charset)

Method Detail

openStreamback to summary
public Writer openStream() throws IOException

Implements abstract com.google.common.io.CharSink.openStream.

Doc from com.google.common.io.CharSink.openStream.

Opens a new Writer for writing to this sink. This method returns a new, independent writer each time it is called.

The caller is responsible for ensuring that the returned writer is closed.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs while opening the writer
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