Class IoUtil

java.lang.Object
com.luna.common.io.NioUtil
com.luna.common.io.IoUtil

public class IoUtil extends NioUtil
IO工具类
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
Author:
xiaoleilu
  • Constructor Details

    • IoUtil

      public IoUtil()
  • Method Details

    • copy

      public static long copy(Reader reader, Writer writer) throws IOException
      将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader
      Parameters:
      reader - Reader
      writer - Writer
      Returns:
      拷贝的字节数
      Throws:
      IOException - IO异常
    • copy

      public static long copy(Reader reader, Writer writer, int bufferSize) throws IOException
      将Reader中的内容复制到Writer中,拷贝后不关闭Reader
      Parameters:
      reader - Reader
      writer - Writer
      bufferSize - 缓存大小
      Returns:
      传输的byte数
      Throws:
      IOException - IO异常
    • copy

      public static long copy(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) throws IOException
      将Reader中的内容复制到Writer中,拷贝后不关闭Reader
      Parameters:
      reader - Reader
      writer - Writer
      bufferSize - 缓存大小
      streamProgress - 进度处理器
      Returns:
      传输的byte数
      Throws:
      IOException - IO异常
    • copy

      public static long copy(InputStream in, OutputStream out) throws IOException
      拷贝流,使用默认Buffer大小,拷贝后不关闭流
      Parameters:
      in - 输入流
      out - 输出流
      Returns:
      传输的byte数
      Throws:
      IOException - IO异常
    • copy

      public static long copy(InputStream in, OutputStream out, int bufferSize) throws IOException
      拷贝流,拷贝后不关闭流
      Parameters:
      in - 输入流
      out - 输出流
      bufferSize - 缓存大小
      Returns:
      传输的byte数
      Throws:
      IOException - IO异常
    • copy

      public static long copy(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws IOException
      拷贝流,拷贝后不关闭流
      Parameters:
      in - 输入流
      out - 输出流
      bufferSize - 缓存大小
      streamProgress - 进度条
      Returns:
      传输的byte数
      Throws:
      IOException - IO异常
    • copy

      public static long copy(FileInputStream in, FileOutputStream out) throws IOException
      拷贝文件流,使用NIO
      Parameters:
      in - 输入
      out - 输出
      Returns:
      拷贝的字节数
      Throws:
      IOException - IO异常
    • getUtf8Reader

      public static BufferedReader getUtf8Reader(InputStream in)
      获得一个文件读取器,默认使用UTF-8编码
      Parameters:
      in - 输入流
      Returns:
      BufferedReader对象
      Since:
      5.1.6
    • getReader

      public static BufferedReader getReader(InputStream in, String charsetName)
      获得一个文件读取器
      Parameters:
      in - 输入流
      charsetName - 字符集名称
      Returns:
      BufferedReader对象
    • getReader

      public static BufferedReader getReader(org.apache.commons.io.input.BOMInputStream in) throws IOException
      BOMInputStream中获取Reader
      Parameters:
      in - BOMInputStream
      Returns:
      BufferedReader
      Throws:
      IOException
      Since:
      5.5.8
    • getReader

      public static BufferedReader getReader(InputStream in, Charset charset)
      获得一个Reader
      Parameters:
      in - 输入流
      charset - 字符集
      Returns:
      BufferedReader对象
    • getReader

      public static BufferedReader getReader(Reader reader)
      获得BufferedReader
      如果是BufferedReader强转返回,否则新建。如果提供的Reader为null返回null
      Parameters:
      reader - 普通Reader,如果为null返回null
      Returns:
      BufferedReader or null
      Since:
      3.0.9
    • getPushBackReader

      public static PushbackReader getPushBackReader(Reader reader, int pushBackSize)
      获得PushbackReader
      如果是PushbackReader强转返回,否则新建
      Parameters:
      reader - 普通Reader
      pushBackSize - 推后的byte数
      Returns:
      PushbackReader
      Since:
      3.1.0
    • getUtf8Writer

      public static OutputStreamWriter getUtf8Writer(OutputStream out)
      获得一个Writer,默认编码UTF-8
      Parameters:
      out - 输入流
      Returns:
      OutputStreamWriter对象
      Since:
      5.1.6
    • getWriter

      public static OutputStreamWriter getWriter(OutputStream out, String charsetName)
      获得一个Writer
      Parameters:
      out - 输入流
      charsetName - 字符集
      Returns:
      OutputStreamWriter对象
    • getWriter

      public static OutputStreamWriter getWriter(OutputStream out, Charset charset)
      获得一个Writer
      Parameters:
      out - 输入流
      charset - 字符集
      Returns:
      OutputStreamWriter对象
    • readUtf8

      public static String readUtf8(InputStream in) throws IOException
      从流中读取UTF8编码的内容
      Parameters:
      in - 输入流
      Returns:
      内容
      Throws:
      IOException - IO异常
      Since:
      5.4.4
    • read

      public static String read(InputStream in, String charsetName) throws IOException
      从流中读取内容,读取完成后关闭流
      Parameters:
      in - 输入流
      charsetName - 字符集
      Returns:
      内容
      Throws:
      IOException - IO异常
    • read

      public static String read(InputStream in, Charset charset) throws IOException
      从流中读取内容,读取完毕后并不关闭流
      Parameters:
      in - 输入流,读取完毕后并不关闭流
      charset - 字符集
      Returns:
      内容
      Throws:
      IOException - IO异常
    • read

      public static FastByteArrayOutputStream read(InputStream in) throws IOException
      从流中读取内容,读到输出流中,读取完毕后并不关闭流
      Parameters:
      in - 输入流
      Returns:
      输出流
      Throws:
      IOException - IO异常
    • read

      public static FastByteArrayOutputStream read(InputStream in, boolean isClose) throws IOException
      从流中读取内容,读到输出流中,读取完毕后并不关闭流
      Parameters:
      in - 输入流
      isClose - 读取完毕后是否关闭流
      Returns:
      输出流
      Throws:
      IOException - IO异常
      Since:
      5.5.3
    • read

      public static String read(Reader reader) throws IOException
      从Reader中读取String,读取完毕后关闭Reader
      Parameters:
      reader - Reader
      Returns:
      String
      Throws:
      IOException - IO异常
    • read

      public static String read(Reader reader, boolean isClose) throws IOException
      Reader中读取String
      Parameters:
      reader - Reader
      isClose - 是否关闭Reader
      Returns:
      String
      Throws:
      IOException - IO异常
    • readBytes

      public static byte[] readBytes(InputStream in) throws IOException
      从流中读取bytes,读取完毕后关闭流
      Parameters:
      in - InputStream
      Returns:
      bytes
      Throws:
      IOException - IO异常
    • readBytes

      public static byte[] readBytes(InputStream in, boolean isClose) throws IOException
      从流中读取bytes
      Parameters:
      in - InputStream
      isClose - 是否关闭输入流
      Returns:
      bytes
      Throws:
      IOException - IO异常
      Since:
      5.0.4
    • readBytes

      public static byte[] readBytes(InputStream in, int length) throws IOException
      读取指定长度的byte数组,不关闭流
      Parameters:
      in - InputStream,为null返回null
      length - 长度,小于等于0返回空byte数组
      Returns:
      bytes
      Throws:
      IOException - IO异常
    • readHex

      public static String readHex(InputStream in, int length, boolean toLowerCase) throws IOException
      读取16进制字符串
      Parameters:
      in - InputStream
      length - 长度
      toLowerCase - true 传换成小写格式 , false 传换成大写格式
      Returns:
      16进制字符串
      Throws:
      IOException - IO异常
    • readHex28Upper

      public static String readHex28Upper(InputStream in) throws IOException
      从流中读取前28个byte并转换为16进制,字母部分使用大写
      Parameters:
      in - InputStream
      Returns:
      16进制字符串
      Throws:
      IOException - IO异常
    • readHex28Lower

      public static String readHex28Lower(InputStream in) throws IOException
      从流中读取前28个byte并转换为16进制,字母部分使用小写
      Parameters:
      in - InputStream
      Returns:
      16进制字符串
      Throws:
      IOException - IO异常
    • readObj

      public static <T> T readObj(InputStream in) throws IOException, UtilException
      从流中读取对象,即对象的反序列化

      注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!

      Type Parameters:
      T - 读取对象的类型
      Parameters:
      in - 输入流
      Returns:
      输出流
      Throws:
      IOException - IO异常
      UtilException - ClassNotFoundException包装
    • readObj

      public static <T> T readObj(InputStream in, Class<T> clazz) throws IOException, UtilException
      从流中读取对象,即对象的反序列化,读取后不关闭流

      注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!

      Type Parameters:
      T - 读取对象的类型
      Parameters:
      in - 输入流
      clazz - 读取对象类型
      Returns:
      输出流
      Throws:
      IOException - IO异常
      UtilException - ClassNotFoundException包装
    • readObj

      public static <T> T readObj(ValidateObjectInputStream in, Class<T> clazz) throws IOException, UtilException
      从流中读取对象,即对象的反序列化,读取后不关闭流

      此方法使用了ValidateObjectInputStream中的黑白名单方式过滤类,用于避免反序列化漏洞
      通过构造ValidateObjectInputStream,调用ValidateObjectInputStream.accept(Class[]) 或者ValidateObjectInputStream.refuse(Class[])方法添加可以被序列化的类或者禁止序列化的类。

      Type Parameters:
      T - 读取对象的类型
      Parameters:
      in - 输入流,使用ValidateObjectInputStream中的黑白名单方式过滤类,用于避免反序列化漏洞
      clazz - 读取对象类型
      Returns:
      输出流
      Throws:
      IOException - IO异常
      UtilException - ClassNotFoundException包装
    • readUtf8Lines

      public static <T extends Collection<String>> T readUtf8Lines(InputStream in, T collection) throws IOException
      从流中读取内容,使用UTF-8编码
      Type Parameters:
      T - 集合类型
      Parameters:
      in - 输入流
      collection - 返回集合
      Returns:
      内容
      Throws:
      IOException - IO异常
    • readLines

      public static <T extends Collection<String>> T readLines(InputStream in, String charsetName, T collection) throws IOException
      从流中读取内容
      Type Parameters:
      T - 集合类型
      Parameters:
      in - 输入流
      charsetName - 字符集
      collection - 返回集合
      Returns:
      内容
      Throws:
      IOException - IO异常
    • readLines

      public static <T extends Collection<String>> T readLines(InputStream in, Charset charset, T collection) throws IOException
      从流中读取内容
      Type Parameters:
      T - 集合类型
      Parameters:
      in - 输入流
      charset - 字符集
      collection - 返回集合
      Returns:
      内容
      Throws:
      IOException - IO异常
    • readLines

      public static <T extends Collection<String>> T readLines(Reader reader, T collection) throws IOException
      从Reader中读取内容
      Type Parameters:
      T - 集合类型
      Parameters:
      reader - Reader
      collection - 返回集合
      Returns:
      内容
      Throws:
      IOException - IO异常
    • readUtf8Lines

      public static void readUtf8Lines(InputStream in, LineHandler lineHandler) throws IOException
      按行读取UTF-8编码数据,针对每行的数据做处理
      Parameters:
      in - InputStream
      lineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方
      Throws:
      IOException - IO异常
      Since:
      3.1.1
    • readLines

      public static void readLines(InputStream in, Charset charset, LineHandler lineHandler) throws IOException
      按行读取数据,针对每行的数据做处理
      Parameters:
      in - InputStream
      charset - Charset编码
      lineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方
      Throws:
      IOException - IO异常
      Since:
      3.0.9
    • readLines

      public static void readLines(Reader reader, LineHandler lineHandler) throws IOException
      按行读取数据,针对每行的数据做处理
      Reader自带编码定义,因此读取数据的编码跟随其编码。
      Parameters:
      reader - Reader
      lineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方
      Throws:
      IOException - IO异常
    • toStream

      public static ByteArrayInputStream toStream(String content, String charsetName)
      String 转为流
      Parameters:
      content - 内容
      charsetName - 编码
      Returns:
      字节流
    • toStream

      public static ByteArrayInputStream toStream(String content, Charset charset)
      String 转为流
      Parameters:
      content - 内容
      charset - 编码
      Returns:
      字节流
    • toUtf8Stream

      public static ByteArrayInputStream toUtf8Stream(String content)
      String 转为UTF-8编码的字节流流
      Parameters:
      content - 内容
      Returns:
      字节流
      Since:
      4.5.1
    • toStream

      public static FileInputStream toStream(File file)
      文件转为FileInputStream
      Parameters:
      file - 文件
      Returns:
      FileInputStream
    • toStream

      public static ByteArrayInputStream toStream(byte[] content)
      Parameters:
      content - 内容bytes
      Returns:
      字节流
      Since:
      4.1.8
    • toStream

      public static ByteArrayInputStream toStream(ByteArrayOutputStream out)
      Parameters:
      out - ByteArrayOutputStream
      Returns:
      字节流
      Since:
      5.3.6
    • toBuffered

      public static BufferedInputStream toBuffered(InputStream in)
      Parameters:
      in - InputStream
      Returns:
      BufferedInputStream
      Since:
      4.0.10
    • toBuffered

      public static BufferedInputStream toBuffered(InputStream in, int bufferSize)
      Parameters:
      in - InputStream
      bufferSize - buffer size
      Returns:
      BufferedInputStream
      Since:
      5.6.1
    • toBuffered

      public static BufferedOutputStream toBuffered(OutputStream out)
      Parameters:
      out - OutputStream
      Returns:
      BufferedOutputStream
      Since:
      4.0.10
    • toBuffered

      public static BufferedOutputStream toBuffered(OutputStream out, int bufferSize)
      Parameters:
      out - OutputStream
      bufferSize - buffer size
      Returns:
      BufferedOutputStream
      Since:
      5.6.1
    • toBuffered

      public static BufferedReader toBuffered(Reader reader)
      Parameters:
      reader - Reader
      Returns:
      BufferedReader
      Since:
      5.6.1
    • toBuffered

      public static BufferedReader toBuffered(Reader reader, int bufferSize)
      Parameters:
      reader - Reader
      bufferSize - buffer size
      Returns:
      BufferedReader
      Since:
      5.6.1
    • toBuffered

      public static BufferedWriter toBuffered(Writer writer)
      Parameters:
      writer - Writer
      Returns:
      BufferedWriter
      Since:
      5.6.1
    • toBuffered

      public static BufferedWriter toBuffered(Writer writer, int bufferSize)
      Parameters:
      writer - Writer
      bufferSize - buffer size
      Returns:
      BufferedWriter
      Since:
      5.6.1
    • toMarkSupportStream

      public static InputStream toMarkSupportStream(InputStream in)
      InputStream转换为支持mark标记的流
      若原流支持mark标记,则返回原流,否则使用BufferedInputStream 包装之
      Parameters:
      in - 流
      Returns:
      InputStream
      Since:
      4.0.9
    • toPushbackStream

      public static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize)
      转换为PushbackInputStream
      如果传入的输入流已经是PushbackInputStream,强转返回,否则新建一个
      Parameters:
      in - InputStream
      pushBackSize - 推后的byte数
      Returns:
      PushbackInputStream
      Since:
      3.1.0
    • toAvailableStream

      public static InputStream toAvailableStream(InputStream in)
      将指定InputStream 转换为InputStream.available()方法可用的流。
      在Socket通信流中,服务端未返回数据情况下InputStream.available()方法始终为0
      因此,在读取前需要调用InputStream.read()读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()方法就正常了。
      需要注意的是,在网络流中,是按照块来传输的,所以 InputStream.available() 读取到的并非最终长度,而是此次块的长度。
      此方法返回对象的规则为:
      • FileInputStream 返回原对象,因为文件流的available方法本身可用
      • 其它InputStream 返回PushbackInputStream
      Parameters:
      in - 被转换的流
      Returns:
      转换后的流,可能为PushbackInputStream
      Since:
      5.5.3
    • write

      public static void write(OutputStream out, boolean isCloseOut, byte[] content) throws IOException
      将byte[]写到流中
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      content - 写入的内容
      Throws:
      IOException - IO异常
    • writeUtf8

      public static void writeUtf8(OutputStream out, boolean isCloseOut, Object... contents) throws IOException
      将多部分内容写到流中,自动转换为UTF-8字符串
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容,调用toString()方法,不包括不会自动换行
      Throws:
      IOException - IO异常
      Since:
      3.1.1
    • write

      public static void write(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) throws IOException
      将多部分内容写到流中,自动转换为字符串
      Parameters:
      out - 输出流
      charsetName - 写出的内容的字符集
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容,调用toString()方法,不包括不会自动换行
      Throws:
      IOException - IO异常
    • write

      public static void write(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) throws IOException
      将多部分内容写到流中,自动转换为字符串
      Parameters:
      out - 输出流
      charset - 写出的内容的字符集
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容,调用toString()方法,不包括不会自动换行
      Throws:
      IOException - IO异常
      Since:
      3.0.9
    • writeObj

      public static void writeObj(OutputStream out, boolean isCloseOut, Serializable obj) throws IOException
      将多部分内容写到流中
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      obj - 写入的对象内容
      Throws:
      IOException - IO异常
      Since:
      5.3.3
    • writeObjects

      public static void writeObjects(OutputStream out, boolean isCloseOut, Serializable... contents) throws IOException
      将多部分内容写到流中
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容
      Throws:
      IOException - IO异常
    • flush

      public static void flush(Flushable flushable)
      从缓存中刷出数据
      Parameters:
      flushable - Flushable
      Since:
      4.2.2
    • close

      public static void close(Closeable closeable)
      关闭
      关闭失败不会抛出异常
      Parameters:
      closeable - 被关闭的对象
    • closeIfPosible

      public static void closeIfPosible(Object obj)
      尝试关闭指定对象
      判断对象如果实现了AutoCloseable,则调用之
      Parameters:
      obj - 可关闭对象
      Since:
      4.3.2
    • contentEquals

      public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException
      对比两个流内容是否相同
      内部会转换流为 BufferedInputStream
      Parameters:
      input1 - 第一个流
      input2 - 第二个流
      Returns:
      两个流的内容一致返回true,否则false
      Throws:
      IOException - IO异常
      Since:
      4.0.6
    • contentEquals

      public static boolean contentEquals(Reader input1, Reader input2) throws IOException
      对比两个Reader的内容是否一致
      内部会转换流为 BufferedInputStream
      Parameters:
      input1 - 第一个reader
      input2 - 第二个reader
      Returns:
      两个流的内容一致返回true,否则false
      Throws:
      IOException - IO异常
      Since:
      4.0.6
    • contentEqualsIgnoreEOL

      public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws IOException
      对比两个流内容是否相同,忽略EOL字符
      内部会转换流为 BufferedInputStream
      Parameters:
      input1 - 第一个流
      input2 - 第二个流
      Returns:
      两个流的内容一致返回true,否则false
      Throws:
      IOException - IO异常
      Since:
      4.0.6
    • checksumCRC32

      public static long checksumCRC32(InputStream in) throws IOException
      计算流CRC32校验码,计算后关闭流
      Parameters:
      in - 文件,不能为目录
      Returns:
      CRC32值
      Throws:
      IOException - IO异常
      Since:
      4.0.6
    • checksum

      public static Checksum checksum(InputStream in, Checksum checksum) throws IOException
      计算流的校验码,计算后关闭流
      Parameters:
      in - 流
      checksum - Checksum
      Returns:
      Checksum
      Throws:
      IOException - IO异常
      Since:
      4.0.10
    • checksumValue

      public static long checksumValue(InputStream in, Checksum checksum) throws IOException
      计算流的校验码,计算后关闭流
      Parameters:
      in - 流
      checksum - Checksum
      Returns:
      Checksum
      Throws:
      IOException - IO异常
      Since:
      5.4.0
    • lineIter

      public static LineIter lineIter(Reader reader)
      返回行遍历器
       LineIterator it = null;
       try {
           it = IoUtil.lineIter(reader);
           while (it.hasNext()) {
               String line = it.nextLine();
               // do something with line
           }
       } finally {
           it.close();
       }
       
      Parameters:
      reader - Reader
      Returns:
      LineIter
      Since:
      5.6.1
    • lineIter

      public static LineIter lineIter(InputStream in, Charset charset)
      返回行遍历器
       LineIterator it = null;
       try {
           it = IoUtil.lineIter(in, CharsetUtil.CHARSET_UTF_8);
           while (it.hasNext()) {
               String line = it.nextLine();
               // do something with line
           }
       } finally {
           it.close();
       }
       
      Parameters:
      in - InputStream
      charset - 编码
      Returns:
      LineIter
      Since:
      5.6.1