追加ArrayBuffers [英] Appending ArrayBuffers

查看:164
本文介绍了追加ArrayBuffers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附加/组合ArrayBuffer的最佳方法是什么?

What is the preferable way of appending/combining ArrayBuffers?

我正在接收和解析具有各种数据结构的网络数据包.传入的消息被读入ArrayBuffers.如果有部分数据包到达,我需要存储它并等待下一条消息,然后再尝试解析它.

I'm receiving and parsing network packets with a variety of data structures. Incoming messages are read into ArrayBuffers. If a partial packet arrives I need to store it and wait for the next message before re-attempting to parse it.

目前我正在做这样的事情:

Currently I'm doing something like this:

function appendBuffer( buffer1, buffer2 ) {
  var tmp = new Uint8Array( buffer1.byteLength + buffer2.byteLength );
  tmp.set( new Uint8Array( buffer1 ), 0 );
  tmp.set( new Uint8Array( buffer2 ), buffer1.byteLength );
  return tmp.buffer;
}

很显然,由于ArrayBuffers具有固定的长度,因此您无法避免必须创建新的缓冲区,但是是否有必要初始化类型化的数组?到达后,我只希望能够将缓冲区视为缓冲区.类型和结构无关紧要.

Obviously you can't get around having to create a new buffer as ArrayBuffers are of a fixed length, but is it necessary to initialize typed arrays? Upon arrival I just want is to be able to treat the buffers as buffers; types and structures are of no concern.

推荐答案

您可以始终使用DataView(

You could always use DataView (http://www.khronos.org/registry/typedarray/specs/latest/#8) rather than a specific typed array, but as has been mentioned in the comments to your question, you can't actually do much with ArrayBuffer on its own.

这篇关于追加ArrayBuffers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆