Package org.miaixz.bus.core.io.copier
Class FileChannelCopier
java.lang.Object
org.miaixz.bus.core.io.copier.IoCopier<FileChannel,FileChannel>
org.miaixz.bus.core.io.copier.FileChannelCopier
FileChannel 数据拷贝封装
FileChannel#transferTo 或 FileChannel#transferFrom 的实现是平台相关的,需要确保低版本平台的兼容性
例如 android 7以下平台在使用 ZipInputStream 解压文件的过程中,
通过 FileChannel#transferFrom 传输到文件时,其返回值可能小于 totalBytes,不处理将导致文件内容缺失
// 错误写法,dstChannel.transferFrom 返回值小于 zipEntry.getSize(),导致解压后文件内容缺失
try (InputStream srcStream = zipFile.getInputStream(zipEntry);
ReadableByteChannel srcChannel = Channels.newChannel(srcStream);
FileOutputStream fos = new FileOutputStream(saveFile);
FileChannel dstChannel = fos.getChannel()) {
dstChannel.transferFrom(srcChannel, 0, zipEntry.getSize());
}
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields inherited from class org.miaixz.bus.core.io.copier.IoCopier
bufferSize, count, flushEveryBuffer, progress -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongcopy(FileInputStream in, FileOutputStream out) 拷贝文件流,使用NIOlongcopy(FileChannel source, FileChannel target) 执行拷贝static FileChannelCopierof()创建FileChannel拷贝器static FileChannelCopierof(long count) 创建FileChannel拷贝器Methods inherited from class org.miaixz.bus.core.io.copier.IoCopier
bufferSize, setFlushEveryBuffer
-
Constructor Details
-
FileChannelCopier
public FileChannelCopier(long count) 构造- Parameters:
count- 拷贝总数,-1表示无限制
-
-
Method Details
-
of
创建FileChannel拷贝器- Returns:
- FileChannelCopier
-
of
创建FileChannel拷贝器- Parameters:
count- 拷贝总数,-1表示无限制- Returns:
- FileChannelCopier
-
copy
拷贝文件流,使用NIO- Parameters:
in- 输入out- 输出- Returns:
- 拷贝的字节数
- Throws:
InternalException- IO异常
-
copy
Description copied from class:IoCopier执行拷贝- Specified by:
copyin classIoCopier<FileChannel,FileChannel> - Parameters:
source- 拷贝源,如InputStream、Reader等target- 拷贝目标,如OutputStream、Writer等- Returns:
- 拷贝的实际长度
-