Package org.miaixz.bus.core.xyz
Class IoKit
java.lang.Object
org.miaixz.bus.core.xyz.IoKit
IO工具类 IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic SinkappendingSink(File file) 返回一个附加到file的接收器static booleanarrayRangeEquals(byte[] a, int aOffset, byte[] b, int bOffset, int byteCount) static Sink返回一个都不写的接收器static BufferSink返回一个新接收器,该接收器缓冲写sink返回的接收器将批量写入sinkstatic BufferSource返回缓冲区从source读取的字节流 返回的源将对其内存缓冲区执行批量读取static voidcheckOffsetAndCount(long size, long offset, long byteCount) static void关闭 关闭失败不会抛出异常static voidclose(AutoCloseable autoCloseable) 关闭 关闭失败不会抛出异常static void尝试关闭指定对象 判断对象如果实现了AutoCloseable,则调用之static voidclose(ServerSocket serverSocket) static void关闭 关闭失败不会抛出异常static voidclose(AsynchronousSocketChannel channel) static voidcloseQuietly(AutoCloseable... closeables) 按照给定顺序连续关闭一系列对象 这些对象必须按照顺序关闭,否则会出错。static booleancontentEquals(InputStream input1, InputStream input2) 对比两个流内容是否相同 内部会转换流为BufferedInputStreamstatic booleancontentEquals(Reader input1, Reader input2) 对比两个Reader的内容是否一致 内部会转换流为BufferedInputStreamstatic booleancontentEqualsIgnoreEOL(Reader input1, Reader input2) 对比两个流内容是否相同,忽略EOL字符 内部会转换流为BufferedInputStreamstatic longcopy(FileInputStream in, FileOutputStream out) 拷贝文件流,使用NIOstatic longcopy(InputStream in, OutputStream out) 拷贝流,使用默认Buffer大小,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, byte[] buffer) 拷贝流,拷贝后不关闭流static longcopy(InputStream in, OutputStream out, int bufferSize) 拷贝流,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, int bufferSize, byte[] buffer) 拷贝流,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, int len, int swapBytes) 拷贝流,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, int bufferSize, int swapBytes, byte[] buffer) 拷贝流,拷贝后不关闭流static longcopy(InputStream in, OutputStream out, int bufferSize, long count, StreamProgress streamProgress) 拷贝流,拷贝后不关闭流static longcopy(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) 拷贝流,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, long bufferSize) 拷贝流,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, long bufferSize, byte[] buffer) 拷贝流,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, long len, int swapBytes) 拷贝流,拷贝后不关闭流static voidcopy(InputStream in, OutputStream out, long bufferSize, int swapBytes, byte[] buffer) 拷贝流,拷贝后不关闭流static long将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Readerstatic long将Reader中的内容复制到Writer中,拷贝后不关闭Readerstatic longcopy(Reader reader, Writer writer, int bufferSize, long count, StreamProgress streamProgress) 将Reader中的内容复制到Writer中,拷贝后不关闭Readerstatic longcopy(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) 将Reader中的内容复制到Writer中,拷贝后不关闭Readerstatic longcopy(FileChannel in, FileChannel out) 拷贝文件Channel,使用NIO,拷贝后不会关闭channelstatic longcopy(ReadableByteChannel in, WritableByteChannel out) 拷贝流,使用NIO,不会关闭channelstatic longcopy(ReadableByteChannel in, WritableByteChannel out, int bufferSize) 拷贝流,使用NIO,不会关闭channelstatic longcopy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, long totalCount, StreamProgress streamProgress) 拷贝流,使用NIO,不会关闭channelstatic longcopy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, StreamProgress streamProgress) 拷贝流,使用NIO,不会关闭channelstatic longcopyNio(InputStream in, OutputStream out, int bufferSize, long count, StreamProgress streamProgress) 拷贝流 本方法不会关闭流static longcopyNio(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) 拷贝流 thanks to: https://github.com/venusdrogon/feilong-io/blob/master/src/main/java/com/feilong/io/IOWriteUtil.java 本方法不会关闭流static void从缓存中刷出数据static booleanReturns true ifeis due to a firmware bug fixed after Android 4.2.2.static LineIteratorlineIter(InputStream in, Charset charset) 返回行遍历器static LineIterator返回行遍历器static voidnullSafeClose(Closeable closeable) 关闭 关闭失败抛出IOException异常static InputStreamopenFileOrURL(String name) static FastByteArrayOutputStreamread(InputStream in) 从流中读取内容,读到输出流中,读取完毕后关闭流static FastByteArrayOutputStreamread(InputStream in, boolean isClose) 从流中读取内容,读到输出流中,读取完毕后可选是否关闭流static Stringread(InputStream in, Charset charset) 从流中读取内容,读取完毕后关闭流static String从Reader中读取String,读取完毕后关闭Readerstatic String从Reader中读取Stringstatic Stringread(FileChannel fileChannel) 从FileChannel中读取UTF-8编码内容static Stringread(FileChannel fileChannel, Charset charset) 从FileChannel中读取内容static FastByteArrayOutputStreamread(ReadableByteChannel channel) 从流中读取内容,读到输出流中static Stringread(ReadableByteChannel channel, Charset charset) 从流中读取内容,读取完毕后并不关闭流static byte[]readBytes(InputStream in) 从流中读取bytes,读取完毕后关闭流static byte[]readBytes(InputStream in, boolean isClose) 从流中读取bytesstatic byte[]readBytes(InputStream in, int length) 读取指定长度的byte数组,不关闭流static StringreadHex(InputStream in, int length, boolean toLowerCase) 读取16进制字符串static voidreadLines(InputStream in, Charset charset, ConsumerX<String> lineHandler) 按行读取数据,针对每行的数据做处理static <T extends Collection<String>>
TreadLines(InputStream in, Charset charset, T collection) 从流中读取内容static voidreadLines(InputStream in, ConsumerX<String> lineHandler) 按行读取UTF-8编码数据,针对每行的数据做处理static <T extends Collection<String>>
TreadLines(InputStream in, T collection) 从流中读取内容,使用UTF-8编码static void按行读取数据,针对每行的数据做处理Reader自带编码定义,因此读取数据的编码跟随其编码。 此方法不会关闭流,除非抛出异常static <T extends Collection<String>>
T从Reader中读取内容static <T> TreadObject(InputStream in, Class<?>... acceptClasses) 从流中读取对象,即对象的反序列化,读取后不关闭流static StringreadUtf8(InputStream in) 从流中读取UTF8编码的内容static intreverseBytesInt(int i) static longreverseBytesLong(long v) static shortreverseBytesShort(short s) static Sink返回一个向file写入的接收器static Sinksink(OutputStream out) 返回一个向out写入的接收器static Sink返回一个向socket写入的接收器。优先选择这个方法, 而不是sink(OutputStream),因为这个方法支持超时 当套接字写超时时,套接字将由任务线程异步关闭static Sinksink(Path path, OpenOption... options) 返回一个向path写入的接收器.static void即使被声明也不允许直接抛出 这是一种很糟糕的做饭,很容易遭到攻击 清理后捕获并重新抛出异常static Source返回从file读取的缓冲数据static Sourcesource(InputStream in) 返回从in中读取的缓冲数据static Source返回从socket读取的缓存信息。与source(InputStream)相比, 更喜欢这个方法, 因为这个方法支持超时。当套接字读取超时时,套接字将由任务线程异步关闭static Sourcesource(Path path, OpenOption... options) 返回从path读取的缓冲数据static InputStream将指定InputStream转换为InputStream.available()方法可用的流。 在Socket通信流中,服务端未返回数据情况下InputStream.available()方法始终为0因此,在读取前需要调用InputStream.read()读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()方法就正常了。 需要注意的是,在网络流中,是按照块来传输的,所以InputStream.available()读取到的并非最终长度,而是此次块的长度。 此方法返回对象的规则为: FileInputStream 返回原对象,因为文件流的available方法本身可用 其它InputStream 返回PushbackInputStreamstatic BomReaderstatic BufferedInputStreamstatic BufferedInputStreamtoBuffered(InputStream in, int bufferSize) static BufferedOutputStreamtoBuffered(OutputStream out) static BufferedOutputStreamtoBuffered(OutputStream out, int bufferSize) static BufferedReadertoBuffered(Reader reader) static BufferedReadertoBuffered(Reader reader, int bufferSize) static BufferedWritertoBuffered(Writer writer) static BufferedWritertoBuffered(Writer writer, int bufferSize) static InputStream将InputStream转换为支持mark标记的流 若原流支持mark标记,则返回原流,否则使用BufferedInputStream包装之static ReadertoMarkSupport(Reader reader) 将Reader转换为支持mark标记的Reader 若原Reader支持mark标记,则返回原Reader,否则使用BufferedReader包装之static PushbackReadertoPushBackReader(Reader reader, int pushBackSize) 获得PushbackReader如果是PushbackReader强转返回,否则新建static PushbackInputStreamtoPushbackStream(InputStream in, int pushBackSize) 转换为PushbackInputStream如果传入的输入流已经是PushbackInputStream,强转返回,否则新建一个static BufferedReadertoReader(InputStream in, Charset charset) 获得一个Readerstatic ByteArrayInputStreamtoStream(byte[] content) byte[] 转为ByteArrayInputStreamstatic ByteArrayInputStreamstatic InputStream文件转为InputStreamstatic ByteArrayInputStreamString 转为UTF-8编码的字节流流static ByteArrayInputStreamString 转为流static InputStream文件转为InputStreamstatic ByteArrayInputStreamstatic StringtoString(ByteArrayOutputStream out, Charset charset) ByteArrayOutputStream转换为Stringstatic BufferedReader获得一个文件读取器,默认使用 UTF-8 编码static OutputStreamWritertoUtf8Writer(OutputStream out) 获得一个Writer,默认编码UTF-8static OutputStreamWritertoWriter(OutputStream out, Charset charset) 获得一个Writerstatic voidwrite(OutputStream out, boolean isCloseOut, byte[] content) 将byte[]写到流中static voidwrite(OutputStream out, boolean isCloseOut, Object... contents) 将多部分内容写到流中,自动转换为UTF-8字符串static voidwrite(OutputStream out, byte[] content) 将byte[]写到流中,并关闭目标流static voidwrite(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) 将多部分内容写到流中,自动转换为字符串static voidwriteObjects(OutputStream out, boolean isCloseOut, Object... contents) 将多部分内容写到流中
-
Constructor Details
-
IoKit
public IoKit()
-
-
Method Details
-
copy
将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader- Parameters:
reader- Readerwriter- Writer- Returns:
- 拷贝的字节数
- Throws:
InternalException- IO异常
-
copy
将Reader中的内容复制到Writer中,拷贝后不关闭Reader- Parameters:
reader- Readerwriter- WriterbufferSize- 缓存大小- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copy
public static long copy(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) throws InternalException 将Reader中的内容复制到Writer中,拷贝后不关闭Reader- Parameters:
reader- Readerwriter- WriterbufferSize- 缓存大小streamProgress- 进度处理器- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copy
public static long copy(Reader reader, Writer writer, int bufferSize, long count, StreamProgress streamProgress) throws InternalException 将Reader中的内容复制到Writer中,拷贝后不关闭Reader- Parameters:
reader- Reader,非空writer- Writer,非空bufferSize- 缓存大小,-1表示默认count- 最大长度,-1表示无限制streamProgress- 进度处理器,null表示无- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copy
拷贝流,使用默认Buffer大小,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copy
拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copy
public static long copy(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小streamProgress- 进度条- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copy
public static long copy(InputStream in, OutputStream out, int bufferSize, long count, StreamProgress streamProgress) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小count- 总拷贝长度,-1表示无限制streamProgress- 进度条- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copy
拷贝文件流,使用NIO- Parameters:
in- 输入out- 输出- Returns:
- 拷贝的字节数
- Throws:
InternalException- IO异常
-
copy
拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流buffer- 缓存- Throws:
IOException
-
copy
public static void copy(InputStream in, OutputStream out, int len, int swapBytes) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流swapBytes- 交换字节- Throws:
InternalException- IO异常
-
copy
public static void copy(InputStream in, OutputStream out, long len, int swapBytes) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流swapBytes- 交换字节- Throws:
InternalException- IO异常
-
copy
拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小- Throws:
InternalException- IO异常
-
copy
public static void copy(InputStream in, OutputStream out, int bufferSize, byte[] buffer) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小buffer- 缓存- Throws:
InternalException- IO异常
-
copy
public static void copy(InputStream in, OutputStream out, long bufferSize, byte[] buffer) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小buffer- 缓存- Throws:
InternalException- IO异常
-
copy
public static void copy(InputStream in, OutputStream out, int bufferSize, int swapBytes, byte[] buffer) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小swapBytes- 交换字节buffer- 缓存- Throws:
InternalException- IO异常
-
copy
public static void copy(InputStream in, OutputStream out, long bufferSize, int swapBytes, byte[] buffer) throws InternalException 拷贝流,拷贝后不关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小swapBytes- 交换字节buffer- 缓存- Throws:
InternalException- IO异常
-
toUtf8Reader
获得一个文件读取器,默认使用 UTF-8 编码- Parameters:
in- 输入流- Returns:
- BufferedReader对象
-
toBomReader
- Parameters:
in-InputStream- Returns:
BomReader
-
toReader
获得一个Reader- Parameters:
in- 输入流charset- 字符集- Returns:
- BufferedReader对象
-
toUtf8Writer
获得一个Writer,默认编码UTF-8- Parameters:
out- 输入流- Returns:
- OutputStreamWriter对象
-
toWriter
获得一个Writer- Parameters:
out- 输入流charset- 字符集- Returns:
- OutputStreamWriter对象
-
readUtf8
从流中读取UTF8编码的内容- Parameters:
in- 输入流- Returns:
- 内容
- Throws:
InternalException- IO异常
-
read
从流中读取内容,读取完毕后关闭流- Parameters:
in- 输入流,读取完毕后关闭流charset- 字符集- Returns:
- 内容
- Throws:
InternalException- IO异常
-
read
从流中读取内容,读到输出流中,读取完毕后关闭流- Parameters:
in- 输入流- Returns:
- 输出流
- Throws:
InternalException- IO异常
-
read
public static FastByteArrayOutputStream read(InputStream in, boolean isClose) throws InternalException 从流中读取内容,读到输出流中,读取完毕后可选是否关闭流- Parameters:
in- 输入流isClose- 读取完毕后是否关闭流- Returns:
- 输出流
- Throws:
InternalException- IO异常
-
read
从Reader中读取String,读取完毕后关闭Reader- Parameters:
reader- Reader- Returns:
- String
- Throws:
InternalException- IO异常
-
read
从Reader中读取String- Parameters:
reader-ReaderisClose- 是否关闭Reader- Returns:
- String
- Throws:
InternalException- IO异常
-
readBytes
从流中读取bytes,读取完毕后关闭流- Parameters:
in-InputStream- Returns:
- bytes
- Throws:
InternalException- IO异常
-
readBytes
从流中读取bytes- Parameters:
in-InputStreamisClose- 是否关闭输入流- Returns:
- bytes
- Throws:
InternalException- IO异常
-
readBytes
读取指定长度的byte数组,不关闭流- Parameters:
in-InputStream,为null返回nulllength- 长度,小于等于0返回空byte数组- Returns:
- bytes
- Throws:
InternalException- IO异常
-
readHex
public static String readHex(InputStream in, int length, boolean toLowerCase) throws InternalException 读取16进制字符串- Parameters:
in-InputStreamlength- 长度toLowerCase- true 传换成小写格式 , false 传换成大写格式- Returns:
- 16进制字符串
- Throws:
InternalException- IO异常
-
readObject
从流中读取对象,即对象的反序列化,读取后不关闭流注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
- Type Parameters:
T- 读取对象的类型- Parameters:
in- 输入流acceptClasses- 读取对象类型- Returns:
- 输出流
- Throws:
InternalException- IO异常InternalException- ClassNotFoundException包装
-
readLines
public static <T extends Collection<String>> T readLines(InputStream in, T collection) throws InternalException 从流中读取内容,使用UTF-8编码- Type Parameters:
T- 集合类型- Parameters:
in- 输入流collection- 返回集合- Returns:
- 内容
- Throws:
InternalException- IO异常
-
readLines
public static <T extends Collection<String>> T readLines(InputStream in, Charset charset, T collection) throws InternalException 从流中读取内容- Type Parameters:
T- 集合类型- Parameters:
in- 输入流charset- 字符集collection- 返回集合- Returns:
- 内容
- Throws:
InternalException- IO异常
-
readLines
public static <T extends Collection<String>> T readLines(Reader reader, T collection) throws InternalException 从Reader中读取内容- Type Parameters:
T- 集合类型- Parameters:
reader-Readercollection- 返回集合- Returns:
- 内容
- Throws:
InternalException- IO异常
-
readLines
public static void readLines(InputStream in, ConsumerX<String> lineHandler) throws InternalException 按行读取UTF-8编码数据,针对每行的数据做处理- Parameters:
in-InputStreamlineHandler- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方- Throws:
InternalException- IO异常
-
readLines
public static void readLines(InputStream in, Charset charset, ConsumerX<String> lineHandler) throws InternalException 按行读取数据,针对每行的数据做处理- Parameters:
in-InputStreamcharset-Charset编码lineHandler- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方- Throws:
InternalException- IO异常
-
readLines
按行读取数据,针对每行的数据做处理Reader自带编码定义,因此读取数据的编码跟随其编码。 此方法不会关闭流,除非抛出异常- Parameters:
reader-ReaderlineHandler- 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方- Throws:
InternalException- IO异常
-
toStream
String 转为UTF-8编码的字节流流- Parameters:
content- 内容- Returns:
- 字节流
-
toStream
String 转为流- Parameters:
content- 内容charset- 编码- Returns:
- 字节流
-
toStream
文件转为InputStream- Parameters:
file- 文件,非空- Returns:
InputStream
-
toStream
文件转为InputStream- Parameters:
path-Path,非空- Returns:
InputStream
-
toStream
byte[] 转为ByteArrayInputStream- Parameters:
content- 内容bytes- Returns:
- 字节流
-
toStream
- Parameters:
out-ByteArrayOutputStream- Returns:
- 字节流
-
toStream
- Parameters:
out-FastByteArrayOutputStream- Returns:
- 字节流
-
toBuffered
- Parameters:
in-InputStream- Returns:
BufferedInputStream
-
toBuffered
- Parameters:
in-InputStreambufferSize- buffer size- Returns:
BufferedInputStream
-
toBuffered
- Parameters:
out-OutputStream- Returns:
BufferedOutputStream
-
toBuffered
- Parameters:
out-OutputStreambufferSize- buffer size- Returns:
BufferedOutputStream
-
toBuffered
- Parameters:
reader-Reader- Returns:
BufferedReader
-
toBuffered
- Parameters:
reader-ReaderbufferSize- buffer size- Returns:
BufferedReader
-
toBuffered
- Parameters:
writer-Writer- Returns:
BufferedWriter
-
toBuffered
- Parameters:
writer-WriterbufferSize- buffer size- Returns:
BufferedWriter
-
toMarkSupport
将InputStream转换为支持mark标记的流 若原流支持mark标记,则返回原流,否则使用BufferedInputStream包装之- Parameters:
in- 流- Returns:
InputStream
-
toMarkSupport
将Reader转换为支持mark标记的Reader 若原Reader支持mark标记,则返回原Reader,否则使用BufferedReader包装之 -
toPushBackReader
获得PushbackReader如果是PushbackReader强转返回,否则新建- Parameters:
reader- 普通ReaderpushBackSize- 推后的byte数- Returns:
PushbackReader
-
toPushbackStream
转换为PushbackInputStream如果传入的输入流已经是PushbackInputStream,强转返回,否则新建一个- Parameters:
in-InputStreampushBackSize- 推后的byte数- Returns:
PushbackInputStream
-
toAvailableStream
将指定InputStream转换为InputStream.available()方法可用的流。 在Socket通信流中,服务端未返回数据情况下InputStream.available()方法始终为0因此,在读取前需要调用InputStream.read()读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()方法就正常了。 需要注意的是,在网络流中,是按照块来传输的,所以InputStream.available()读取到的并非最终长度,而是此次块的长度。 此方法返回对象的规则为:- FileInputStream 返回原对象,因为文件流的available方法本身可用
- 其它InputStream 返回PushbackInputStream
- Parameters:
in- 被转换的流- Returns:
- 转换后的流,可能为
PushbackInputStream
-
write
将byte[]写到流中,并关闭目标流- Parameters:
out- 输出流content- 写入的内容- Throws:
InternalException- IO异常
-
write
public static void write(OutputStream out, boolean isCloseOut, byte[] content) throws InternalException 将byte[]写到流中- Parameters:
out- 输出流isCloseOut- 写入完毕是否关闭输出流content- 写入的内容- Throws:
InternalException- IO异常
-
write
public static void write(OutputStream out, boolean isCloseOut, Object... contents) throws InternalException 将多部分内容写到流中,自动转换为UTF-8字符串- Parameters:
out- 输出流isCloseOut- 写入完毕是否关闭输出流contents- 写入的内容,调用toString()方法,不包括不会自动换行- Throws:
InternalException- IO异常
-
write
public static void write(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) throws InternalException 将多部分内容写到流中,自动转换为字符串- Parameters:
out- 输出流charset- 写出的内容的字符集isCloseOut- 写入完毕是否关闭输出流contents- 写入的内容,调用toString()方法,不包括不会自动换行- Throws:
InternalException- IO异常
-
writeObjects
public static void writeObjects(OutputStream out, boolean isCloseOut, Object... contents) throws InternalException 将多部分内容写到流中- Parameters:
out- 输出流isCloseOut- 写入完毕是否关闭输出流contents- 写入的内容- Throws:
InternalException- IO异常
-
read
从FileChannel中读取UTF-8编码内容- Parameters:
fileChannel- 文件管道- Returns:
- 内容
- Throws:
InternalException- IO异常
-
read
从FileChannel中读取内容- Parameters:
fileChannel- 文件管道charset- 字符集- Returns:
- 内容
- Throws:
InternalException- IO异常
-
read
从流中读取内容,读到输出流中- Parameters:
channel- 可读通道,读取完毕后并不关闭通道- Returns:
- 输出流
- Throws:
InternalException- IO异常
-
read
从流中读取内容,读取完毕后并不关闭流- Parameters:
channel- 可读通道,读取完毕后并不关闭通道charset- 字符集- Returns:
- 内容
- Throws:
InternalException- IO异常
-
copy
拷贝文件Channel,使用NIO,拷贝后不会关闭channel- Parameters:
in-FileChannel,非空out-FileChannel,非空- Returns:
- 拷贝的字节数
- Throws:
InternalException- IO异常
-
copy
拷贝流,使用NIO,不会关闭channel- Parameters:
in-ReadableByteChannelout-WritableByteChannel- Returns:
- 拷贝的字节数
- Throws:
InternalException- IO异常
-
copy
public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize) throws InternalException 拷贝流,使用NIO,不会关闭channel- Parameters:
in-ReadableByteChannelout-WritableByteChannelbufferSize- 缓冲大小,如果小于等于0,使用默认- Returns:
- 拷贝的字节数
- Throws:
InternalException- IO异常
-
copy
public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, StreamProgress streamProgress) throws InternalException 拷贝流,使用NIO,不会关闭channel- Parameters:
in-ReadableByteChannelout-WritableByteChannelbufferSize- 缓冲大小,如果小于等于0,使用默认streamProgress-StreamProgress进度处理器- Returns:
- 拷贝的字节数
- Throws:
InternalException- IO异常
-
copy
public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, long totalCount, StreamProgress streamProgress) 拷贝流,使用NIO,不会关闭channel- Parameters:
in-ReadableByteChannelout-WritableByteChannelbufferSize- 缓冲大小,如果小于等于0,使用默认totalCount- 读取总长度streamProgress-StreamProgress进度处理器- Returns:
- 拷贝的字节数
-
copyNio
public static long copyNio(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws InternalException 拷贝流 thanks to: https://github.com/venusdrogon/feilong-io/blob/master/src/main/java/com/feilong/io/IOWriteUtil.java 本方法不会关闭流- Parameters:
in- 输入流out- 输出流bufferSize- 缓存大小streamProgress- 进度条- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
copyNio
public static long copyNio(InputStream in, OutputStream out, int bufferSize, long count, StreamProgress streamProgress) throws InternalException 拷贝流 本方法不会关闭流- Parameters:
in- 输入流, 非空out- 输出流, 非空bufferSize- 缓存大小,-1表示默认count- 最大长度,-1表示无限制streamProgress- 进度条,null表示无进度条- Returns:
- 传输的byte数
- Throws:
InternalException- IO异常
-
flush
从缓存中刷出数据- Parameters:
flushable-Flushable
-
close
尝试关闭指定对象 判断对象如果实现了AutoCloseable,则调用之- Parameters:
obj- 可关闭对象
-
close
关闭 关闭失败不会抛出异常- Parameters:
closeable- 被关闭的对象
-
close
关闭 关闭失败不会抛出异常- Parameters:
autoCloseable- 被关闭的对象
-
close
关闭 关闭失败不会抛出异常- Parameters:
socket- 被关闭的对象
-
close
- Parameters:
serverSocket- 被关闭的对象 关闭serverSocket,忽略任何已检查的异常。 如果serverSocket为空,则不执行任何操作
-
close
- Parameters:
channel- 需要被关闭的通道
-
closeQuietly
按照给定顺序连续关闭一系列对象 这些对象必须按照顺序关闭,否则会出错。- Parameters:
closeables- 需要关闭的对象
-
isAndroidGetsocknameError
Returns true ifeis due to a firmware bug fixed after Android 4.2.2. https://code.google.com/p/android/issues/detail?id=54072 -
nullSafeClose
关闭 关闭失败抛出IOException异常- Parameters:
closeable- 被关闭的对象- Throws:
IOException- IO异常
-
sink
返回一个向out写入的接收器- Parameters:
out- 输出流- Returns:
- 接收缓冲区
-
sink
返回一个向socket写入的接收器。优先选择这个方法, 而不是sink(OutputStream),因为这个方法支持超时 当套接字写超时时,套接字将由任务线程异步关闭- Parameters:
socket- 套接字- Returns:
- 接收器
- Throws:
IOException- IO异常
-
source
返回从in中读取的缓冲数据- Parameters:
in- 数据输入流- Returns:
- 缓冲数据
-
source
返回从file读取的缓冲数据- Parameters:
file- 文件- Returns:
- 缓冲数据
- Throws:
FileNotFoundException- 文件未找到
-
source
返回从path读取的缓冲数据- Parameters:
path- 路径options- 选项- Returns:
- 缓冲数据
- Throws:
IOException- IO异常
-
sink
返回一个向file写入的接收器- Parameters:
file- 文件- Returns:
- 接收器
- Throws:
FileNotFoundException- 文件未找到
-
appendingSink
返回一个附加到file的接收器- Parameters:
file- 文件- Returns:
- 接收器
- Throws:
FileNotFoundException- 文件未找到
-
sink
返回一个向path写入的接收器.- Parameters:
path- 路径options- 属性- Returns:
- 写入的数据的接收器
- Throws:
IOException- IO异常
-
blackhole
返回一个都不写的接收器- Returns:
- 接收器
-
source
返回从socket读取的缓存信息。与source(InputStream)相比, 更喜欢这个方法, 因为这个方法支持超时。当套接字读取超时时,套接字将由任务线程异步关闭- Parameters:
socket- 套接字- Returns:
- 取的缓存信息
- Throws:
IOException- IO异常
-
contentEquals
public static boolean contentEquals(InputStream input1, InputStream input2) throws InternalException 对比两个流内容是否相同 内部会转换流为BufferedInputStream- Parameters:
input1- 第一个流input2- 第二个流- Returns:
- 两个流的内容一致返回true,否则false
- Throws:
InternalException- IO异常
-
contentEquals
对比两个Reader的内容是否一致 内部会转换流为BufferedInputStream- Parameters:
input1- 第一个readerinput2- 第二个reader- Returns:
- 两个流的内容一致返回true,否则false
- Throws:
InternalException- IO异常
-
contentEqualsIgnoreEOL
对比两个流内容是否相同,忽略EOL字符 内部会转换流为BufferedInputStream- Parameters:
input1- 第一个流input2- 第二个流- Returns:
- 两个流的内容一致返回true,否则false
- Throws:
InternalException- IO异常
-
lineIter
返回行遍历器LineIterator it = null; try { it = IoKit.lineIter(reader); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }- Parameters:
reader-Reader- Returns:
LineIterator
-
lineIter
返回行遍历器LineIterator it = null; try { it = IoKit.lineIter(in, Charset.CHARSET_UTF_8); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }- Parameters:
in-InputStreamcharset- 编码- Returns:
LineIterator
-
toString
ByteArrayOutputStream转换为String- Parameters:
out-ByteArrayOutputStreamcharset- 编码- Returns:
- 字符串
-
checkOffsetAndCount
public static void checkOffsetAndCount(long size, long offset, long byteCount) -
sneakyRethrow
即使被声明也不允许直接抛出 这是一种很糟糕的做饭,很容易遭到攻击 清理后捕获并重新抛出异常- Parameters:
t- 异常
-
arrayRangeEquals
public static boolean arrayRangeEquals(byte[] a, int aOffset, byte[] b, int bOffset, int byteCount) -
buffer
返回缓冲区从source读取的字节流 返回的源将对其内存缓冲区执行批量读取- Parameters:
source- 字节流- Returns:
- 返回缓冲区
-
buffer
返回一个新接收器,该接收器缓冲写sink返回的接收器将批量写入sink- Parameters:
sink- 接收一个字节流- Returns:
- 接收缓冲区
-
reverseBytesShort
public static short reverseBytesShort(short s) -
reverseBytesInt
public static int reverseBytesInt(int i) -
reverseBytesLong
public static long reverseBytesLong(long v) -
openFileOrURL
- Throws:
IOException
-