|
34 | 34 |
|
35 | 35 | 很多浏览器操作的 API,用到了二进制数组操作二进制数据,下面是其中的几个。
|
36 | 36 |
|
37 |
| -- File API |
38 |
| -- XMLHttpRequest |
39 |
| -- Fetch API |
40 |
| -- Canvas |
41 |
| -- WebSockets |
| 37 | +- [Canvas](#canvas) |
| 38 | +- [Fetch API](#fetch-api) |
| 39 | +- [File API](#file-api) |
| 40 | +- [WebSockets](#websocket) |
| 41 | +- [XMLHttpRequest](#ajax) |
42 | 42 |
|
43 | 43 | ## ArrayBuffer 对象
|
44 | 44 |
|
@@ -448,37 +448,42 @@ Float64Array.BYTES_PER_ELEMENT // 8
|
448 | 448 |
|
449 | 449 | ### ArrayBuffer 与字符串的互相转换
|
450 | 450 |
|
451 |
| -`ArrayBuffer`转为字符串,或者字符串转为`ArrayBuffer`,有一个前提,即字符串的编码方法是确定的。假定字符串采用 UTF-16 编码(JavaScript 的内部编码方式),可以自己编写转换函数。 |
| 451 | +`ArrayBuffer` 和字符串的相互转换,使用原生 `TextEncoder` 和 `TextDecoder` 方法。为了便于说明用法,下面的代码都按照 TypeScript 的用法,给出了类型签名。 |
452 | 452 |
|
453 | 453 | ```javascript
|
454 |
| -// ArrayBuffer 转为字符串,参数为 ArrayBuffer 对象 |
455 |
| -function ab2str(buf) { |
456 |
| - // 注意,如果是大型二进制数组,为了避免溢出, |
457 |
| - // 必须一个一个字符地转 |
458 |
| - if (buf && buf.byteLength < 1024) { |
459 |
| - return String.fromCharCode.apply(null, new Uint16Array(buf)); |
460 |
| - } |
| 454 | +/** |
| 455 | + * Convert ArrayBuffer/TypedArray to String via TextDecoder |
| 456 | + * |
| 457 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder |
| 458 | + */ |
| 459 | +function ab2str( |
| 460 | + input: ArrayBuffer | Uint8Array | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array, |
| 461 | + outputEncoding: string = 'utf8', |
| 462 | +): string { |
| 463 | + const decoder = new TextDecoder(outputEncoding) |
| 464 | + return decoder.decode(input) |
| 465 | +} |
461 | 466 |
|
462 |
| - const bufView = new Uint16Array(buf); |
463 |
| - const len = bufView.length; |
464 |
| - const bstr = new Array(len); |
465 |
| - for (let i = 0; i < len; i++) { |
466 |
| - bstr[i] = String.fromCharCode.call(null, bufView[i]); |
467 |
| - } |
468 |
| - return bstr.join(''); |
| 467 | +/** |
| 468 | + * Convert String to ArrayBuffer via TextEncoder |
| 469 | + * |
| 470 | + * @see https://developer.mozilla.org/zh-CN/docs/Web/API/TextEncoder |
| 471 | + */ |
| 472 | +function str2ab(input: string): ArrayBuffer { |
| 473 | + const view = str2Uint8Array(input) |
| 474 | + return view.buffer |
469 | 475 | }
|
470 | 476 |
|
471 |
| -// 字符串转为 ArrayBuffer 对象,参数为字符串 |
472 |
| -function str2ab(str) { |
473 |
| - const buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节 |
474 |
| - const bufView = new Uint16Array(buf); |
475 |
| - for (let i = 0, strLen = str.length; i < strLen; i++) { |
476 |
| - bufView[i] = str.charCodeAt(i); |
477 |
| - } |
478 |
| - return buf; |
| 477 | +/** Convert String to Uint8Array */ |
| 478 | +function str2Uint8Array(input: string): Uint8Array { |
| 479 | + const encoder = new TextEncoder() |
| 480 | + const view = encoder.encode(input) |
| 481 | + return view |
479 | 482 | }
|
480 | 483 | ```
|
481 | 484 |
|
| 485 | +上面代码中,`ab2str()`的第二个参数`outputEncoding`给出了输出编码的编码,一般保持默认值(`utf-8`),其他可选值参见[官方文档](https://encoding.spec.whatwg.org)或 [Node.js 文档](https://nodejs.org/api/util.html#util_whatwg_supported_encodings)。 |
| 486 | + |
482 | 487 | ### 溢出
|
483 | 488 |
|
484 | 489 | 不同的视图类型,所能容纳的数值范围是确定的。超出这个范围,就会出现溢出。比如,8 位视图只能容纳一个 8 位的二进制值,如果放入一个 9 位的值,就会溢出。
|
@@ -574,7 +579,7 @@ v3.byteOffset // 2
|
574 | 579 |
|
575 | 580 | ### TypedArray.prototype.length
|
576 | 581 |
|
577 |
| -`length`属性表示 TypedArray 数组含有多少个成员。注意将`byteLength`属性和`length`属性区分,前者是字节长度,后者是成员长度。 |
| 582 | +`length`属性表示 `TypedArray` 数组含有多少个成员。注意将 `length` 属性和 `byteLength` 属性区分,前者是成员长度,后者是字节长度。 |
578 | 583 |
|
579 | 584 | ```javascript
|
580 | 585 | const a = new Int16Array(8);
|
|
0 commit comments