javascript 流和缓冲区互相转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 流和缓冲区互相转换相关的知识,希望对你有一定的参考价值。

function streamToBuffer(stream) {
  return new Promise((resolve, reject) => {
    const buffers = [];
    stream.on('error', reject);
    stream.on('data', data => buffers.push(data));
    stream.on('end', () => resolve(Buffer.from(buffers)));
  });
}

function streamToArray(stream) {
  return new Promise((resolve, reject) => {
    const buffers = [];
    stream.on('error', reject);
    stream.on('data', data => buffers.push(data));
    stream.on('end', () => resolve(Buffer.concat(buffers)));
  });
}





let Duplex = require('stream').Duplex;  
function bufferToStream(buffer) {  
  let stream = new Duplex();
  stream.push(buffer);
  stream.push(null);
  return stream;
}

let Readable = require('stream').Readable ; 
function bufferToStream(buffer) { 
  let stream = new Readable ();
  stream.push(buffer);
  stream.push(null);
  return stream;
}
  
  
  

以上是关于javascript 流和缓冲区互相转换的主要内容,如果未能解决你的问题,请参考以下文章

Java中InputStream和Reader之间的转换(字符流和字节流怎么互相转换啊)

转换流和缓冲流

Java IO-3 转换流和缓冲流

JavaSE——转换流和缓冲流

JAVA SE基础篇51装饰流之缓冲流和转换流

Qt--文本流和数据流缓冲操作和数据操作