Package com.luna.common.io
Class NioUtil
java.lang.Object
com.luna.common.io.NioUtil
- Direct Known Subclasses:
IoUtil
NIO相关工具封装,主要针对Channel读写、拷贝等封装
- Since:
- 5.5.3
- Author:
- looly
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
默认缓存大小 8192static final int
默认大缓存大小 32768static final int
默认中等缓存大小 16384static final int
数据流末尾 -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
close
(AutoCloseable closeable) 关闭
关闭失败不会抛出异常static long
copy
(FileChannel inChannel, FileChannel outChannel) 拷贝文件Channel,使用NIO,拷贝后不会关闭channelstatic long
copy
(ReadableByteChannel in, WritableByteChannel out) 拷贝流,使用NIO,不会关闭channelstatic long
copy
(ReadableByteChannel in, WritableByteChannel out, int bufferSize) 拷贝流,使用NIO,不会关闭channelstatic long
copy
(ReadableByteChannel in, WritableByteChannel out, int bufferSize, StreamProgress streamProgress) 拷贝流,使用NIO,不会关闭channelstatic long
copyByNIO
(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 String
read
(FileChannel fileChannel, String charsetName) 从FileChannel中读取内容,读取完毕后并不关闭Channelstatic String
read
(FileChannel fileChannel, Charset charset) 从FileChannel中读取内容static FastByteArrayOutputStream
read
(ReadableByteChannel channel) 从流中读取内容,读到输出流中static String
read
(ReadableByteChannel channel, Charset charset) 从流中读取内容,读取完毕后并不关闭流static String
readUtf8
(FileChannel fileChannel) 从FileChannel中读取UTF-8编码内容
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE默认缓存大小 8192- See Also:
-
DEFAULT_MIDDLE_BUFFER_SIZE
public static final int DEFAULT_MIDDLE_BUFFER_SIZE默认中等缓存大小 16384- See Also:
-
DEFAULT_LARGE_BUFFER_SIZE
public static final int DEFAULT_LARGE_BUFFER_SIZE默认大缓存大小 32768- See Also:
-
EOF
public static final int EOF数据流末尾- See Also:
-
-
Constructor Details
-
NioUtil
public NioUtil()
-
-
Method Details
-
copyByNIO
public static long copyByNIO(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws IOException 拷贝流 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:
IOException
- IO异常
-
copy
拷贝文件Channel,使用NIO,拷贝后不会关闭channel- Parameters:
inChannel
-FileChannel
outChannel
-FileChannel
- Returns:
- 拷贝的字节数
- Throws:
IOException
- IO异常- Since:
- 5.5.3
-
copy
拷贝流,使用NIO,不会关闭channel- Parameters:
in
-ReadableByteChannel
out
-WritableByteChannel
- Returns:
- 拷贝的字节数
- Throws:
IOException
- IO异常- Since:
- 4.5.0
-
copy
public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize) throws IOException 拷贝流,使用NIO,不会关闭channel- Parameters:
in
-ReadableByteChannel
out
-WritableByteChannel
bufferSize
- 缓冲大小,如果小于等于0,使用默认- Returns:
- 拷贝的字节数
- Throws:
IOException
- IO异常- Since:
- 4.5.0
-
copy
public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, StreamProgress streamProgress) throws IOException 拷贝流,使用NIO,不会关闭channel- Parameters:
in
-ReadableByteChannel
out
-WritableByteChannel
bufferSize
- 缓冲大小,如果小于等于0,使用默认streamProgress
-StreamProgress
进度处理器- Returns:
- 拷贝的字节数
- Throws:
IOException
- IO异常
-
read
从流中读取内容,读取完毕后并不关闭流- Parameters:
channel
- 可读通道,读取完毕后并不关闭通道charset
- 字符集- Returns:
- 内容
- Throws:
IOException
- IO异常- Since:
- 4.5.0
-
read
从流中读取内容,读到输出流中- Parameters:
channel
- 可读通道,读取完毕后并不关闭通道- Returns:
- 输出流
- Throws:
IOException
- IO异常
-
readUtf8
从FileChannel中读取UTF-8编码内容- Parameters:
fileChannel
- 文件管道- Returns:
- 内容
- Throws:
IOException
- IO异常
-
read
从FileChannel中读取内容,读取完毕后并不关闭Channel- Parameters:
fileChannel
- 文件管道charsetName
- 字符集- Returns:
- 内容
- Throws:
IOException
- IO异常
-
read
从FileChannel中读取内容- Parameters:
fileChannel
- 文件管道charset
- 字符集- Returns:
- 内容
- Throws:
IOException
- IO异常
-
close
关闭
关闭失败不会抛出异常- Parameters:
closeable
- 被关闭的对象
-