javascript ArrayBuffer,它有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11554006/
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
javascript ArrayBuffer, what's it for?
提问by mkoryak
Maybe it's late, or maybe it's the sake, but I just read the docs for ArrayBufferand can't think of a single thing it would be really useful for.
也许为时已晚,也可能是因为这个原因,但我只是阅读了ArrayBuffer的文档,但想不出它真正有用的地方。
Can someone enlighten me?
有人可以启发我吗?
Are there any uses anyone can think of that don't involve images?
有没有任何人能想到的不涉及图像的用途?
采纳答案by Imdad
Basically ArrayBuffer is used to keep binary data. It can be the binary data of an image for example.
基本上 ArrayBuffer 用于保存二进制数据。例如,它可以是图像的二进制数据。
In other languages buffers are proved very useful. Yes, of-course it is little difficult to understand/use than other data types.
在其他语言中,缓冲区被证明非常有用。是的,当然,它比其他数据类型更难理解/使用。
ArrayBuffer can be used to get data of jpg image (RGB bytes) and produce a png out of it by adding alpha byte (i.e. RGBA).
ArrayBuffer 可用于获取 jpg 图像(RGB 字节)的数据,并通过添加 alpha 字节(即 RGBA)从中生成 png。
Mozilla site has given a small use of ArrayBuffer here
Mozilla 站点在此处对 ArrayBuffer 进行了少量使用
Working with complex data structures
By combining a single buffer with multiple views of different types, starting at different offsets into the buffer, you can interact with data objects containing multiple data types. This lets you, for example, interact with complex data structures from WebGL, data files, or C structures you need to use while using js-ctypes.
Consider this C structure:
struct someStruct { unsigned long id; char username[16]; float amountDue; };
You can access a buffer containing data in this format like this:
var buffer = new ArrayBuffer(24); // ... read the data into the buffer ... var idView = new Uint32Array(buffer, 0, 1); var usernameView = new Uint8Array(buffer, 4, 16); var amountDueView = new Float32Array(buffer, 20, 1);
Then you can access, for example, the amount due with
amountDueView[0]
.Note:The data structure alignmentin a C structure is platform-dependent. Take precautions and considerations for these padding differences.
处理复杂的数据结构
通过将单个缓冲区与多个不同类型的视图组合在一起,从缓冲区的不同偏移量开始,您可以与包含多种数据类型的数据对象进行交互。例如,这使您可以与WebGL 中的复杂数据结构、数据文件或在使用js-ctypes时需要使用的 C 结构进行交互。
考虑这个 C 结构:
struct someStruct { unsigned long id; char username[16]; float amountDue; };
您可以像这样访问包含这种格式数据的缓冲区:
var buffer = new ArrayBuffer(24); // ... read the data into the buffer ... var idView = new Uint32Array(buffer, 0, 1); var usernameView = new Uint8Array(buffer, 4, 16); var amountDueView = new Float32Array(buffer, 20, 1);
然后您可以访问,例如,到期金额
amountDueView[0]
。注意:C 结构中的数据结构对齐是平台相关的。对这些填充差异采取预防措施和考虑。
回答by Jonathan
Other than images, it's useful for precisely constructing and destructing low level network data packets used in protocols like UDP.
除了图像,它对于精确构建和破坏用于 UDP 等协议的低级网络数据包很有用。