Class StreamKit

java.lang.Object
org.aoju.bus.core.toolkit.StreamKit

public class StreamKit extends Object
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • StreamKit

      public StreamKit()
  • Method Details

    • equals

      public static boolean equals(InputStream ina, InputStream inb) throws IOException
      判断两个输入流是否严格相等
      Parameters:
      ina - 输入流
      inb - 输入流
      Returns:
      the boolean
      Throws:
      IOException - 异常
    • read

      public static StringBuilder read(Reader reader) throws IOException
      从一个文本流中读取全部内容并返回

      注意,它并不会关闭输出流

      Parameters:
      reader - 文本输出流
      Returns:
      文本内容
      Throws:
      IOException - 异常
    • read

      public static byte[] read(InputStream ins) throws IOException
      读取一个输入流中所有的字节
      Parameters:
      ins - 输入流,必须支持 available()
      Returns:
      一个字节数组
      Throws:
      IOException - 异常
    • read

      public static int read(Reader reader, StringBuilder sb) throws IOException
      从一个文本流中读取全部内容并写入缓冲
      Parameters:
      reader - 文本输出流
      sb - 输出的文本缓冲
      Returns:
      读取的字符数量
      Throws:
      IOException - 异常
    • readAll

      public static String readAll(Reader reader)
      从一个文本输入流读取所有内容,并将该流关闭
      Parameters:
      reader - 文本输入流
      Returns:
      输入流所有内容
    • readText

      public static String readText(InputStream in) throws IOException
      Throws:
      IOException
    • readText

      public static String readText(InputStream in, String encoding) throws IOException
      Throws:
      IOException
    • readText

      public static String readText(InputStream in, String encoding, int bufferSize) throws IOException
      Throws:
      IOException
    • readText

      public static String readText(Reader reader) throws IOException
      Throws:
      IOException
    • readText

      public static String readText(Reader reader, int bufferSize) throws IOException
      Throws:
      IOException
    • write

      public static void write(Writer writer, CharSequence cs) throws IOException
      将一段文本全部写入一个writer

      注意,它并不会关闭输出流

      Parameters:
      writer - 操作器
      cs - 文本
      Throws:
      IOException - 异常
    • write

      public static long write(OutputStream ops, InputStream ins) throws IOException
      将输入流写入一个输出流 块大小为 8192

      注意,它并不会关闭输入/出流

      Parameters:
      ops - 输出流
      ins - 输入流
      Returns:
      写入的字节数
      Throws:
      IOException - 异常
    • write

      public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException
      将输入流写入一个输出流

      注意,它并不会关闭输入/出流

      Parameters:
      ops - 输出流
      ins - 输入流
      bufferSize - 缓冲块大小
      Returns:
      写入的字节数
      Throws:
      IOException - 异常
    • write

      public static long write(Writer writer, Reader reader) throws IOException
      将文本输入流写入一个文本输出流 块大小为 8192

      注意,它并不会关闭输入/出流

      Parameters:
      writer - 输出流
      reader - 输入流
      Returns:
      the long
      Throws:
      IOException - 异常
    • write

      public static void write(OutputStream ops, byte[] bytes) throws IOException
      将一个字节数组写入一个输出流

      注意,它并不会关闭输出流

      Parameters:
      ops - 输出流
      bytes - 字节数组
      Throws:
      IOException - 异常
    • readAndClose

      public static String readAndClose(Reader reader)
      从一个文本流中读取全部内容并返回
      Parameters:
      reader - 文本输入流
      Returns:
      文本内容
    • readAndClose

      public static byte[] readAndClose(InputStream ins)
      读取一个输入流中所有的字节,并关闭输入流
      Parameters:
      ins - 输入流,必须支持 available()
      Returns:
      一个字节数组
    • readAndClose

      public static int readAndClose(InputStreamReader reader, StringBuilder sb)
      从一个文本流中读取全部内容并写入缓冲

      注意,它会关闭输出流

      Parameters:
      reader - 文本输出流
      sb - 输出的文本缓冲
      Returns:
      读取的字符数量
    • writeAndClose

      public static void writeAndClose(Writer writer, CharSequence cs)
      将一段文本全部写入一个writer

      注意,它会关闭输出流

      Parameters:
      writer - 输出流
      cs - 文本
    • writeAndClose

      public static long writeAndClose(OutputStream ops, InputStream ins)
      将输入流写入一个输出流 块大小为 8192

      注意,它会关闭输入/出流

      Parameters:
      ops - 输出流
      ins - 输入流
      Returns:
      写入的字节数
    • writeAndClose

      public static long writeAndClose(Writer writer, Reader reader)
      将文本输入流写入一个文本输出流 块大小为 8192

      注意,它会关闭输入/出流

      Parameters:
      writer - 输出流
      reader - 输入流
      Returns:
      the long
    • writeAndClose

      public static void writeAndClose(OutputStream ops, byte[] bytes)
      将一个字节数组写入一个输出流

      注意,它会关闭输出流

      Parameters:
      ops - 输出流
      bytes - 字节数组
    • writeAndClose

      public static long writeAndClose(OutputStream ops, InputStream ins, int buf)
    • buff

      public static BufferedReader buff(Reader reader)
      为一个文本输入流包裹一个缓冲流 如果这个输入流本身就是缓冲流,则直接返回
      Parameters:
      reader - 文本输入流
      Returns:
      缓冲文本输入流
    • buff

      public static BufferedWriter buff(Writer writer)
      为一个文本输出流包裹一个缓冲流 如果这个文本输出流本身就是缓冲流,则直接返回
      Parameters:
      writer - 文本输出流
      Returns:
      缓冲文本输出流
    • buff

      public static BufferedInputStream buff(InputStream ins)
      为一个输入流包裹一个缓冲流 如果这个输入流本身就是缓冲流,则直接返回
      Parameters:
      ins - 输入流
      Returns:
      缓冲输入流
    • buff

      public static BufferedOutputStream buff(OutputStream ops)
      为一个输出流包裹一个缓冲流 如果这个输出流本身就是缓冲流,则直接返回
      Parameters:
      ops - 输出流
      Returns:
      缓冲输出流
    • readFully

      public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException
      Throws:
      IOException
    • skipFully

      public static void skipFully(InputStream in, long n) throws IOException
      Throws:
      IOException
    • safeClose

      public static boolean safeClose(Closeable cb)
      关闭一个可关闭对象,可以接受 null 如果成功关闭,返回 true,发生异常 返回 false
      Parameters:
      cb - 可关闭对象
      Returns:
      是否成功关闭
    • safeFlush

      public static void safeFlush(Flushable fa)
      安全刷新一个可刷新的对象,可接受 null
      Parameters:
      fa - 可刷新对象
    • utf8r

      public static Reader utf8r(InputStream is)
    • utf8w

      public static Writer utf8w(OutputStream os)
    • utf8filte

      public static InputStream utf8filte(InputStream in)
      判断并移除UTF-8的BOM头
      Parameters:
      in - 输入流
      Returns:
      输入流
    • fileOut

      public static OutputStream fileOut(File file)
      根据一个文件建立一个输出流
      Parameters:
      file - 文件
      Returns:
      输出流
    • wrap

      public static InputStream wrap(byte[] bytes)
    • appendWriteAndClose

      public static void appendWriteAndClose(File f, String text)
    • findFileAsStream

      public static InputStream findFileAsStream(String path, Class<?> klass, String enc)
      获取输出流
      Parameters:
      path - 文件路径
      klass - 参考的类, -- 会用这个类的 ClassLoader
      enc - 文件路径编码
      Returns:
      输出流
    • readAvailable

      public static int readAvailable(InputStream in, byte[] b, int off, int len) throws IOException
      Throws:
      IOException
    • nextLineTrim

      public static String nextLineTrim(BufferedReader br) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream out, byte[] buf) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream out) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream out, int len, byte[] buf) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream out, int len) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream out, int len, int swapBytes, byte[] buf) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream in, OutputStream out, int len, int swapBytes) throws IOException
      Throws:
      IOException
    • openFileOrURL

      public static InputStream openFileOrURL(String name) throws IOException
      Throws:
      IOException
    • io

      public static void io(Reader in, Writer out, int bufferSize) throws IOException
      Throws:
      IOException
    • of

      public static <T> Stream<T> of(T... array)
    • of

      public static <T> Stream<T> of(Iterable<T> iterable)
      Iterable转换为Stream,默认非并行
      Type Parameters:
      T - 集合元素类型
      Parameters:
      iterable - 集合
      Returns:
      Stream
    • of

      public static <T> Stream<T> of(Iterable<T> iterable, boolean parallel)
      Type Parameters:
      T - 集合元素类型
      Parameters:
      iterable - 集合
      parallel - 是否并行
      Returns:
      Stream
    • of

      public static Stream<String> of(File file)
      按行读取文件为Stream
      Parameters:
      file - 文件
      Returns:
      Stream
    • of

      public static Stream<String> of(Path path)
      按行读取文件为Stream
      Parameters:
      path - 路径
      Returns:
      Stream
    • of

      public static Stream<String> of(File file, Charset charset)
      按行读取文件为Stream
      Parameters:
      file - 文件
      charset - 编码
      Returns:
      Stream
    • of

      public static Stream<String> of(Path path, Charset charset)
      按行读取文件为Stream
      Parameters:
      path - 路径
      charset - 编码
      Returns:
      Stream