public class IoUtil extends NioUtil
DEFAULT_BUFFER_SIZE, DEFAULT_LARGE_BUFFER_SIZE, DEFAULT_MIDDLE_BUFFER_SIZE, EOF
Constructor and Description |
---|
IoUtil() |
Modifier and Type | Method and Description |
---|---|
static Checksum |
checksum(InputStream in,
Checksum checksum)
计算流的校验码,计算后关闭流
|
static long |
checksumCRC32(InputStream in)
计算流CRC32校验码,计算后关闭流
|
static long |
checksumValue(InputStream in,
Checksum checksum)
计算流的校验码,计算后关闭流
|
static void |
close(Closeable closeable)
关闭
关闭失败不会抛出异常 |
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)
拷贝文件流,使用NIO
|
static 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 |
copy(Reader reader,
Writer writer)
将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader
|
static long |
copy(Reader reader,
Writer writer,
int bufferSize)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
|
static long |
copy(Reader reader,
Writer writer,
int bufferSize,
StreamProgress streamProgress)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
|
static void |
flush(Flushable flushable)
从缓存中刷出数据
|
static PushbackReader |
getPushBackReader(Reader reader,
int pushBackSize)
|
static BufferedReader |
getReader(BOMInputStream in)
从
BOMInputStream 中获取Reader |
static BufferedReader |
getReader(InputStream in,
Charset charset)
获得一个Reader
|
static BufferedReader |
getReader(InputStream in,
String charsetName)
获得一个文件读取器
|
static BufferedReader |
getReader(Reader reader)
|
static BufferedReader |
getUtf8Reader(InputStream in)
获得一个文件读取器,默认使用UTF-8编码
|
static OutputStreamWriter |
getUtf8Writer(OutputStream out)
获得一个Writer,默认编码UTF-8
|
static OutputStreamWriter |
getWriter(OutputStream out,
Charset charset)
获得一个Writer
|
static OutputStreamWriter |
getWriter(OutputStream out,
String charsetName)
获得一个Writer
|
static FastByteArrayOutputStream |
read(InputStream in)
从流中读取内容,读到输出流中,读取完毕后并不关闭流
|
static FastByteArrayOutputStream |
read(InputStream in,
boolean isClose)
从流中读取内容,读到输出流中,读取完毕后并不关闭流
|
static String |
read(InputStream in,
Charset charset)
从流中读取内容,读取完毕后并不关闭流
|
static String |
read(InputStream in,
String charsetName)
从流中读取内容,读取完成后关闭流
|
static String |
read(Reader reader)
从Reader中读取String,读取完毕后关闭Reader
|
static String |
read(Reader reader,
boolean isClose)
从
Reader 中读取String |
static byte[] |
readBytes(InputStream in)
从流中读取bytes,读取完毕后关闭流
|
static byte[] |
readBytes(InputStream in,
boolean isCLose)
从流中读取bytes
|
static byte[] |
readBytes(InputStream in,
int length)
读取指定长度的byte数组,不关闭流
|
static String |
readHex(InputStream in,
int length,
boolean toLowerCase)
读取16进制字符串
|
static String |
readHex28Lower(InputStream in)
从流中读取前28个byte并转换为16进制,字母部分使用小写
|
static String |
readHex28Upper(InputStream in)
从流中读取前28个byte并转换为16进制,字母部分使用大写
|
static void |
readLines(InputStream in,
Charset charset,
LineHandler lineHandler)
按行读取数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readLines(InputStream in,
Charset charset,
T collection)
从流中读取内容
|
static <T extends Collection<String>> |
readLines(InputStream in,
String charsetName,
T collection)
从流中读取内容
|
static void |
readLines(Reader reader,
LineHandler lineHandler)
按行读取数据,针对每行的数据做处理
Reader 自带编码定义,因此读取数据的编码跟随其编码。 |
static <T extends Collection<String>> |
readLines(Reader reader,
T collection)
从Reader中读取内容
|
static <T> T |
readObj(InputStream in)
从流中读取对象,即对象的反序列化
|
static <T> T |
readObj(InputStream in,
Class<T> clazz)
从流中读取对象,即对象的反序列化,读取后不关闭流
|
static <T> T |
readObj(ValidateObjectInputStream in,
Class<T> clazz)
从流中读取对象,即对象的反序列化,读取后不关闭流
|
static String |
readUtf8(InputStream in)
从流中读取UTF8编码的内容
|
static void |
readUtf8Lines(InputStream in,
LineHandler lineHandler)
按行读取UTF-8编码数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readUtf8Lines(InputStream in,
T collection)
从流中读取内容,使用UTF-8编码
|
static InputStream |
toAvailableStream(InputStream in)
将指定
InputStream 转换为InputStream.available() 方法可用的流。在Socket通信流中,服务端未返回数据情况下 InputStream.available() 方法始终为0 因此,在读取前需要调用 InputStream.read() 读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available() 方法就正常了。需要注意的是,在网络流中,是按照块来传输的,所以 InputStream.available() 读取到的并非最终长度,而是此次块的长度。此方法返回对象的规则为: FileInputStream 返回原对象,因为文件流的available方法本身可用 其它InputStream 返回PushbackInputStream |
static BufferedInputStream |
toBuffered(InputStream in)
|
static BufferedOutputStream |
toBuffered(OutputStream out)
|
static InputStream |
toMarkSupportStream(InputStream in)
|
static PushbackInputStream |
toPushbackStream(InputStream in,
int pushBackSize)
|
static ByteArrayInputStream |
toStream(byte[] content)
String 转为流
|
static FileInputStream |
toStream(File file)
文件转为流
|
static ByteArrayInputStream |
toStream(String content,
Charset charset)
String 转为流
|
static ByteArrayInputStream |
toStream(String content,
String charsetName)
String 转为流
|
static ByteArrayInputStream |
toUtf8Stream(String content)
String 转为UTF-8编码的字节流流
|
static void |
write(OutputStream out,
boolean isCloseOut,
byte[] content)
将byte[]写到流中
|
static void |
write(OutputStream out,
Charset charset,
boolean isCloseOut,
Object... contents)
将多部分内容写到流中,自动转换为字符串
|
static void |
write(OutputStream out,
String charsetName,
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字符串
|
public static long copy(Reader reader, Writer writer) throws IORuntimeException
reader
- Readerwriter
- WriterIORuntimeException
- IO异常public static long copy(Reader reader, Writer writer, int bufferSize) throws IORuntimeException
reader
- Readerwriter
- WriterbufferSize
- 缓存大小IORuntimeException
- IO异常public static long copy(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) throws IORuntimeException
reader
- Readerwriter
- WriterbufferSize
- 缓存大小streamProgress
- 进度处理器IORuntimeException
- IO异常public static long copy(InputStream in, OutputStream out) throws IORuntimeException
in
- 输入流out
- 输出流IORuntimeException
- IO异常public static long copy(InputStream in, OutputStream out, int bufferSize) throws IORuntimeException
in
- 输入流out
- 输出流bufferSize
- 缓存大小IORuntimeException
- IO异常public static long copy(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws IORuntimeException
in
- 输入流out
- 输出流bufferSize
- 缓存大小streamProgress
- 进度条IORuntimeException
- IO异常public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException
in
- 输入out
- 输出IORuntimeException
- IO异常public static BufferedReader getUtf8Reader(InputStream in)
in
- 输入流public static BufferedReader getReader(InputStream in, String charsetName)
in
- 输入流charsetName
- 字符集名称public static BufferedReader getReader(BOMInputStream in)
BOMInputStream
中获取Readerin
- BOMInputStream
BufferedReader
public static BufferedReader getReader(InputStream in, Charset charset)
in
- 输入流charset
- 字符集public static BufferedReader getReader(Reader reader)
reader
- 普通Reader,如果为null返回nullBufferedReader
or nullpublic static PushbackReader getPushBackReader(Reader reader, int pushBackSize)
reader
- 普通ReaderpushBackSize
- 推后的byte数PushbackReader
public static OutputStreamWriter getUtf8Writer(OutputStream out)
out
- 输入流public static OutputStreamWriter getWriter(OutputStream out, String charsetName)
out
- 输入流charsetName
- 字符集public static OutputStreamWriter getWriter(OutputStream out, Charset charset)
out
- 输入流charset
- 字符集public static String readUtf8(InputStream in) throws IORuntimeException
in
- 输入流IORuntimeException
- IO异常public static String read(InputStream in, String charsetName) throws IORuntimeException
in
- 输入流charsetName
- 字符集IORuntimeException
- IO异常public static String read(InputStream in, Charset charset) throws IORuntimeException
in
- 输入流,读取完毕后并不关闭流charset
- 字符集IORuntimeException
- IO异常public static FastByteArrayOutputStream read(InputStream in) throws IORuntimeException
in
- 输入流IORuntimeException
- IO异常public static FastByteArrayOutputStream read(InputStream in, boolean isClose) throws IORuntimeException
in
- 输入流isClose
- 读取完毕后是否关闭流IORuntimeException
- IO异常public static String read(Reader reader) throws IORuntimeException
reader
- ReaderIORuntimeException
- IO异常public static String read(Reader reader, boolean isClose) throws IORuntimeException
Reader
中读取Stringreader
- Reader
isClose
- 是否关闭Reader
IORuntimeException
- IO异常public static byte[] readBytes(InputStream in) throws IORuntimeException
in
- InputStream
IORuntimeException
- IO异常public static byte[] readBytes(InputStream in, boolean isCLose) throws IORuntimeException
in
- InputStream
isCLose
- 是否关闭输入流IORuntimeException
- IO异常public static byte[] readBytes(InputStream in, int length) throws IORuntimeException
in
- InputStream
,为null返回nulllength
- 长度,小于等于0返回空byte数组IORuntimeException
- IO异常public static String readHex(InputStream in, int length, boolean toLowerCase) throws IORuntimeException
in
- InputStream
length
- 长度toLowerCase
- true 传换成小写格式 , false 传换成大写格式IORuntimeException
- IO异常public static String readHex28Upper(InputStream in) throws IORuntimeException
in
- InputStream
IORuntimeException
- IO异常public static String readHex28Lower(InputStream in) throws IORuntimeException
in
- InputStream
IORuntimeException
- IO异常public static <T> T readObj(InputStream in) throws IORuntimeException, UtilException
注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
T
- 读取对象的类型in
- 输入流IORuntimeException
- IO异常UtilException
- ClassNotFoundException包装public static <T> T readObj(InputStream in, Class<T> clazz) throws IORuntimeException, UtilException
注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
T
- 读取对象的类型in
- 输入流clazz
- 读取对象类型IORuntimeException
- IO异常UtilException
- ClassNotFoundException包装public static <T> T readObj(ValidateObjectInputStream in, Class<T> clazz) throws IORuntimeException, UtilException
此方法使用了ValidateObjectInputStream
中的黑白名单方式过滤类,用于避免反序列化漏洞
通过构造ValidateObjectInputStream
,调用ValidateObjectInputStream.accept(Class[])
或者ValidateObjectInputStream.refuse(Class[])
方法添加可以被序列化的类或者禁止序列化的类。
T
- 读取对象的类型in
- 输入流,使用ValidateObjectInputStream
中的黑白名单方式过滤类,用于避免反序列化漏洞clazz
- 读取对象类型IORuntimeException
- IO异常UtilException
- ClassNotFoundException包装public static <T extends Collection<String>> T readUtf8Lines(InputStream in, T collection) throws IORuntimeException
T
- 集合类型in
- 输入流collection
- 返回集合IORuntimeException
- IO异常public static <T extends Collection<String>> T readLines(InputStream in, String charsetName, T collection) throws IORuntimeException
T
- 集合类型in
- 输入流charsetName
- 字符集collection
- 返回集合IORuntimeException
- IO异常public static <T extends Collection<String>> T readLines(InputStream in, Charset charset, T collection) throws IORuntimeException
T
- 集合类型in
- 输入流charset
- 字符集collection
- 返回集合IORuntimeException
- IO异常public static <T extends Collection<String>> T readLines(Reader reader, T collection) throws IORuntimeException
T
- 集合类型reader
- Reader
collection
- 返回集合IORuntimeException
- IO异常public static void readUtf8Lines(InputStream in, LineHandler lineHandler) throws IORuntimeException
in
- InputStream
lineHandler
- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException
- IO异常public static void readLines(InputStream in, Charset charset, LineHandler lineHandler) throws IORuntimeException
in
- InputStream
charset
- Charset
编码lineHandler
- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException
- IO异常public static void readLines(Reader reader, LineHandler lineHandler) throws IORuntimeException
Reader
自带编码定义,因此读取数据的编码跟随其编码。reader
- Reader
lineHandler
- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方IORuntimeException
- IO异常public static ByteArrayInputStream toStream(String content, String charsetName)
content
- 内容charsetName
- 编码public static ByteArrayInputStream toStream(String content, Charset charset)
content
- 内容charset
- 编码public static ByteArrayInputStream toUtf8Stream(String content)
content
- 内容public static FileInputStream toStream(File file)
file
- 文件FileInputStream
public static ByteArrayInputStream toStream(byte[] content)
content
- 内容bytespublic static BufferedInputStream toBuffered(InputStream in)
in
- InputStream
BufferedInputStream
public static BufferedOutputStream toBuffered(OutputStream out)
out
- OutputStream
BufferedOutputStream
public static InputStream toMarkSupportStream(InputStream in)
in
- 流InputStream
public static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize)
in
- InputStream
pushBackSize
- 推后的byte数PushbackInputStream
public static InputStream toAvailableStream(InputStream in)
InputStream
转换为InputStream.available()
方法可用的流。InputStream.available()
方法始终为0
InputStream.read()
读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()
方法就正常了。InputStream.available()
读取到的并非最终长度,而是此次块的长度。in
- 被转换的流PushbackInputStream
public static void write(OutputStream out, boolean isCloseOut, byte[] content) throws IORuntimeException
out
- 输出流isCloseOut
- 写入完毕是否关闭输出流content
- 写入的内容IORuntimeException
- IO异常public static void writeUtf8(OutputStream out, boolean isCloseOut, Object... contents) throws IORuntimeException
out
- 输出流isCloseOut
- 写入完毕是否关闭输出流contents
- 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException
- IO异常public static void write(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) throws IORuntimeException
out
- 输出流charsetName
- 写出的内容的字符集isCloseOut
- 写入完毕是否关闭输出流contents
- 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException
- IO异常public static void write(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) throws IORuntimeException
out
- 输出流charset
- 写出的内容的字符集isCloseOut
- 写入完毕是否关闭输出流contents
- 写入的内容,调用toString()方法,不包括不会自动换行IORuntimeException
- IO异常public static void writeObj(OutputStream out, boolean isCloseOut, Serializable obj) throws IORuntimeException
out
- 输出流isCloseOut
- 写入完毕是否关闭输出流obj
- 写入的对象内容IORuntimeException
- IO异常public static void writeObjects(OutputStream out, boolean isCloseOut, Serializable... contents) throws IORuntimeException
out
- 输出流isCloseOut
- 写入完毕是否关闭输出流contents
- 写入的内容IORuntimeException
- IO异常public static void flush(Flushable flushable)
flushable
- Flushable
public static void close(Closeable closeable)
closeable
- 被关闭的对象public static void closeIfPosible(Object obj)
AutoCloseable
,则调用之obj
- 可关闭对象public static boolean contentEquals(InputStream input1, InputStream input2) throws IORuntimeException
BufferedInputStream
input1
- 第一个流input2
- 第二个流IORuntimeException
- IO异常public static boolean contentEquals(Reader input1, Reader input2) throws IORuntimeException
BufferedInputStream
input1
- 第一个readerinput2
- 第二个readerIORuntimeException
- IO异常public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws IORuntimeException
BufferedInputStream
input1
- 第一个流input2
- 第二个流IORuntimeException
- IO异常public static long checksumCRC32(InputStream in) throws IORuntimeException
in
- 文件,不能为目录IORuntimeException
- IO异常public static Checksum checksum(InputStream in, Checksum checksum) throws IORuntimeException
in
- 流checksum
- Checksum
IORuntimeException
- IO异常public static long checksumValue(InputStream in, Checksum checksum)
in
- 流checksum
- Checksum
IORuntimeException
- IO异常Copyright © 2021. All rights reserved.