content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.core.io.copier;
  
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.nio.channels.FileChannel;
  
  import org.miaixz.bus.core.lang.exception.InternalException;
  import org.miaixz.bus.core.xyz.IoKit;
  
  /**
   * {@link FileChannel} 数据拷贝封装
   *
   * <pre>{@code
   * 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());
   *  }
   * }</pre>
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  public class FileChannelCopier extends IoCopier<FileChannel, FileChannel> {
  
      /**
       * 构造
       *
       * @param count 拷贝总数，-1表示无限制
       */
      public FileChannelCopier(final long count) {
          super(-1, count, null);
      }
  
      /**
       * 创建{@link FileChannel} 拷贝器
       *
       * @return FileChannelCopier
       */
      public static FileChannelCopier of() {
          return of(-1);
      }
  
      /**
       * 创建{@link FileChannel} 拷贝器
       *
       * @param count 拷贝总数，-1表示无限制
       * @return FileChannelCopier
       */
      public static FileChannelCopier of(final long count) {
          return new FileChannelCopier(count);
      }
  
      /**
       * 拷贝文件流，使用NIO
       *
       * @param in  输入
       * @param out 输出
       * @return 拷贝的字节数
       * @throws InternalException IO异常
       */
      public long copy(final FileInputStream in, final FileOutputStream out) throws InternalException {
          FileChannel inChannel = null;
          FileChannel outChannel = null;
          try {
              inChannel = in.getChannel();
              outChannel = out.getChannel();
              return copy(inChannel, outChannel);
          } finally {
              IoKit.closeQuietly(outChannel);
              IoKit.closeQuietly(inChannel);
          }
      }
  
      @Override
      public long copy(final FileChannel source, final FileChannel target) {
          try {
              return doCopySafely(source, target);
          } catch (final IOException e) {
              throw new InternalException(e);
          }
      }
  
      /**
       * 文件拷贝实现
       *
       * <pre>
       * 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());
       *  }
       * </pre>
       *
       * @param inChannel  输入通道
       * @param outChannel 输出通道
       * @return 输入通道的字节数
       * @throws IOException 发生IO错误
       * @link http://androidxref.com/6.0.1_r10/xref/libcore/luni/src/main/java/java/nio/FileChannelImpl.java
       * @link http://androidxref.com/7.0.0_r1/xref/libcore/ojluni/src/main/java/sun/nio/ch/FileChannelImpl.java
       * @link http://androidxref.com/7.0.0_r1/xref/libcore/ojluni/src/main/native/FileChannelImpl.c
       */
      private long doCopySafely(final FileChannel inChannel, final FileChannel outChannel) throws IOException {
          long totalBytes = inChannel.size();
          if (this.count > 0 && this.count < totalBytes) {
              // 限制拷贝总数
              totalBytes = count;
          }
          for (long pos = 0, remaining = totalBytes; remaining > 0;) { // 确保文件内容不会缺失
              final long writeBytes = inChannel.transferTo(pos, remaining, outChannel); // 实际传输的字节数
              pos += writeBytes;
              remaining -= writeBytes;
          }
          return totalBytes;
      }
  
  }
  