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?
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?
Share Improve this question edited Feb 5, 2013 at 8:58 vsergiu asked Feb 4, 2013 at 20:20 vsergiuvsergiu 1371 silver badge11 bronze badges 2-
The syntax of your sample code is invalid (
buff.push[data]
) and your question is vague. Can you post the part of your code that attempts to store the data in the database and explain what you expect to happen and what happens instead? – maerics Commented Feb 4, 2013 at 23:14 - I fixed the typo...as I said I want to store the byte array into a buffer and after that dump it in the database...and I can not do it right now because I get an array of byte array chunks that e from the stream...I want the entire byte array into 1 single chunk as I stated – vsergiu Commented Feb 5, 2013 at 9:00
1 Answer
Reset to default 5Depending 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>
});