Reputation: 137
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?
Upvotes: 2
Views: 9626
Reputation: 156434
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>
});
Upvotes: 5