2

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?

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? 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 come from the stream...I want the entire byte array into 1 single chunk as I stated Commented Feb 5, 2013 at 9:00

1 Answer 1

5

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>
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.