Class ReaderInputStream
java.lang.Object
java.io.InputStream
cloud.opencode.base.io.stream.ReaderInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable
Reader to InputStream Adapter
Reader到InputStream的适配器
Adapts a Reader to behave as an InputStream by encoding
characters to bytes using a CharsetEncoder. This is the inverse of
InputStreamReader.
通过使用CharsetEncoder将字符编码为字节,将Reader适配为
InputStream。这是InputStreamReader的逆操作。
Features | 主要功能:
- Converts Reader to InputStream - 将Reader转换为InputStream
- Configurable charset encoding - 可配置的字符集编码
- Efficient buffered conversion - 高效的缓冲转换
- Proper encoder flushing on stream end - 流结束时正确刷新编码器
Usage Examples | 使用示例:
// Convert a StringReader to InputStream (UTF-8)
Reader reader = new StringReader("Hello World");
try (InputStream is = new ReaderInputStream(reader)) {
byte[] data = is.readAllBytes();
}
// With specific charset
try (InputStream is = new ReaderInputStream(reader, StandardCharsets.ISO_8859_1)) {
process(is);
}
Security | 安全性:
- Thread-safe: No - 线程安全: 否
- Since:
- JDK 25, opencode-base-io V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionReaderInputStream(Reader reader) Creates a ReaderInputStream with UTF-8 encoding 使用UTF-8编码创建ReaderInputStreamReaderInputStream(Reader reader, Charset charset) Creates a ReaderInputStream with the specified charset 使用指定的字符集创建ReaderInputStreamReaderInputStream(Reader reader, CharsetEncoder encoder) Creates a ReaderInputStream with the specified charset encoder 使用指定的字符集编码器创建ReaderInputStream -
Method Summary
Methods inherited from class InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
ReaderInputStream
Creates a ReaderInputStream with the specified charset encoder 使用指定的字符集编码器创建ReaderInputStream- Parameters:
reader- the reader to adapt | 要适配的Readerencoder- the charset encoder | 字符集编码器- Throws:
NullPointerException- if reader or encoder is null | 当reader或encoder为null时抛出
-
ReaderInputStream
Creates a ReaderInputStream with the specified charset 使用指定的字符集创建ReaderInputStream- Parameters:
reader- the reader to adapt | 要适配的Readercharset- the charset for encoding | 编码使用的字符集- Throws:
NullPointerException- if reader or charset is null | 当reader或charset为null时抛出
-
ReaderInputStream
Creates a ReaderInputStream with UTF-8 encoding 使用UTF-8编码创建ReaderInputStream- Parameters:
reader- the reader to adapt | 要适配的Reader- Throws:
NullPointerException- if reader is null | 当reader为null时抛出
-
-
Method Details
-
read
Reads a single byte from the stream 从流中读取单个字节- Specified by:
readin classInputStream- Returns:
- the byte read, or -1 if end of stream | 读取的字节,如果到达流末尾则返回-1
- Throws:
OpenIOOperationException- if an I/O error occurs | 当发生I/O错误时抛出IOException
-
read
Reads bytes into the specified buffer 将字节读入指定的缓冲区- Overrides:
readin classInputStream- Parameters:
b- the buffer to read into | 读入的缓冲区off- the start offset in the buffer | 缓冲区中的起始偏移量len- the maximum number of bytes to read | 要读取的最大字节数- Returns:
- the number of bytes read, or -1 if end of stream | 读取的字节数,如果到达流末尾则返回-1
- Throws:
IOException- if an I/O error occurs | 当发生I/O错误时抛出IndexOutOfBoundsException- if off or len is invalid | 当off或len无效时抛出
-
close
Closes the stream and the underlying reader 关闭流和底层的Reader- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if an I/O error occurs | 当发生I/O错误时抛出
-