javascript - Deno file.read with file.write, size not matching - Stack Overflow

I am having some issue with write files in DenoThe resulting files are damangedThe code is very simpl

I am having some issue with write files in Deno

The resulting files are damanged

The code is very simple, it's just reads from a file and writes to another:

  let bytesRead: number | null = -1;
  while(bytesRead !== null){
    const buf = new Uint8Array(5 * 1_000_000);
    bytesRead = await infh.read(buf)
    if(bytesRead !== null){
      await outfh.write(buf);
    }
  }

I am having some issue with write files in Deno

The resulting files are damanged

The code is very simple, it's just reads from a file and writes to another:

  let bytesRead: number | null = -1;
  while(bytesRead !== null){
    const buf = new Uint8Array(5 * 1_000_000);
    bytesRead = await infh.read(buf)
    if(bytesRead !== null){
      await outfh.write(buf);
    }
  }
Share Improve this question asked Mar 3 at 17:24 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges 2
  • in what way are the resulting files "damaged"? assuming infh.read(buf) is this you may want to read what is returned by that method (I assume Deno returns the same result - can't be bothered reading Deno documentation) – Bravo Commented Mar 3 at 20:53
  • deno does not return the buf, it only returns the number of bytes read. For some reason the resulting file is larger than the input file.. – Alex Commented Mar 4 at 10:44
Add a comment  | 

1 Answer 1

Reset to default 1

The Deno docs make it clear — for both the read and write methods on an instance of Deno.FsFile:

It is not guaranteed that the full buffer will be read/written in a single call.


The idiomatic approach for copying data from one instance to another is to pipe the ReadableStream of the input file to the WritableStream of the output file:

// declare let sourceFile: Deno.FsFile;
// declare let destinationFile: Deno.FsFile;

await sourceFile.readable.pipeTo(destinationFile.writable);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745081121a4610130.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信