saker.util Documentation TaskDoc JavaDoc Packages
public interface ByteSink extends Closeable, Flushable
Interface for handling byte stream output.

Byte sink is similar to OutputStream, but is defined as an interface for allowing stream operations over RMI. RMI solutions can create proxy objects for network transferred objects only for interfaces, therefore it was necessary to declare such an interface that is usable for this use-case.

This interface works in similar ways as the OutputStream class, but is designed to be RMI compatible. The interface also contains extra method(s) for more efficient implementations of common use-cases (like readFrom(ByteSource)).

To convert between OutputStream and ByteSink objects use the static methods declared in this interface.

Byte sink implementations are not thread-safe by default.

Methods
public default void
Closes this stream and releases any system resources associated with it.
public default void
Flushes this stream by writing any buffered output to the underlying stream.
public default long
Reads bytes from the argument byte source and writes it to this byte sink.
public static OutputStream
Converts the argument ByteSink to an OutputStream.
public static ByteSink
Converts the argument ObjectOutput to a ByteSink.
public static ByteSink
Converts the argument OutputStream to a ByteSink.
public default void
write(int b)
Writes a single byte to the byte sink.
public void
Writes the bytes contained in the argument byte array to the byte sink.
public default void close() throws IOException
Overridden from: Closeable
Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable.close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

IOExceptionif an I/O error occurs
public default void flush() throws IOException
Overridden from: Flushable
Flushes this stream by writing any buffered output to the underlying stream.
IOExceptionIf an I/O error occurs
Reads bytes from the argument byte source and writes it to this byte sink.

This method will possibly read all bytes from the argument byte source and all the read bytes will be written to this byte sink. If the argument is a blocking source, then this method will block too.

Calling this method instead of copying the bytes externally can have advantages, as implementations can read the bytes into an internal buffer more efficiently, therefore avoiding unnecessary copying and allocations.

The default implementation calls ByteSource.writeTo(ByteSink) of the argument.

inThe byte source to read the input from.
The number of bytes read and written to this sink.
IOExceptionIn case of I/O error.
NullPointerExceptionIf the argument is null.
Converts the argument ByteSink to an OutputStream.

If the argument is already an OutputStream, then it will be returned without modification.

Else it will be wrapped into a forwarding OutputStream.

Closing the result will close the argument too.

sinkThe byte sink.
The output stream that uses the passed byte sink argument, or null if the argument is null.
public static ByteSink valueOf(ObjectOutput os)
Converts the argument ObjectOutput to a ByteSink.

If the argument is already a byte sink, then it will be returned without modification.

Else it will be wrapped into a forwarding ByteSink.

Closing the result will close the argument.

osThe object output.
The byte sink that uses the passed output argument, or null if the argument is null.
public static ByteSink valueOf(OutputStream os)
Converts the argument OutputStream to a ByteSink.

If the argument is already a byte sink, then it will be returned without modification.

Else it will be wrapped into a forwarding ByteSink.

Closing the result will close the argument too.

osThe output stream.
The byte sink that uses the passed output stream argument, or null if the argument is null.
public default void write(int b) throws IOException
Writes a single byte to the byte sink.

This method works similarly to OutputStream.write(int).

bThe byte.
IOExceptionIn case of I/O error.
Writes the bytes contained in the argument byte array to the byte sink.

This method works similarly to OutputStream.write(byte[], int, int).

bufThe bytes to write.
IOExceptionIn case of I/O error.
NullPointerExceptionIf the argument is null.