Class FastByteArrayOutputStream

java.lang.Object
java.io.OutputStream
cloud.opencode.base.io.stream.FastByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class FastByteArrayOutputStream extends OutputStream
Fast Byte Array Output Stream 高性能字节数组输出流

High-performance byte array output stream that minimizes unnecessary array copying operations.

高性能字节数组输出流,最小化不必要的数组复制操作。

Features | 主要功能:

  • Efficient memory usage - 高效内存使用
  • Direct buffer access - 直接缓冲区访问
  • No synchronization overhead - 无同步开销

Usage Examples | 使用示例:

FastByteArrayOutputStream out = new FastByteArrayOutputStream();
out.write("Hello".getBytes());
byte[] data = out.toByteArray();

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • FastByteArrayOutputStream

      public FastByteArrayOutputStream()
      Creates a stream with default capacity 创建默认容量的流
    • FastByteArrayOutputStream

      public FastByteArrayOutputStream(int initialCapacity)
      Creates a stream with specified initial capacity 创建指定初始容量的流
      Parameters:
      initialCapacity - the initial capacity | 初始容量
  • Method Details

    • write

      public void write(int b)
      Specified by:
      write in class OutputStream
    • write

      public void write(byte[] b, int off, int len)
      Overrides:
      write in class OutputStream
    • size

      public int size()
      Gets the current size 获取当前大小
      Returns:
      byte count | 字节数
    • toByteArray

      public byte[] toByteArray()
      Gets the byte array (may share internal buffer) 获取字节数组(可能共享内部缓冲区)

      Note: If the buffer is exactly the right size, the internal buffer is returned directly. Modifying it will affect this stream.

      注意:如果缓冲区大小刚好合适,将直接返回内部缓冲区。 修改它会影响此流。

      Returns:
      byte array | 字节数组
    • toByteArrayCopy

      public byte[] toByteArrayCopy()
      Gets a copy of the byte array 获取字节数组副本
      Returns:
      byte array copy | 字节数组副本
    • writeTo

      public void writeTo(OutputStream out)
      Writes content to another output stream 将内容写入另一个输出流
      Parameters:
      out - the target output stream | 目标输出流
    • reset

      public void reset()
      Resets the stream 重置流
    • toInputStream

      public InputStream toInputStream()
      Converts to input stream 转换为输入流
      Returns:
      input stream | 输入流
    • getBuffer

      public byte[] getBuffer()
      Gets the internal buffer (for advanced use) 获取内部缓冲区(高级用法)
      Returns:
      internal buffer | 内部缓冲区
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String charsetName)
      Converts to string with charset 使用字符集转换为字符串
      Parameters:
      charsetName - the charset name | 字符集名称
      Returns:
      string | 字符串