javascript 调整数组缓冲区的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18600895/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:32:25  来源:igfitidea点击:

Resize ArrayBuffer

javascriptarrays

提问by don kaka

If I want to create an arraybuffer, I write: var buff = new ArrayBuffer(size)

如果我想创建一个数组缓冲区,我写: var buff = new ArrayBuffer(size)

But how is it possible to resize an existing buffer? I mean, adding some more bytes at the end of the buffer.

但是如何调整现有缓冲区的大小呢?我的意思是,在缓冲区的末尾添加更多字节。

采纳答案by progrenhard

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;

I've done it in C++ like this. Just made a bigger array and copy the contents over and then return the larger array and set it as the original.

我已经像这样用 C++ 完成了。只是做了一个更大的数组并复制内容,然后返回更大的数组并将其设置为原始数组。

回答by user7090288

No set on the ArrayBuffer itself. There is seton the TypedArray though. Use like this:

ArrayBuffer 本身没有设置。有set对TypedArray虽然。像这样使用:

var oldBuffer = new ArrayBuffer(20);

var newBuffer = new ArrayBuffer(40);
new Uint8Array(newBuffer).set(oldBuffer);

回答by Artyer

The way to do that would be ArrayBuffer.transfer(oldBuffer, newByteLength), like so:

这样做的方法是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);

However (as of October 2017) there is 0% browser support (not even Node.js support) and is not even drafted yet.

但是(截至 2017 年 10 月)浏览器支持率为 0%(甚至不支持 Node.js),甚至还没有起草。



Until this is available, you can use the polyfill given on the page, which copies 8 bytes at a time, so is relatively quick for large arrays (Though it does not empty the given array, which is impossible to do).

在此可用之前,您可以使用页面上给出的 polyfill,它一次复制 8 个字节,因此对于大型数组来说相对较快(尽管它不会清空给定数组,这是不可能的)。

Here is a modified version using TypeArray.prototype.setinstead of for loops:

这是使用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;
  };
}

回答by Denys Séguret

You can't.

你不能。

From the MDN:

来自 MDN

The ArrayBuffer is a data type that is used to represent a generic, fixed-lengthbinary data buffer.

ArrayBuffer 是一种数据类型,用于表示通用的、 固定长度的二进制数据缓冲区。

I don't know what you try to do but obviously you don't use them how they're meant to be used.

我不知道你想做什么,但显然你没有按照它们的意图使用它们。

This pageis a good starting point to learn how to use typed arrays in JavaScript.

此页面是学习如何在 JavaScript 中使用类型化数组的良好起点。

回答by Jon Lennart Aasenden

What you are looking for is the "set" method. After creating a new, larger array, simply call the set function and copy over the older content.

您正在寻找的是“设置”方法。创建新的更大的数组后,只需调用 set 函数并复制旧内容。