Node.js convert buffer to Int8Array
Asked Answered
T

2

6

in the client side, i am converting a TypedArray into Blob and transmitting to server, to check if data is correct, i want to compare the first five values on client side and server side,

on client side:

var fileReader = new FileReader();
fileReader.onload = function() {
    callback(new Int8Array(this.result));
};
fileReader.readAsArrayBuffer(blob);

(from which i read first five values in callback fn)

but on server, I found code to convert blob to buffer, and from my understanding, buffer and arraybuffer are not the same, var buffer1 = new Buffer( blob, 'binary' );

does buffer have something similar to DataView of arraybuffer, now how do I read the first 5 values of buffer1 as integers which I am able to do in client side?

Tical answered 7/10, 2014 at 7:6 Comment(0)
L
9

Node Buffer is currently entirely based on Uint8Array. Everything you can do with a Uint8Array instance you can do with a Buffer instance. And even more, because Buffer adds additional functions and properties. Internally, when a Buffer instance has to be created, they actually create an Uint8Array instance and then set its prototype to the Node Buffer prototype. So you can access the underlying ArrayBuffer with buffer1.buffer, etc.

Lynn answered 7/2, 2017 at 11:17 Comment(3)
upvoted, but would be great if you expand on the answer with some example code :)Tical
Thanks for the upvote. In fact I'm not a js developer. I'm a C++ developer who writes C++ extension modules. I know something about V8, libuv and the nodejs C++ part of the code. I looked at node buffer internal implementation and wanted to help by sharing what I saw there. But I think the js part would be easier knowing that info...Lynn
then a link for others to inspect that code would be nice. I suppose this suffices: github.com/nodejs/node/blob/v17.7.1/lib/buffer.js#L344Marek
H
12

To answer the original question, you can use the buffer property of a Buffer instance to get the ArrayBuffer, then instantiate a Uint8Array based on that.

function viewBufferAsUint8Array(buf) {
    return new Uint8Array(buf.buffer);
}

NOTE: The Uint8Array and ArrayBuffer will occupy the same memory, so changes to one will result in changes to the other. The code below will copy the contents into a new Uint8Array that is not linked to the buffer:

function copyBufferToSeparateUint8Array(buf, truncateSizeTo) {
    var orig = new Uint8Array( buf.buffer );
    if (!truncateSizeTo) return new Uint8Array( orig );
    
    var copy = new Uint8Array( truncateSizeTo );
    copy.set( orig );
    return copy;
}

Although copying the buffer into a separate Uint8Array is a little slower, it has the added benefit of allowing one to choose the size of the new Uint8Array.

Example usage:

var buf = Buffer.alloc(4);
buf[0] = buf[1] = buf[2] = buf[3] = 45;

var asU8Array = viewBufferAsUint8Array( buf );

var copiedU8 = copyBufferToSeparateUint8Array( buf );

asU8Array[2] = 120;

console.log( buf[2] );       // 120
console.log( asU8Array[2] ); // 120
console.log( copiedU8[2] );  // 45
Hayner answered 8/6, 2020 at 19:56 Comment(0)
L
9

Node Buffer is currently entirely based on Uint8Array. Everything you can do with a Uint8Array instance you can do with a Buffer instance. And even more, because Buffer adds additional functions and properties. Internally, when a Buffer instance has to be created, they actually create an Uint8Array instance and then set its prototype to the Node Buffer prototype. So you can access the underlying ArrayBuffer with buffer1.buffer, etc.

Lynn answered 7/2, 2017 at 11:17 Comment(3)
upvoted, but would be great if you expand on the answer with some example code :)Tical
Thanks for the upvote. In fact I'm not a js developer. I'm a C++ developer who writes C++ extension modules. I know something about V8, libuv and the nodejs C++ part of the code. I looked at node buffer internal implementation and wanted to help by sharing what I saw there. But I think the js part would be easier knowing that info...Lynn
then a link for others to inspect that code would be nice. I suppose this suffices: github.com/nodejs/node/blob/v17.7.1/lib/buffer.js#L344Marek

© 2022 - 2024 — McMap. All rights reserved.