如何限制从可读流(ReadableStream)中每次读取的数据大小?

4

我正在使用HTML文件输入从我的设备存储访问文件,然后将该文件作为流读取。

  • 是否有内置的方法来限制每个读取的长度到特定字节数。
const stream = myFile.stream() 
const reader = stream.getReader();

//actually I use this each time I want to read more data from the stream
reader.read().then(function({ done, value }) { 
  
    const bufferSizeInBytes = value.length //  I want to limit this to 1000 bytes as max value
   
    })

另一个困扰我的问题是,为什么我们每次读取都会得到不同大小的缓冲区,这是由可用内存或CPU决定的吗?如果它取决于内存,我们应该能够在一次读取中读取整个流,因为该文件大小约为100MB,而我的可用内存约为6GB,但实际上需要多次读取,这让我认为内存并不是此操作背后的唯一因素。
非常感谢您的任何帮助。
2个回答

2

目前无法控制默认文件流的读取块大小,您可以尝试将其转换为ByteStream,然后使用stream.getReader({ mode: 'byob' })获取BYOB reader以控制读取大小限制。

更多信息请参见:https://web.dev/streams/


1
一种方法是创建一个中间的ReadableStream,它将有一个缓冲区,一旦缓冲区超过所需的chunkSize,则入队(或如果我们处于最后一部分,那么只剩下一个小于chunkSize的部分)。像这样:
  const reader = readable.getReader();
  const chunkSize = 1 * 1024 * 1024 // 1MB

  let buffer: Uint8Array;

  const readableWithDefinedChunks = new ReadableStream({
    async pull(controller) {
      let fulfilledChunkQuota = false;

      while (!fulfilledChunkQuota) {
        const status = await reader.read();

        if (!status.done) {
          const chunk = status.value;
          buffer = new Uint8Array([...(buffer || []), ...chunk]);

          while (buffer.byteLength >= chunkSize) {
            const chunkToSend = buffer.slice(0, chunkSize);
            controller.enqueue(chunkToSend);
            buffer = new Uint8Array([...buffer.slice(chunkSize)]);
            fulfilledChunkQuota = true;
          }
        }
        if (status.done) {
          fulfilledChunkQuota = true;
          if (buffer.byteLength > 0) {
            controller.enqueue(buffer);
          }
          controller.close();
        }
      }
    },
  });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接