Jak dołączyć dane binarne do bufora w node.js

Mam bufor z niektórymi danymi binarnymi:

<code>var b = new Buffer ([0x00, 0x01, 0x02]);
</code>

i chcę dołączyć0x03.

Jak mogę dołączyć więcej danych binarnych? Szukam w dokumentacji, ale w celu dołączenia danych musi to być łańcuch, jeśli nie, wystąpi błąd (Błąd typu: Argument musi być łańcuchem):

<code>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
</code>

Jedynym rozwiązaniem, jakie tutaj widzę, jest utworzenie nowego bufora dla wszystkich dołączonych danych binarnych i skopiowanie go do głównego bufora z poprawnym przesunięciem:

<code>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>
</code>

Ale wydaje się to trochę nieefektywne, ponieważ muszę utworzyć instancję nowego bufora dla każdego dodatku.

Czy znasz lepszy sposób dołączania danych binarnych?

EDYTOWAĆ

Napisałem aBufferedWriter który zapisuje bajty do pliku za pomocą wewnętrznych buforów. Taki sam jakBufferedReader ale do pisania.

Szybki przykład:

<code>//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
</code>

OSTATNIA AKTUALIZACJA

Posługiwać siękonkat.

questionAnswers(3)

yourAnswerToTheQuestion