Javascript 如何在javascript中将字节、多字节和缓冲区附加到ArrayBuffer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33702838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append bytes, multi-bytes and buffer to ArrayBuffer in javascript?
提问by codneto
Javascript ArrayBuffer or TypedArrays dont have any kind of appendByte(), appendBytes(), or appendBuffer() methods. So if I want to fill an ArrayBuffer one value at a time, how do I do it?
Javascript ArrayBuffer 或 TypedArrays 没有任何类型的 appendByte()、appendBytes() 或 appendBuffer() 方法。所以如果我想一次填充一个 ArrayBuffer 一个值,我该怎么做?
var firstVal = 0xAB; // 1 byte
var secondVal = 0x3D7F // 2 bytes
var anotherUint8Array = someArr;
var buffer = new ArrayBuffer(); // I don't know the length yet
var bufferArr = new UInt8Array(buffer);
// following methods do not exist. What are the alternatives for each??
bufferArr.appendByte(firstVal);
bufferArr.appendBytes(secondVal);
bufferArr.appendBuffer(anotherUint8Array);
回答by Paul S.
You can create a new TypedArraywith a new ArrayBuffer, but you can't change the size of an existing buffer
您可以使用新的ArrayBuffer创建新的TypedArray,但不能更改现有缓冲区的大小
function concatTypedArrays(a, b) { // a, b TypedArray of same type
var c = new (a.constructor)(a.length + b.length);
c.set(a, 0);
c.set(b, a.length);
return c;
}
Now can do
现在可以做
var a = new Uint8Array(2),
b = new Uint8Array(3);
a[0] = 1; a[1] = 2;
b[0] = 3; b[1] = 4;
concatTypedArrays(a, b); // [1, 2, 3, 4, 0] Uint8Array length 5
If you want to use different types, go via Uint8Arrayas the smallest unit is a byte, i.e.
如果要使用不同的类型,请通过,Uint8Array因为最小单位是byte,即
function concatBuffers(a, b) {
return concatTypedArrays(
new Uint8Array(a.buffer || a),
new Uint8Array(b.buffer || b)
).buffer;
}
This means .lengthwill work as expected, you could now convert this to your typed array of choice (make sure it's a type that would accept the .byteLengthof the buffer though)
这意味着.length将按预期工作,您现在可以将其转换为您选择的类型化数组(但请确保它是一种可以接受.byteLength缓冲区的类型)
From here, you could now implement any method you like for concatenating your data, e.g.
从这里开始,您现在可以实现您喜欢的任何连接数据的方法,例如
function concatBytes(ui8a, byte) {
var b = new Uint8Array(1);
b[0] = byte;
return concatTypedArrays(ui8a, b);
}
var u8 = new Uint8Array(0);
u8 = concatBytes(u8, 0x80); // [128]
回答by newguy
Paul's answer allows you to concatenate one TypedArray to an existing TypedArray. In ES6, you can use the following function to concatenate multiple TypedArrays:
Paul 的回答允许您将一个 TypedArray 连接到一个现有的 TypedArray。在 ES6 中,您可以使用以下函数连接多个 TypedArray:
function concatenate(resultConstructor, ...arrays) {
let totalLength = 0;
for (const arr of arrays) {
totalLength += arr.length;
}
const result = new resultConstructor(totalLength);
let offset = 0;
for (const arr of arrays) {
result.set(arr, offset);
offset += arr.length;
}
return result;
}
const ta = concatenate(Uint8Array,
Uint8Array.of(1, 2), Uint8Array.of(3, 4));
console.log(ta); // Uint8Array [1, 2, 3, 4]
console.log(ta.buffer.byteLength); // 4
To append a new byte is:
追加一个新字节是:
const byte = 3;
concatenate(Uint8Array, Uint8Array.of(1, 2), Uint8Array.of(byte));
This method is found in ExploringJS.
这个方法可以在ExploringJS 中找到。

