Class WriterOutputStream
java.lang.Object
java.io.OutputStream
cloud.opencode.base.io.stream.WriterOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
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
ConstructorsConstructorDescriptionWriterOutputStream(Writer writer) Creates a WriterOutputStream with UTF-8 decoding 使用UTF-8解码创建WriterOutputStreamWriterOutputStream(Writer writer, Charset charset) Creates a WriterOutputStream with the specified charset 使用指定的字符集创建WriterOutputStreamWriterOutputStream(Writer writer, CharsetDecoder decoder) Creates a WriterOutputStream with the specified charset decoder 使用指定的字符集解码器创建WriterOutputStream -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the stream, flushing any remaining bytes, and closes the writer 关闭流,刷新所有剩余字节,并关闭Writervoidflush()Flushes the stream by decoding any buffered bytes and flushing the writer 通过解码所有缓冲的字节并刷新Writer来刷新流voidwrite(byte[] b, int off, int len) Writes bytes from the specified buffer 从指定的缓冲区写入字节voidwrite(int b) Writes a single byte to the stream 向流中写入单个字节Methods inherited from class OutputStream
nullOutputStream, write
-
Constructor Details
-
WriterOutputStream
Creates a WriterOutputStream with the specified charset decoder 使用指定的字符集解码器创建WriterOutputStream- Parameters:
writer- the writer to adapt | 要适配的Writerdecoder- the charset decoder | 字符集解码器- Throws:
NullPointerException- if writer or decoder is null | 当writer或decoder为null时抛出
-
WriterOutputStream
Creates a WriterOutputStream with the specified charset 使用指定的字符集创建WriterOutputStream- Parameters:
writer- the writer to adapt | 要适配的Writercharset- the charset for decoding | 解码使用的字符集- Throws:
NullPointerException- if writer or charset is null | 当writer或charset为null时抛出
-
WriterOutputStream
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
Writes a single byte to the stream 向流中写入单个字节- Specified by:
writein classOutputStream- Parameters:
b- the byte to write | 要写入的字节- Throws:
IOException- if an I/O error occurs | 当发生I/O错误时抛出
-
write
Writes bytes from the specified buffer 从指定的缓冲区写入字节- Overrides:
writein classOutputStream- 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
Flushes the stream by decoding any buffered bytes and flushing the writer 通过解码所有缓冲的字节并刷新Writer来刷新流- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- if an I/O error occurs | 当发生I/O错误时抛出
-
close
Closes the stream, flushing any remaining bytes, and closes the writer 关闭流,刷新所有剩余字节,并关闭Writer- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an I/O error occurs | 当发生I/O错误时抛出
-