1

transferFromを使用して、NIOを使用していくつかの小さなファイルからファイルを組み立てようとしています。

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
4

1 に答える 1

2

最も明白な答えは次の 2 つです。

  1. tmpT5 が 0 バイトのファイルを指している、または
  2. path が指すファイルの長さが 4096 バイト未満であること。

transferFromドキュメントから:

指定された位置がファイルの現在のサイズより大きい場合、バイトは転送されません。

于 2010-01-23T20:20:52.413 に答える