Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append binary data to a buffer in node.js

I have a buffer with some binary data:

var b = new Buffer ([0x00, 0x01, 0x02]); 

and I want to append 0x03.

How can I append more binary data? I'm searching in the documentation but for appending data it must be a string, if not, an error occurs (TypeError: Argument must be a string):

var b = new Buffer (256); b.write ("hola"); console.log (b.toString ("utf8", 0, 4)); //hola b.write (", adios", 4); console.log (b.toString ("utf8", 0, 11)); //hola, adios 

Then, the only solution I can see here is to create a new buffer for every appended binary data and copy it to the major buffer with the correct offset:

var b = new Buffer (4); //4 for having a nice printed buffer, but the size will be 16KB new Buffer ([0x00, 0x01, 0x02]).copy (b); console.log (b); //<Buffer 00 01 02 00> new Buffer ([0x03]).copy (b, 3); console.log (b); //<Buffer 00 01 02 03> 

But this seems a bit inefficient because I have to instantiate a new buffer for every append.

Do you know a better way for appending binary data?

EDIT

I've written a BufferedWriter that writes bytes to a file using internal buffers. Same as BufferedReader but for writing.

A quick example:

//The BufferedWriter truncates the file because append == false new BufferedWriter ("file")     .on ("error", function (error){         console.log (error);     })      //From the beginning of the file:     .write ([0x00, 0x01, 0x02], 0, 3) //Writes 0x00, 0x01, 0x02     .write (new Buffer ([0x03, 0x04]), 1, 1) //Writes 0x04     .write (0x05) //Writes 0x05     .close (); //Closes the writer. A flush is implicitly done.  //The BufferedWriter appends content to the end of the file because append == true new BufferedWriter ("file", true)     .on ("error", function (error){         console.log (error);     })      //From the end of the file:     .write (0xFF) //Writes 0xFF     .close (); //Closes the writer. A flush is implicitly done.  //The file contains: 0x00, 0x01, 0x02, 0x04, 0x05, 0xFF 

LAST UPDATE

Use concat.

like image 577
Gabriel Llamas Avatar asked Apr 27 '12 18:04

Gabriel Llamas


People also ask

How do I concatenate a buffer?

The Buffer. concat() method is used to concat all buffer objects in a given array into one buffer object. The return value of this method is also a buffer. If length of buffer is not provided then it is calculated from the Buffer instances in list.

Is buffer a binary data?

Buffers only deal with binary data, and it can not be resizable. Each integer in a buffer represents a byte.

How does the buffer store data in node JS?

The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.


2 Answers

Updated Answer for Node.js ~>0.8

Node is able to concatenate buffers on its own now.

var newBuffer = Buffer.concat([buffer1, buffer2]); 

Old Answer for Node.js ~0.6

I use a module to add a .concat function, among others:

https://github.com/coolaj86/node-bufferjs

I know it isn't a "pure" solution, but it works very well for my purposes.

like image 67
Brad Avatar answered Sep 22 '22 12:09

Brad


Buffers are always of fixed size, there is no built in way to resize them dynamically, so your approach of copying it to a larger Buffer is the only way.

However, to be more efficient, you could make the Buffer larger than the original contents, so it contains some "free" space where you can add data without reallocating the Buffer. That way you don't need to create a new Buffer and copy the contents on each append operation.

like image 43
stewe Avatar answered Sep 20 '22 12:09

stewe