Class ByteSink

java.lang.Object
cloud.opencode.base.io.source.ByteSink

public abstract class ByteSink extends Object
Abstract Byte Sink - A writable destination for bytes 抽象字节接收器 - 可写的字节目标

An abstraction over different destinations for byte data. Counterpart to ByteSource.

对不同字节数据目标的抽象。是ByteSource的对应物。

Features | 主要功能:

  • Unified byte writing - 统一的字节写入
  • Write from InputStream - 从输入流写入
  • Write byte arrays - 写入字节数组
  • Support for append mode - 支持追加模式

Usage Examples | 使用示例:

// Write to file
ByteSink sink = ByteSink.toPath(Paths.get("output.bin"));
sink.write(data);

// Copy from source to sink
ByteSource source = ByteSource.fromPath(Paths.get("input.bin"));
source.copyTo(sink);

Security | 安全性:

  • Thread-safe: No, not safe for concurrent writes - 线程安全: 否,不支持并发写入
  • Null-safe: No, data must not be null - 空值安全: 否,数据不可为null
Since:
JDK 25, opencode-base-io V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ByteSink

      public ByteSink()
  • Method Details

    • openStream

      public abstract OutputStream openStream()
      Opens a new OutputStream for writing. 打开新的输出流进行写入。

      The caller is responsible for closing the stream.

      调用者负责关闭流。

      Returns:
      a new OutputStream | 新的输出流
      Throws:
      OpenIOOperationException - if the stream cannot be opened | 如果流无法打开
    • asCharSink

      public CharSink asCharSink(Charset charset)
      Returns a view of this sink as a CharSink with the given charset. 返回使用给定字符集作为CharSink的此接收器视图。
      Parameters:
      charset - the charset to use | 要使用的字符集
      Returns:
      a CharSink view | CharSink视图
    • asCharSink

      public CharSink asCharSink()
      Returns a view of this sink as a CharSink with UTF-8 charset. 返回使用UTF-8字符集作为CharSink的此接收器视图。
      Returns:
      a CharSink view | CharSink视图
    • write

      public void write(byte[] bytes)
      Writes all bytes from a byte array. 从字节数组写入所有字节。
      Parameters:
      bytes - the bytes to write | 要写入的字节
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • writeFrom

      public long writeFrom(InputStream input)
      Writes all bytes from an InputStream. 从输入流写入所有字节。
      Parameters:
      input - the input stream | 输入流
      Returns:
      the number of bytes written | 写入的字节数
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • toPath

      public static ByteSink toPath(Path path)
      Creates a ByteSink for writing to a file path. 创建用于写入文件路径的ByteSink。
      Parameters:
      path - the file path | 文件路径
      Returns:
      a ByteSink | ByteSink
    • toPathAppend

      public static ByteSink toPathAppend(Path path)
      Creates a ByteSink for appending to a file path. 创建用于追加到文件路径的ByteSink。
      Parameters:
      path - the file path | 文件路径
      Returns:
      a ByteSink | ByteSink
    • nullSink

      public static ByteSink nullSink()
      Creates a ByteSink that discards all written data. 创建丢弃所有写入数据的ByteSink。
      Returns:
      a null ByteSink | 空ByteSink