如果我想创建一个数组缓冲区,我会写:var buff = new ArrayBuffer(size)
但是如何调整现有缓冲区的大小呢?我的意思是,在缓冲区的末尾添加更多字节。
如果我想创建一个数组缓冲区,我会写:var buff = new ArrayBuffer(size)
但是如何调整现有缓冲区的大小呢?我的意思是,在缓冲区的末尾添加更多字节。
ArrayBuffer 本身没有设置。虽然set
在 TypedArray 上有。像这样使用:
var oldBuffer = new ArrayBuffer(20);
var newBuffer = new ArrayBuffer(40);
new Uint8Array(newBuffer).set(oldBuffer);
这样做的方法是ArrayBuffer.transfer(oldBuffer, newByteLength)
,像这样:
var buffA = new ArrayBuffer(30);
var buffB = ArrayBuffer.transfer(buffA, 40);
// buffA is now an empty array buffer (`buffA.byteLength === 0`)
// whereas buffB is a new buffer that starts with a's contents
// and ends with 10 bytes that are 0s.
// Much faster than manually copying. You could even just do:
var buffA = new ArrayBuffer(30);
buffA = ArrayBuffer.transfer(buffA, 40);
// Or as a prototype method
ArrayBuffer.prototype.resize = function(newByteLength) {
return ArrayBuffer.transfer(this, newByteLength);
}
var buffA = new ArrayBuffer(30);
buffA = buffA.resize(40);
但是(截至 2017 年 10 月)浏览器支持为 0%(甚至不支持 Node.js),甚至还没有起草。
在此可用之前,您可以使用页面上给出的 polyfill,它一次复制 8 个字节,因此对于大型数组来说相对较快(尽管它不会清空给定的数组,这是不可能的)。
这是使用TypeArray.prototype.set
而不是 for 循环的修改版本:
if (!ArrayBuffer.transfer) {
ArrayBuffer.transfer = function(oldBuffer, newByteLength) {
var srcBuffer = Object(oldBuffer);
var destBuffer = new ArrayBuffer(newByteLength);
if (!(srcBuffer instanceof ArrayBuffer) || !(destBuffer instanceof ArrayBuffer)) {
throw new TypeError('Source and destination must be ArrayBuffer instances');
}
var copylen = Math.min(srcBuffer.byteLength, destBuffer.byteLength);
/* Copy 8 bytes at a time */
var length = Math.trunc(copylen / 64);
(new Float64Array(destBuffer, 0, length))
.set(new Float64Array(srcBuffer, 0, length));
/* Copy the remaining 0 to 7 bytes, 1 byte at a time */
var offset = length * 64;
length = copylen - offset;
(new Uint8Array(srcBuffer, offset, length))
.set(new Uint8Array(destBuffer, offset, length));
return destBuffer;
};
}
[编辑]
上面的替代版本,不使用 ArrayBuffer 命名空间,并采用稍微不同的方法
function arrayBufferTransfer(oldBuffer, newByteLength) {
const
srcArray = new Uint8Array(oldBuffer),
destArray = new Uint8Array(newByteLength),
copylen = Math.min(srcArray.buffer.byteLength, destArray.buffer.byteLength),
floatArrayLength = Math.trunc(copylen / 8),
floatArraySource = new Float64Array(srcArray.buffer,0,floatArrayLength),
floarArrayDest = new Float64Array(destArray.buffer,0,floatArrayLength);
floarArrayDest.set(floatArraySource);
let bytesCopied = floatArrayLength * 8;
// slowpoke copy up to 7 bytes.
while (bytesCopied < copylen ) {
destArray[bytesCopied]=srcArray[bytesCopied];
bytesCopied++;
}
return destArray.buffer;
}
您正在寻找的是“设置”方法。创建一个更大的新数组后,只需调用 set 函数并复制旧内容即可。
var buff = new ArrayBuffer(32);
buff[31] = 43;
var newBuff = new ArrayBuffer(buff.byteLength*2);
for (var i=0;i<buff.byteLength;i++){
newBuff[i] = buff[i];
}
buff = newBuff;
我已经像这样在 C++ 中完成了它。只需制作一个更大的数组并将内容复制过来,然后返回更大的数组并将其设置为原始数组。