开发者

nio FileChannel.transferFrom transferring 0?

开发者 https://www.devze.com 2022-12-18 09:12 出处:网络
I\'m trying to use NIO to assemble a file out of several smaller files, using transferFrom. The call to transferFrom returns 0. No exception. Nothing done to turn on synchronous behavior.

I'm trying to use NIO to assemble a file out of several smaller files, using transferFrom.

The call to transferFrom returns 0. No exception. Nothing done to turn on synchronous behavior.

    FileOutput开发者_运维知识库Stream 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;

An example log:

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


The two most obvious answers are:

  1. That tmpT5 points to a zero byte file, or
  2. That the file pointed to by path is less than 4096 bytes long.

From the transferFrom docs:

If the given position is greater than the file's current size then no bytes are transferred.

0

精彩评论

暂无评论...
验证码 换一张
取 消