Interface ByteSink

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ByteSink
A writable destination of bytes, such as a file, that opens a new stream each time it is written.

Unlike an OutputStream, a sink is not an open resource: it can be kept, passed around and written any number of times, and each write method opens a stream and closes it before returning. Only openStream() and openBufferedStream() leave a stream for the caller to close.

The only abstract method is openStream(), so a lambda makes a sink:

ByteSink sink = () -> socket.getOutputStream();
ByteSink.of(file).write(bytes);
ByteSink.of(log, StandardOpenOption.CREATE, StandardOpenOption.APPEND).write(entry);
Since:
0.1.1
  • Method Details

    • of

      static ByteSink of(Path file, OpenOption... options)
      Returns a sink that writes a file. By default, each write creates the file or replaces its content, but it is not atomic: see AtomicFiles for that.
      Parameters:
      file - the file to write
      options - how to open the file, as for Files.newOutputStream(Path, OpenOption...)
      Returns:
      a sink that writes file
    • openStream

      OutputStream openStream() throws IOException
      Opens a new stream to write the bytes of this sink. The caller must close it.
      Returns:
      a new stream
      Throws:
      IOException - if the stream cannot be opened
    • openBufferedStream

      default OutputStream openBufferedStream() throws IOException
      Opens a new buffered stream to write the bytes of this sink. The caller must close it.
      Returns:
      a new buffered stream
      Throws:
      IOException - if the stream cannot be opened
    • write

      default void write(byte[] bytes) throws IOException
      Writes bytes to this sink.
      Parameters:
      bytes - the bytes to write
      Throws:
      IOException - if this sink cannot be written
    • writeFrom

      default long writeFrom(InputStream in) throws IOException
      Writes the bytes of a stream to this sink, up to its end. The stream is left open.
      Parameters:
      in - the stream to read
      Returns:
      the number of bytes written
      Throws:
      IOException - if in cannot be read or this sink cannot be written
    • asCharSink

      default CharSink asCharSink()
      Returns a sink that writes characters to this one, encoded in the current default charset. Writing to it fails with a CharacterCodingException on characters that charset cannot encode, or on a malformed surrogate pair, rather than replace them.
      Returns:
      a sink of characters
      Since:
      0.1.2
      See Also:
    • asCharSink

      default CharSink asCharSink(Charset charset)
      Returns a sink that writes characters to this one, encoded in a charset. Writing to it fails with a CharacterCodingException on characters charset cannot encode, or on a malformed surrogate pair, rather than replace them.
      Parameters:
      charset - the charset to encode the characters in
      Returns:
      a sink of characters
      Throws:
      IllegalArgumentException - if charset does not support encoding