Class WriterOutputStream

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

public class WriterOutputStream extends OutputStream
Writer to OutputStream Adapter Writer到OutputStream的适配器

Adapts a Writer to behave as an OutputStream by decoding bytes to characters using a CharsetDecoder. This is the inverse of OutputStreamWriter.

通过使用CharsetDecoder将字节解码为字符,将Writer适配为 OutputStream。这是OutputStreamWriter的逆操作。

Features | 主要功能:

  • Converts Writer to OutputStream - 将Writer转换为OutputStream
  • Configurable charset decoding - 可配置的字符集解码
  • Buffered byte accumulation - 缓冲字节累积
  • Proper decoder flushing on close - 关闭时正确刷新解码器

Usage Examples | 使用示例:

// Convert a StringWriter to OutputStream (UTF-8)
StringWriter sw = new StringWriter();
try (OutputStream os = new WriterOutputStream(sw)) {
    os.write("Hello".getBytes(StandardCharsets.UTF_8));
}
String result = sw.toString();

// With specific charset
try (OutputStream os = new WriterOutputStream(sw, StandardCharsets.ISO_8859_1)) {
    os.write(data);
}

Security | 安全性:

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

    Constructors
    Constructor
    Description
    Creates a WriterOutputStream with UTF-8 decoding 使用UTF-8解码创建WriterOutputStream
    WriterOutputStream(Writer writer, Charset charset)
    Creates a WriterOutputStream with the specified charset 使用指定的字符集创建WriterOutputStream
    Creates a WriterOutputStream with the specified charset decoder 使用指定的字符集解码器创建WriterOutputStream
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the stream, flushing any remaining bytes, and closes the writer 关闭流,刷新所有剩余字节,并关闭Writer
    void
    Flushes the stream by decoding any buffered bytes and flushing the writer 通过解码所有缓冲的字节并刷新Writer来刷新流
    void
    write(byte[] b, int off, int len)
    Writes bytes from the specified buffer 从指定的缓冲区写入字节
    void
    write(int b)
    Writes a single byte to the stream 向流中写入单个字节

    Methods inherited from class OutputStream

    nullOutputStream, write

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WriterOutputStream

      public WriterOutputStream(Writer writer, CharsetDecoder decoder)
      Creates a WriterOutputStream with the specified charset decoder 使用指定的字符集解码器创建WriterOutputStream
      Parameters:
      writer - the writer to adapt | 要适配的Writer
      decoder - the charset decoder | 字符集解码器
      Throws:
      NullPointerException - if writer or decoder is null | 当writer或decoder为null时抛出
    • WriterOutputStream

      public WriterOutputStream(Writer writer, Charset charset)
      Creates a WriterOutputStream with the specified charset 使用指定的字符集创建WriterOutputStream
      Parameters:
      writer - the writer to adapt | 要适配的Writer
      charset - the charset for decoding | 解码使用的字符集
      Throws:
      NullPointerException - if writer or charset is null | 当writer或charset为null时抛出
    • WriterOutputStream

      public WriterOutputStream(Writer writer)
      Creates a WriterOutputStream with UTF-8 decoding 使用UTF-8解码创建WriterOutputStream
      Parameters:
      writer - the writer to adapt | 要适配的Writer
      Throws:
      NullPointerException - if writer is null | 当writer为null时抛出
  • Method Details

    • write

      public void write(int b) throws IOException
      Writes a single byte to the stream 向流中写入单个字节
      Specified by:
      write in class OutputStream
      Parameters:
      b - the byte to write | 要写入的字节
      Throws:
      IOException - if an I/O error occurs | 当发生I/O错误时抛出
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Writes bytes from the specified buffer 从指定的缓冲区写入字节
      Overrides:
      write in class OutputStream
      Parameters:
      b - the buffer containing bytes to write | 包含要写入字节的缓冲区
      off - the start offset in the buffer | 缓冲区中的起始偏移量
      len - the number of bytes to write | 要写入的字节数
      Throws:
      IOException - if an I/O error occurs | 当发生I/O错误时抛出
      IndexOutOfBoundsException - if off or len is invalid | 当off或len无效时抛出
    • flush

      public void flush() throws IOException
      Flushes the stream by decoding any buffered bytes and flushing the writer 通过解码所有缓冲的字节并刷新Writer来刷新流
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - if an I/O error occurs | 当发生I/O错误时抛出
    • close

      public void close() throws IOException
      Closes the stream, flushing any remaining bytes, and closes the writer 关闭流,刷新所有剩余字节,并关闭Writer
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - if an I/O error occurs | 当发生I/O错误时抛出