Class ReaderInputStream

java.lang.Object
java.io.InputStream
cloud.opencode.base.io.stream.ReaderInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class ReaderInputStream extends InputStream
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 Details

    • ReaderInputStream

      public ReaderInputStream(Reader reader, CharsetEncoder encoder)
      Creates a ReaderInputStream with the specified charset encoder 使用指定的字符集编码器创建ReaderInputStream
      Parameters:
      reader - the reader to adapt | 要适配的Reader
      encoder - the charset encoder | 字符集编码器
      Throws:
      NullPointerException - if reader or encoder is null | 当reader或encoder为null时抛出
    • ReaderInputStream

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

      public ReaderInputStream(Reader reader)
      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

      public int read() throws IOException
      Reads a single byte from the stream 从流中读取单个字节
      Specified by:
      read in class InputStream
      Returns:
      the byte read, or -1 if end of stream | 读取的字节,如果到达流末尾则返回-1
      Throws:
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Reads bytes into the specified buffer 将字节读入指定的缓冲区
      Overrides:
      read in class InputStream
      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

      public void close() throws IOException
      Closes the stream and the underlying reader 关闭流和底层的Reader
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - if an I/O error occurs | 当发生I/O错误时抛出