public class FileChannelCopier extends IoCopier<FileChannel,FileChannel>
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());
}
bufferSize, count, flushEveryBuffer, progress
Constructor and Description |
---|
FileChannelCopier(long count)
构造
|
Modifier and Type | Method and Description |
---|---|
long |
copy(FileChannel source,
FileChannel target)
执行拷贝
|
long |
copy(FileInputStream in,
FileOutputStream out)
拷贝文件流,使用NIO
|
static FileChannelCopier |
of()
创建
FileChannel 拷贝器 |
static FileChannelCopier |
of(long count)
创建
FileChannel 拷贝器 |
bufferSize, setFlushEveryBuffer
public FileChannelCopier(long count)
count
- 拷贝总数,-1表示无限制public static FileChannelCopier of()
FileChannel
拷贝器public static FileChannelCopier of(long count)
FileChannel
拷贝器count
- 拷贝总数,-1表示无限制public long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException
in
- 输入out
- 输出IORuntimeException
- IO异常public long copy(FileChannel source, FileChannel target)
IoCopier
copy
in class IoCopier<FileChannel,FileChannel>
source
- 拷贝源,如InputStream、Reader等target
- 拷贝目标,如OutputStream、Writer等Copyright © 2025. All rights reserved.