Package com.luna.common.io
Class IoUtil
java.lang.Object
com.luna.common.io.NioUtil
com.luna.common.io.IoUtil
IO工具类
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
- Author:
- xiaoleilu
-
Field Summary
Fields inherited from class com.luna.common.io.NioUtil
DEFAULT_BUFFER_SIZE, DEFAULT_LARGE_BUFFER_SIZE, DEFAULT_MIDDLE_BUFFER_SIZE, EOF
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Checksum
checksum
(InputStream in, Checksum checksum) 计算流的校验码,计算后关闭流static long
计算流CRC32校验码,计算后关闭流static long
checksumValue
(InputStream in, Checksum checksum) 计算流的校验码,计算后关闭流static void
关闭
关闭失败不会抛出异常static void
closeIfPosible
(Object obj) 尝试关闭指定对象
判断对象如果实现了AutoCloseable
,则调用之static boolean
contentEquals
(InputStream input1, InputStream input2) 对比两个流内容是否相同
内部会转换流为BufferedInputStream
static boolean
contentEquals
(Reader input1, Reader input2) 对比两个Reader的内容是否一致
内部会转换流为BufferedInputStream
static boolean
contentEqualsIgnoreEOL
(Reader input1, Reader input2) 对比两个流内容是否相同,忽略EOL字符
内部会转换流为BufferedInputStream
static long
copy
(FileInputStream in, FileOutputStream out) 拷贝文件流,使用NIOstatic long
copy
(InputStream in, OutputStream out) 拷贝流,使用默认Buffer大小,拷贝后不关闭流static long
copy
(InputStream in, OutputStream out, int bufferSize) 拷贝流,拷贝后不关闭流static long
copy
(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) 拷贝流,拷贝后不关闭流static long
将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Readerstatic long
将Reader中的内容复制到Writer中,拷贝后不关闭Readerstatic long
copy
(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) 将Reader中的内容复制到Writer中,拷贝后不关闭Readerstatic void
从缓存中刷出数据static PushbackReader
getPushBackReader
(Reader reader, int pushBackSize) static BufferedReader
getReader
(InputStream in, String charsetName) 获得一个文件读取器static BufferedReader
getReader
(InputStream in, Charset charset) 获得一个Readerstatic BufferedReader
static BufferedReader
getReader
(org.apache.commons.io.input.BOMInputStream in) 从BOMInputStream
中获取Readerstatic BufferedReader
获得一个文件读取器,默认使用UTF-8编码static OutputStreamWriter
获得一个Writer,默认编码UTF-8static OutputStreamWriter
getWriter
(OutputStream out, String charsetName) 获得一个Writerstatic OutputStreamWriter
getWriter
(OutputStream out, Charset charset) 获得一个Writerstatic LineIter
lineIter
(InputStream in, Charset charset) 返回行遍历器static LineIter
返回行遍历器static FastByteArrayOutputStream
read
(InputStream in) 从流中读取内容,读到输出流中,读取完毕后并不关闭流static FastByteArrayOutputStream
read
(InputStream in, boolean isClose) 从流中读取内容,读到输出流中,读取完毕后并不关闭流static String
read
(InputStream in, String charsetName) 从流中读取内容,读取完成后关闭流static String
read
(InputStream in, Charset charset) 从流中读取内容,读取完毕后并不关闭流static String
从Reader中读取String,读取完毕后关闭Readerstatic String
从Reader
中读取Stringstatic byte[]
readBytes
(InputStream in) 从流中读取bytes,读取完毕后关闭流static byte[]
readBytes
(InputStream in, boolean isClose) 从流中读取bytesstatic byte[]
readBytes
(InputStream in, int length) 读取指定长度的byte数组,不关闭流static String
readHex
(InputStream in, int length, boolean toLowerCase) 读取16进制字符串static String
从流中读取前28个byte并转换为16进制,字母部分使用小写static String
从流中读取前28个byte并转换为16进制,字母部分使用大写static <T extends Collection<String>>
TreadLines
(InputStream in, String charsetName, T collection) 从流中读取内容static void
readLines
(InputStream in, Charset charset, LineHandler lineHandler) 按行读取数据,针对每行的数据做处理static <T extends Collection<String>>
TreadLines
(InputStream in, Charset charset, T collection) 从流中读取内容static void
readLines
(Reader reader, LineHandler lineHandler) 按行读取数据,针对每行的数据做处理
Reader
自带编码定义,因此读取数据的编码跟随其编码。static <T extends Collection<String>>
T从Reader中读取内容static <T> T
readObj
(ValidateObjectInputStream in, Class<T> clazz) 从流中读取对象,即对象的反序列化,读取后不关闭流static <T> T
readObj
(InputStream in) 从流中读取对象,即对象的反序列化static <T> T
readObj
(InputStream in, Class<T> clazz) 从流中读取对象,即对象的反序列化,读取后不关闭流static String
readUtf8
(InputStream in) 从流中读取UTF8编码的内容static void
readUtf8Lines
(InputStream in, LineHandler lineHandler) 按行读取UTF-8编码数据,针对每行的数据做处理static <T extends Collection<String>>
TreadUtf8Lines
(InputStream in, T collection) 从流中读取内容,使用UTF-8编码static InputStream
将指定InputStream
转换为InputStream.available()
方法可用的流。
在Socket通信流中,服务端未返回数据情况下InputStream.available()
方法始终为0
因此,在读取前需要调用InputStream.read()
读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()
方法就正常了。
需要注意的是,在网络流中,是按照块来传输的,所以InputStream.available()
读取到的并非最终长度,而是此次块的长度。
此方法返回对象的规则为: FileInputStream 返回原对象,因为文件流的available方法本身可用 其它InputStream 返回PushbackInputStreamstatic BufferedInputStream
static BufferedInputStream
toBuffered
(InputStream in, int bufferSize) static BufferedOutputStream
toBuffered
(OutputStream out) static BufferedOutputStream
toBuffered
(OutputStream out, int bufferSize) static BufferedReader
toBuffered
(Reader reader) static BufferedReader
toBuffered
(Reader reader, int bufferSize) static BufferedWriter
toBuffered
(Writer writer) static BufferedWriter
toBuffered
(Writer writer, int bufferSize) static InputStream
static PushbackInputStream
toPushbackStream
(InputStream in, int pushBackSize) static ByteArrayInputStream
toStream
(byte[] content) byte[] 转为ByteArrayInputStream
static ByteArrayInputStream
static FileInputStream
文件转为FileInputStream
static ByteArrayInputStream
String 转为流static ByteArrayInputStream
String 转为流static ByteArrayInputStream
toUtf8Stream
(String content) String 转为UTF-8编码的字节流流static void
write
(OutputStream out, boolean isCloseOut, byte[] content) 将byte[]写到流中static void
write
(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) 将多部分内容写到流中,自动转换为字符串static void
write
(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) 将多部分内容写到流中,自动转换为字符串static void
writeObj
(OutputStream out, boolean isCloseOut, Serializable obj) 将多部分内容写到流中static void
writeObjects
(OutputStream out, boolean isCloseOut, Serializable... contents) 将多部分内容写到流中static void
writeUtf8
(OutputStream out, boolean isCloseOut, Object... contents) 将多部分内容写到流中,自动转换为UTF-8字符串
-
Constructor Details
-
IoUtil
public IoUtil()
-
-
Method Details
-
copy
将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader- Parameters:
reader
- Readerwriter
- Writer- Returns:
- 拷贝的字节数
- Throws:
IOException
- IO异常
-
copy
将Reader中的内容复制到Writer中,拷贝后不关闭Reader- Parameters:
reader
- Readerwriter
- WriterbufferSize
- 缓存大小- 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
- Readerwriter
- WriterbufferSize
- 缓存大小streamProgress
- 进度处理器- Returns:
- 传输的byte数
- Throws:
IOException
- IO异常
-
copy
拷贝流,使用默认Buffer大小,拷贝后不关闭流- Parameters:
in
- 输入流out
- 输出流- Returns:
- 传输的byte数
- Throws:
IOException
- IO异常
-
copy
拷贝流,拷贝后不关闭流- 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
拷贝文件流,使用NIO- Parameters:
in
- 输入out
- 输出- Returns:
- 拷贝的字节数
- Throws:
IOException
- IO异常
-
getUtf8Reader
获得一个文件读取器,默认使用UTF-8编码- Parameters:
in
- 输入流- Returns:
- BufferedReader对象
- Since:
- 5.1.6
-
getReader
获得一个文件读取器- 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
获得一个Reader- Parameters:
in
- 输入流charset
- 字符集- Returns:
- BufferedReader对象
-
getReader
- Parameters:
reader
- 普通Reader,如果为null返回null- Returns:
BufferedReader
or null- Since:
- 3.0.9
-
getPushBackReader
- Parameters:
reader
- 普通ReaderpushBackSize
- 推后的byte数- Returns:
PushbackReader
- Since:
- 3.1.0
-
getUtf8Writer
获得一个Writer,默认编码UTF-8- Parameters:
out
- 输入流- Returns:
- OutputStreamWriter对象
- Since:
- 5.1.6
-
getWriter
获得一个Writer- Parameters:
out
- 输入流charsetName
- 字符集- Returns:
- OutputStreamWriter对象
-
getWriter
获得一个Writer- Parameters:
out
- 输入流charset
- 字符集- Returns:
- OutputStreamWriter对象
-
readUtf8
从流中读取UTF8编码的内容- Parameters:
in
- 输入流- Returns:
- 内容
- Throws:
IOException
- IO异常- Since:
- 5.4.4
-
read
从流中读取内容,读取完成后关闭流- Parameters:
in
- 输入流charsetName
- 字符集- Returns:
- 内容
- Throws:
IOException
- IO异常
-
read
从流中读取内容,读取完毕后并不关闭流- Parameters:
in
- 输入流,读取完毕后并不关闭流charset
- 字符集- Returns:
- 内容
- Throws:
IOException
- IO异常
-
read
从流中读取内容,读到输出流中,读取完毕后并不关闭流- Parameters:
in
- 输入流- Returns:
- 输出流
- Throws:
IOException
- IO异常
-
read
从流中读取内容,读到输出流中,读取完毕后并不关闭流- Parameters:
in
- 输入流isClose
- 读取完毕后是否关闭流- Returns:
- 输出流
- Throws:
IOException
- IO异常- Since:
- 5.5.3
-
read
从Reader中读取String,读取完毕后关闭Reader- Parameters:
reader
- Reader- Returns:
- String
- Throws:
IOException
- IO异常
-
read
从Reader
中读取String- Parameters:
reader
-Reader
isClose
- 是否关闭Reader
- Returns:
- String
- Throws:
IOException
- IO异常
-
readBytes
从流中读取bytes,读取完毕后关闭流- Parameters:
in
-InputStream
- Returns:
- bytes
- Throws:
IOException
- IO异常
-
readBytes
从流中读取bytes- Parameters:
in
-InputStream
isClose
- 是否关闭输入流- Returns:
- bytes
- Throws:
IOException
- IO异常- Since:
- 5.0.4
-
readBytes
读取指定长度的byte数组,不关闭流- Parameters:
in
-InputStream
,为null返回nulllength
- 长度,小于等于0返回空byte数组- Returns:
- bytes
- Throws:
IOException
- IO异常
-
readHex
读取16进制字符串- Parameters:
in
-InputStream
length
- 长度toLowerCase
- true 传换成小写格式 , false 传换成大写格式- Returns:
- 16进制字符串
- Throws:
IOException
- IO异常
-
readHex28Upper
从流中读取前28个byte并转换为16进制,字母部分使用大写- Parameters:
in
-InputStream
- Returns:
- 16进制字符串
- Throws:
IOException
- IO异常
-
readHex28Lower
从流中读取前28个byte并转换为16进制,字母部分使用小写- Parameters:
in
-InputStream
- Returns:
- 16进制字符串
- Throws:
IOException
- IO异常
-
readObj
从流中读取对象,即对象的反序列化注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
- Type Parameters:
T
- 读取对象的类型- Parameters:
in
- 输入流- Returns:
- 输出流
- Throws:
IOException
- IO异常UtilException
- ClassNotFoundException包装
-
readObj
从流中读取对象,即对象的反序列化,读取后不关闭流注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
- 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
按行读取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
按行读取数据,针对每行的数据做处理
Reader
自带编码定义,因此读取数据的编码跟随其编码。- Parameters:
reader
-Reader
lineHandler
- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方- Throws:
IOException
- IO异常
-
toStream
String 转为流- Parameters:
content
- 内容charsetName
- 编码- Returns:
- 字节流
-
toStream
String 转为流- Parameters:
content
- 内容charset
- 编码- Returns:
- 字节流
-
toUtf8Stream
String 转为UTF-8编码的字节流流- Parameters:
content
- 内容- Returns:
- 字节流
- Since:
- 4.5.1
-
toStream
文件转为FileInputStream
- Parameters:
file
- 文件- Returns:
FileInputStream
-
toStream
byte[] 转为ByteArrayInputStream
- Parameters:
content
- 内容bytes- Returns:
- 字节流
- Since:
- 4.1.8
-
toStream
- Parameters:
out
-ByteArrayOutputStream
- Returns:
- 字节流
- Since:
- 5.3.6
-
toBuffered
- Parameters:
in
-InputStream
- Returns:
BufferedInputStream
- Since:
- 4.0.10
-
toBuffered
- Parameters:
in
-InputStream
bufferSize
- buffer size- Returns:
BufferedInputStream
- Since:
- 5.6.1
-
toBuffered
- Parameters:
out
-OutputStream
- Returns:
BufferedOutputStream
- Since:
- 4.0.10
-
toBuffered
- Parameters:
out
-OutputStream
bufferSize
- buffer size- Returns:
BufferedOutputStream
- Since:
- 5.6.1
-
toBuffered
- Parameters:
reader
-Reader
- Returns:
BufferedReader
- Since:
- 5.6.1
-
toBuffered
- Parameters:
reader
-Reader
bufferSize
- buffer size- Returns:
BufferedReader
- Since:
- 5.6.1
-
toBuffered
- Parameters:
writer
-Writer
- Returns:
BufferedWriter
- Since:
- 5.6.1
-
toBuffered
- Parameters:
writer
-Writer
bufferSize
- buffer size- Returns:
BufferedWriter
- Since:
- 5.6.1
-
toMarkSupportStream
- Parameters:
in
- 流- Returns:
InputStream
- Since:
- 4.0.9
-
toPushbackStream
- Parameters:
in
-InputStream
pushBackSize
- 推后的byte数- Returns:
PushbackInputStream
- Since:
- 3.1.0
-
toAvailableStream
将指定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
将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
从缓存中刷出数据- Parameters:
flushable
-Flushable
- Since:
- 4.2.2
-
close
关闭
关闭失败不会抛出异常- Parameters:
closeable
- 被关闭的对象
-
closeIfPosible
尝试关闭指定对象
判断对象如果实现了AutoCloseable
,则调用之- Parameters:
obj
- 可关闭对象- Since:
- 4.3.2
-
contentEquals
对比两个流内容是否相同
内部会转换流为BufferedInputStream
- Parameters:
input1
- 第一个流input2
- 第二个流- Returns:
- 两个流的内容一致返回true,否则false
- Throws:
IOException
- IO异常- Since:
- 4.0.6
-
contentEquals
对比两个Reader的内容是否一致
内部会转换流为BufferedInputStream
- Parameters:
input1
- 第一个readerinput2
- 第二个reader- Returns:
- 两个流的内容一致返回true,否则false
- Throws:
IOException
- IO异常- Since:
- 4.0.6
-
contentEqualsIgnoreEOL
对比两个流内容是否相同,忽略EOL字符
内部会转换流为BufferedInputStream
- Parameters:
input1
- 第一个流input2
- 第二个流- Returns:
- 两个流的内容一致返回true,否则false
- Throws:
IOException
- IO异常- Since:
- 4.0.6
-
checksumCRC32
计算流CRC32校验码,计算后关闭流- Parameters:
in
- 文件,不能为目录- Returns:
- CRC32值
- Throws:
IOException
- IO异常- Since:
- 4.0.6
-
checksum
计算流的校验码,计算后关闭流- Parameters:
in
- 流checksum
-Checksum
- Returns:
- Checksum
- Throws:
IOException
- IO异常- Since:
- 4.0.10
-
checksumValue
计算流的校验码,计算后关闭流- Parameters:
in
- 流checksum
-Checksum
- Returns:
- Checksum
- Throws:
IOException
- IO异常- Since:
- 5.4.0
-
lineIter
返回行遍历器LineIterator it = null; try { it = IoUtil.lineIter(reader); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
-
lineIter
返回行遍历器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
-