Class IoUtil

java.lang.Object
com.sure.tool.io.IoUtil

public class IoUtil extends Object
IO 流工具类,参考 Hutool 的 IoUtil 设计。

English: Stream and reader/writer helpers for copying, reading and closing resources safely.

Since:
0.1.0
Author:
suretool
  • Field Details

    • DEFAULT_BUFFER_SIZE

      public static final int DEFAULT_BUFFER_SIZE
      默认缓冲区大小
      See Also:
  • Method Details

    • copy

      public static long copy(InputStream in, OutputStream out) throws IOException
      复制输入流到输出流。
      Parameters:
      in - 输入流
      out - 输出流
      Returns:
      复制的字节数
      Throws:
      IOException - IO 异常
    • copy

      public static long copy(InputStream in, OutputStream out, int bufferSize) throws IOException
      复制输入流到输出流。
      Parameters:
      in - 输入流
      out - 输出流
      bufferSize - 缓冲区大小
      Returns:
      复制的字节数
      Throws:
      IOException - IO 异常
    • copy

      public static long copy(Reader reader, Writer writer) throws IOException
      复制字符流。
      Parameters:
      reader - 字符输入流
      writer - 字符输出流
      Returns:
      复制的字符数
      Throws:
      IOException - IO 异常
    • readBytes

      public static byte[] readBytes(InputStream in) throws IOException
      读取输入流全部字节。
      Parameters:
      in - 输入流
      Returns:
      字节数组
      Throws:
      IOException - IO 异常
    • readUtf8

      public static String readUtf8(InputStream in) throws IOException
      按 UTF-8 读取输入流为字符串。
      Parameters:
      in - 输入流
      Returns:
      字符串
      Throws:
      IOException - IO 异常
    • read

      public static String read(InputStream in, Charset charset) throws IOException
      按指定字符集读取输入流为字符串。
      Parameters:
      in - 输入流
      charset - 字符集
      Returns:
      字符串
      Throws:
      IOException - IO 异常
    • readUtf8Lines

      public static List<String> readUtf8Lines(InputStream in) throws IOException
      按 UTF-8 读取输入流为行列表。
      Parameters:
      in - 输入流
      Returns:
      行列表
      Throws:
      IOException - IO 异常
    • readLines

      public static List<String> readLines(InputStream in, Charset charset) throws IOException
      按指定字符集读取输入流为行列表。
      Parameters:
      in - 输入流
      charset - 字符集
      Returns:
      行列表
      Throws:
      IOException - IO 异常
    • write

      public static void write(byte[] data, OutputStream out) throws IOException
      写入字节数组。
      Parameters:
      data - 字节数组
      out - 输出流
      Throws:
      IOException - IO 异常
    • writeUtf8

      public static void writeUtf8(String content, OutputStream out) throws IOException
      按 UTF-8 写入字符串。
      Parameters:
      content - 字符串
      out - 输出流
      Throws:
      IOException - IO 异常
    • write

      public static void write(String content, OutputStream out, Charset charset) throws IOException
      按指定字符集写入字符串。
      Parameters:
      content - 字符串
      out - 输出流
      charset - 字符集
      Throws:
      IOException - IO 异常
    • write

      public static void write(String content, Writer writer) throws IOException
      按指定字符集写入字符串(通过 Writer)。
      Parameters:
      content - 字符串
      writer - 字符输出流
      Throws:
      IOException - IO 异常
    • copy

      public static long copy(InputStream in, File file) throws IOException
      将输入流内容写入文件(自动创建父目录)。
      Parameters:
      in - 输入流
      file - 目标文件
      Returns:
      写入字节数
      Throws:
      IOException - IO 失败
    • copy

      public static long copy(File file, OutputStream out) throws IOException
      将文件内容写入输出流。
      Parameters:
      file - 源文件
      out - 输出流
      Returns:
      写入字节数
      Throws:
      IOException - IO 失败
    • writeLines

      public static void writeLines(Collection<?> lines, OutputStream out, Charset charset) throws IOException
      逐行写出集合内容(每行末尾追加行分隔符)。
      Parameters:
      lines - 行集合(元素经 String.valueOf(Object) 转换,null 元素写出空行)
      out - 输出流
      charset - 字符集
      Throws:
      IOException - IO 失败
    • readLines

      public static int readLines(InputStream in, Charset charset, Collection<String> lines) throws IOException
      追加式读取流内容并按行写入目标集合(适合大文件分块处理,避免一次性 OOM)。
      Parameters:
      in - 输入流
      charset - 字符集
      lines - 目标集合(内容追加到末尾)
      Returns:
      读取行数
      Throws:
      IOException - IO 失败
    • closeQuietly

      public static void closeQuietly(Closeable... closeables)
      安静关闭可关闭资源,忽略关闭异常。
      Parameters:
      closeables - 可关闭资源
    • contentEquals

      public static boolean contentEquals(InputStream in1, InputStream in2) throws IOException
      比较两个输入流内容是否一致。
      Parameters:
      in1 - 输入流 1
      in2 - 输入流 2
      Returns:
      内容是否一致
      Throws:
      IOException - IO 异常
    • toOutput

      public static long toOutput(InputStream in, OutputStream out) throws IOException
      输入流转输出流(输出流关闭)。
      Parameters:
      in - 输入流
      out - 输出流
      Returns:
      复制的字节数
      Throws:
      IOException - IO 异常
    • toBufferedInputStream

      public static BufferedInputStream toBufferedInputStream(InputStream in)
      将输入流包装为缓冲输入流(null 安全)。

      示例:IoUtil.toBufferedInputStream(in) 等价于 new BufferedInputStream(in)。

      Parameters:
      in - 输入流,可为 null
      Returns:
      缓冲输入流;in 为 null 返回 null
    • toBufferedReader

      public static BufferedReader toBufferedReader(Reader reader)
      将字符流包装为缓冲字符流(null 安全)。

      示例:IoUtil.toBufferedReader(reader) 等价于 new BufferedReader(reader)。

      Parameters:
      reader - 字符流,可为 null
      Returns:
      缓冲字符流;reader 为 null 返回 null
    • readFully

      public static void readFully(InputStream in, byte[] buf, int off, int len) throws IOException
      精确读取指定长度的字节:必须读满 len 字节,否则抛 IOException。

      示例:IoUtil.readFully(in, buf, 0, 4) 保证 buf 前 4 字节有效。

      Parameters:
      in - 输入流
      buf - 目标数组
      off - 起始偏移
      len - 读取长度
      Throws:
      IOException - 读不满 len 字节或 IO 异常
    • skipFully

      public static void skipFully(InputStream in, long skip) throws IOException
      精确跳过指定字节数:不足则抛 IOException。

      示例:IoUtil.skipFully(in, 4) 保证恰好跳过 4 字节。

      Parameters:
      in - 输入流
      skip - 待跳过字节数,非负
      Throws:
      IOException - 跳不足 skip 字节或 IO 异常
    • toString

      public static String toString(Reader reader) throws IOException
      字符流全量读取为字符串(使用平台默认字符集解码)。

      示例:IoUtil.toString(reader) 读取全部剩余字符。

      Parameters:
      reader - 字符流
      Returns:
      全部内容字符串
      Throws:
      IOException - IO 异常
    • lineIterator

      public static IoUtil.LineIterator lineIterator(Reader reader) throws IOException
      按行读取的懒加载迭代器(字符流版),使用后应关闭返回的 IoUtil.LineIterator。

      示例:try (var it = IoUtil.lineIterator(reader)) { ... } 逐行消费。

      Parameters:
      reader - 字符流
      Returns:
      行迭代器
      Throws:
      IOException - 包装行读取异常
    • lineIterator

      public static IoUtil.LineIterator lineIterator(InputStream in, Charset charset) throws IOException
      按行读取的懒加载迭代器(字节流版),使用后应关闭返回的 IoUtil.LineIterator。

      示例:try (var it = IoUtil.lineIterator(in, StandardCharsets.UTF_8)) { ... } 逐行消费。

      Parameters:
      in - 输入流
      charset - 字符集
      Returns:
      行迭代器
      Throws:
      IOException - 包装行读取异常
    • contentEqualsIgnoreEOL

      public static boolean contentEqualsIgnoreEOL(Reader reader1, Reader reader2) throws IOException
      忽略行结束符(\r、\n、\r\n)比较两个字符流内容是否一致。

      示例:IoUtil.contentEqualsIgnoreEOL(r1, r2) 对 "a\r\nb" 与 "a\nb" 返回 true。

      Parameters:
      reader1 - 字符流 1
      reader2 - 字符流 2
      Returns:
      忽略行结束符后内容是否一致
      Throws:
      IOException - IO 异常
    • copy

      public static long copy(Reader reader, OutputStream out, Charset charset) throws IOException
      字符流复制到输出流(按指定字符集编码写出),不关闭流。

      示例:IoUtil.copy(reader, out, StandardCharsets.UTF_8)。

      Parameters:
      reader - 字符流
      out - 输出流
      charset - 写出字符集
      Returns:
      复制的字符数
      Throws:
      IOException - IO 异常
    • copy

      public static long copy(InputStream in, Writer writer, Charset charset) throws IOException
      输入流复制到字符流(按指定字符集解码),不关闭流。

      示例:IoUtil.copy(in, writer, StandardCharsets.UTF_8)。

      Parameters:
      in - 输入流
      writer - 字符流
      charset - 解码字符集
      Returns:
      写出的字符数
      Throws:
      IOException - IO 异常