Class FileChannelCopier

java.lang.Object
org.miaixz.bus.core.io.copier.IoCopier<FileChannel,FileChannel>
org.miaixz.bus.core.io.copier.FileChannelCopier

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());
  }
 
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • FileChannelCopier

      public FileChannelCopier(long count)
      构造
      Parameters:
      count - 拷贝总数,-1表示无限制
  • Method Details