nio FileChannel.transferFrom转移0?

时间:2010-01-23 20:02:10

标签: java nio

我正在尝试使用NIO使用transferFrom从几个较小的文件中组装文件。

对transferFrom的调用返回0.没有异常。没有办法打开同步行为。

    FileOutputStream fos = new FileOutputStream(path);
    FileChannel fileBeingAssembled = fos.channel();
    int progressiveOffset = 4096;
    FileInputStream fis = new FileInputStream(tmpT5);
    FileChannel channel = fis.getChannel();
    channel.position(0);
    int thisItemLength = (int)channel.size();
    LOG.info("Writing " + tag + " at " + progressiveOffset + " length " + thisItemLength);
    fileBeingAssembled.position(progressiveOffset);
    long x = fileBeingAssembled.transferFrom(channel, progressiveOffset, thisItemLength);
    LOG.info("transferred " + x);
    progressiveOffset += thisItemLength;

示例日志:

4409 [main] INFO  com.basistech.seg.writing.ModelOutputTask  - available 1856216
4409 [main] INFO  com.basistech.seg.writing.ModelOutputTask  - Writing word at 15024620 length 1856216
4419 [main] INFO  com.basistech.seg.writing.ModelOutputTask  - transferred 0

1 个答案:

答案 0 :(得分:2)

两个最明显的答案是:

  1. tmpT5指向零字节文件,或
  2. path指向的文件长度小于4096字节。
  3. 来自transferFrom文档:

      

    如果给定位置大于文件的当前大小,则不传输任何字节。