Class OpenStream

java.lang.Object
cloud.opencode.base.io.OpenStream

public final class OpenStream extends Object
Stream Processing Utility Class 流处理工具类

Utility class for stream operations including copying, reading, writing, and stream transformation.

用于流操作的工具类,包括复制、读取、写入和流转换。

Features | 主要功能:

  • Stream copying - 流复制
  • Stream reading - 流读取
  • Stream transformation - 流转换
  • Safe closing - 安全关闭

Usage Examples | 使用示例:

// Copy stream
long copied = OpenStream.copy(input, output);

// Read as bytes
byte[] data = OpenStream.toByteArray(input);

// Read as string
String content = OpenStream.toString(input);

// Safe close
OpenStream.closeQuietly(stream1, stream2);

Security | 安全性:

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

    • DEFAULT_BUFFER_SIZE

      public static final int DEFAULT_BUFFER_SIZE
      Default buffer size (8KB) 默认缓冲区大小(8KB)
      See Also:
  • Method Details

    • copy

      public static long copy(InputStream input, OutputStream output)
      Copies input stream to output stream 复制输入流到输出流
      Parameters:
      input - the input stream | 输入流
      output - the output stream | 输出流
      Returns:
      bytes copied | 复制的字节数
    • copy

      public static long copy(InputStream input, OutputStream output, int bufferSize)
      Copies input stream to output stream with buffer size 使用指定缓冲区大小复制输入流到输出流
      Parameters:
      input - the input stream | 输入流
      output - the output stream | 输出流
      bufferSize - the buffer size | 缓冲区大小
      Returns:
      bytes copied | 复制的字节数
    • copy

      public static long copy(Reader reader, Writer writer)
      Copies Reader to Writer 复制Reader到Writer
      Parameters:
      reader - the reader | Reader
      writer - the writer | Writer
      Returns:
      characters copied | 复制的字符数
    • copyToFile

      public static long copyToFile(InputStream input, Path file)
      Copies input stream to file 复制输入流到文件
      Parameters:
      input - the input stream | 输入流
      file - the target file | 目标文件
      Returns:
      bytes copied | 复制的字节数
    • toByteArray

      public static byte[] toByteArray(InputStream input)
      Reads input stream as byte array 读取输入流为字节数组
      Parameters:
      input - the input stream | 输入流
      Returns:
      byte array | 字节数组
    • toByteArray

      public static byte[] toByteArray(InputStream input, int maxSize)
      Reads input stream as byte array with size limit 读取输入流为字节数组(带大小限制)
      Parameters:
      input - the input stream | 输入流
      maxSize - the max size | 最大大小
      Returns:
      byte array | 字节数组
    • toString

      public static String toString(InputStream input, Charset charset)
      Reads input stream as string 读取输入流为字符串
      Parameters:
      input - the input stream | 输入流
      charset - the charset | 字符集
      Returns:
      content string | 内容字符串
    • toString

      public static String toString(InputStream input)
      Reads input stream as UTF-8 string 读取输入流为UTF-8字符串
      Parameters:
      input - the input stream | 输入流
      Returns:
      content string | 内容字符串
    • toString

      public static String toString(Reader reader)
      Reads reader as string 读取Reader为字符串
      Parameters:
      reader - the reader | Reader
      Returns:
      content string | 内容字符串
    • write

      public static void write(byte[] data, OutputStream output)
      Writes byte array to output stream 写入字节数组到输出流
      Parameters:
      data - the data | 数据
      output - the output stream | 输出流
    • write

      public static void write(String data, OutputStream output, Charset charset)
      Writes string to output stream 写入字符串到输出流
      Parameters:
      data - the data | 数据
      output - the output stream | 输出流
      charset - the charset | 字符集
    • write

      public static void write(String data, Writer writer)
      Writes string to writer 写入字符串到Writer
      Parameters:
      data - the data | 数据
      writer - the writer | Writer
    • closeQuietly

      public static void closeQuietly(Closeable closeable)
      Closes safely, ignoring exceptions 安全关闭,忽略异常
      Parameters:
      closeable - the closeable | 可关闭对象
    • closeQuietly

      public static void closeQuietly(Closeable... closeables)
      Closes multiple closeables safely 安全关闭多个可关闭对象
      Parameters:
      closeables - the closeables | 可关闭对象数组
    • buffer

      public static BufferedInputStream buffer(InputStream input)
      Wraps as buffered input stream 包装为缓冲输入流
      Parameters:
      input - the input stream | 输入流
      Returns:
      buffered input stream | 缓冲输入流
    • buffer

      public static BufferedOutputStream buffer(OutputStream output)
      Wraps as buffered output stream 包装为缓冲输出流
      Parameters:
      output - the output stream | 输出流
      Returns:
      buffered output stream | 缓冲输出流
    • toReader

      public static Reader toReader(InputStream input, Charset charset)
      Converts input stream to reader 将输入流转换为Reader
      Parameters:
      input - the input stream | 输入流
      charset - the charset | 字符集
      Returns:
      reader | Reader
    • toWriter

      public static Writer toWriter(OutputStream output, Charset charset)
      Converts output stream to writer 将输出流转换为Writer
      Parameters:
      output - the output stream | 输出流
      charset - the charset | 字符集
      Returns:
      writer | Writer
    • toInputStream

      public static InputStream toInputStream(String data, Charset charset)
      Converts string to input stream 将字符串转换为输入流
      Parameters:
      data - the data | 数据
      charset - the charset | 字符集
      Returns:
      input stream | 输入流
    • toInputStream

      public static InputStream toInputStream(byte[] data)
      Converts byte array to input stream 将字节数组转换为输入流
      Parameters:
      data - the data | 数据
      Returns:
      input stream | 输入流
    • skip

      public static long skip(InputStream input, long skip)
      Skips bytes from input stream 从输入流跳过字节
      Parameters:
      input - the input stream | 输入流
      skip - bytes to skip | 要跳过的字节数
      Returns:
      actual bytes skipped | 实际跳过的字节数
    • drain

      public static long drain(InputStream input)
      Drains all remaining bytes from input stream 排空输入流中的所有剩余字节
      Parameters:
      input - the input stream | 输入流
      Returns:
      bytes drained | 排空的字节数
    • contentEquals

      public static boolean contentEquals(InputStream input1, InputStream input2)
      Compares two input streams for content equality 比较两个输入流的内容是否相同
      Parameters:
      input1 - the first input stream | 第一个输入流
      input2 - the second input stream | 第二个输入流
      Returns:
      true if equal | 如果相同返回true
    • contentEquals

      public static boolean contentEquals(Reader reader1, Reader reader2)
      Compares two readers for content equality 比较两个Reader的内容是否相同
      Parameters:
      reader1 - the first reader | 第一个Reader
      reader2 - the second reader | 第二个Reader
      Returns:
      true if equal | 如果相同返回true