I'm trying to manipulate binary data with ArrayBuffer. If original ArrayBuffer looks like this:
var buffer = new ArrayBuffer(40);
var dv = new DataView(buffer);
var num = 0;
for(var i = 0; i < 40; i+=2) {
dv.setUint16(i, num++);
}
Then ArrayBuffer has 20 length of Uint16Array. I want to prepend extra binary datas, not append. Let's say that I want to prepend 5 bytes of Uint8 like this:
ArrayBuffer[0] = 10;
ArrayBuffer[1] = 20;
ArrayBuffer[2] = 30;
ArrayBuffer[3] = 40;
ArrayBuffer[4] = 50;
ArrayBuffer[5 ~ 25] = Original Data
As I know, there is no prepending like method in ArrayBuffer, so I did it myself. This wasn't matter, but considering endianness was really made me crazy.
First 5 bytes of data was produced by me, so I could set the endianness manually, but the original data is coming from external, so data could be Little Endian and Big Endian both.
I'm making somekind of binary data module for Node.js and Web Browser both can be use, and basic usage is prepending extra binary data from Web Browser by using this module, and browser sent this to server, then server reads this binary data from this module too, and split to prepended and orignal.
But the problem is, if the browser, I mean client computer using little-endian, and server using big-endian, original data might not be read correctly, because they have different endianness.
I'm appending original data to new binary data like this. New binary data has different 5 bytes of data at 0 ~ 5 bytes. So I have to write after 5 bytes offset.
// write rest of data
var newBuffer = new ArrayBuffer(5 + origBuffer.byteLength);
var ndv = new DataView(newBuffer);
var dv = new DataView(origBuffer);
for(var i = 0; i < origBuffer.byteLength; i++) {
var offset = 5 + i;
ndv.setUint8(offset, dv.getUint8(i));
}
Testing on my local machine is fine, because server and client both using same cpu, but in reallife, if they has different endianness, this module will not work correctly.
Is there a endianness safe way to copying binary data with ArrayBuffer & DataView? Or should I just forget about the endianness? Any advice will very appreciate it.