简体   繁体   中英

working with byte array data in node.js and how to process it

I need the entire byte array data fetched from a socket and after that I need to insert it into a database a BLOB. Do not ask me why I am not formatting the data from byte array, because I need to work with that structure.

I am storing the byte array data into a js array. I tried to store it within a buffer object but I get errors when I try to write the byte array data into the buffer because it can convert it.

My question is how is the easiest way to work with byte array in js. I have the following code:

var buff =[];
sock.on('data', function(data) {
    buff.push(data);
})

sock.on('end', function() {
    console.log(data) // [<Byte Array>,<Byte Array>, ...]
});

Basically I want to insert my data as [] not as [,, ...]. What is the best solution for my problem?

Depending on your database interface you might be able to stream each element of your JS array as a separate chunk.

[Update] It looks like node.js now provides a Buffer.concat(...) method to concatenate a bunch of buffers into a single one (basically replacing the "buffertools" library I mention below).

var buffers = [];
sock.on('data', function(data) {
  buffers.push(data);
}).on('end', function() {
  var bytes = Buffer.concat(buffers); // One big byte array here.
  // ...
});

[Original] Alternatively, you could use the buffertools module to concatenate all of the chunks into a single buffer. For example:

var buffertools = require('buffertools');
var buff = new Buffer();
sock.on('data', function(data) {
  buff = buff.concat(data);
}).on('end', function() {
  console.log(buff); // <Byte Array>
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM