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.
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 Summary
Modifier and TypeMethodDescriptiondefault CharSinkReturns a sink that writes characters to this one, encoded in the current default charset.default CharSinkasCharSink(Charset charset) Returns a sink that writes characters to this one, encoded in a charset.static ByteSinkof(Path file, OpenOption... options) Returns a sink that writes a file.default OutputStreamOpens a new buffered stream to write the bytes of this sink.Opens a new stream to write the bytes of this sink.default voidwrite(byte[] bytes) Writes bytes to this sink.default longwriteFrom(InputStream in) Writes the bytes of a stream to this sink, up to its end.
-
Method Details
-
of
Returns a sink that writes a file. By default, each write creates the file or replaces its content, but it is not atomic: seeAtomicFilesfor that.- Parameters:
file- the file to writeoptions- how to open the file, as forFiles.newOutputStream(Path, OpenOption...)- Returns:
- a sink that writes
file
-
openStream
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
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
Writes bytes to this sink.- Parameters:
bytes- the bytes to write- Throws:
IOException- if this sink cannot be written
-
writeFrom
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- ifincannot be read or this sink cannot be written
-
asCharSink
Returns a sink that writes characters to this one, encoded in the current default charset. Writing to it fails with aCharacterCodingExceptionon 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
Returns a sink that writes characters to this one, encoded in a charset. Writing to it fails with aCharacterCodingExceptionon characterscharsetcannot 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- ifcharsetdoes not support encoding
-