melo-example-robot 1.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1188 @@
1
+ //////////////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // Copyright (c) 2014-present, Egret Technology.
4
+ // All rights reserved.
5
+ // Redistribution and use in source and binary forms, with or without
6
+ // modification, are permitted provided that the following conditions are met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright
9
+ // notice, this list of conditions and the following disclaimer.
10
+ // * Redistributions in binary form must reproduce the above copyright
11
+ // notice, this list of conditions and the following disclaimer in the
12
+ // documentation and/or other materials provided with the distribution.
13
+ // * Neither the name of the Egret nor the
14
+ // names of its contributors may be used to endorse or promote products
15
+ // derived from this software without specific prior written permission.
16
+ //
17
+ // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
18
+ // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
+ // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
+ // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
+ // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
23
+ // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
+ // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ //
28
+ //////////////////////////////////////////////////////////////////////////////////////
29
+
30
+
31
+ /**
32
+ * The Endian class contains values that denote the byte order used to represent multibyte numbers.
33
+ * The byte order is either bigEndian (most significant byte first) or littleEndian (least significant byte first).
34
+ * @version Egret 2.4
35
+ * @platform Web,Native
36
+ * @language en_US
37
+ */
38
+ /**
39
+ * Endian 类中包含一些值,它们表示用于表示多字节数字的字节顺序。
40
+ * 字节顺序为 bigEndian(最高有效字节位于最前)或 littleEndian(最低有效字节位于最前)。
41
+ * @version Egret 2.4
42
+ * @platform Web,Native
43
+ * @language zh_CN
44
+ */
45
+ export class Endian {
46
+ /**
47
+ * Indicates the least significant byte of the multibyte number appears first in the sequence of bytes.
48
+ * The hexadecimal number 0x12345678 has 4 bytes (2 hexadecimal digits per byte). The most significant byte is 0x12. The least significant byte is 0x78. (For the equivalent decimal number, 305419896, the most significant digit is 3, and the least significant digit is 6).
49
+ * @version Egret 2.4
50
+ * @platform Web,Native
51
+ * @language en_US
52
+ */
53
+ /**
54
+ * 表示多字节数字的最低有效字节位于字节序列的最前面。
55
+ * 十六进制数字 0x12345678 包含 4 个字节(每个字节包含 2 个十六进制数字)。最高有效字节为 0x12。最低有效字节为 0x78。(对于等效的十进制数字 305419896,最高有效数字是 3,最低有效数字是 6)。
56
+ * @version Egret 2.4
57
+ * @platform Web,Native
58
+ * @language zh_CN
59
+ */
60
+ public static LITTLE_ENDIAN: string = 'littleEndian';
61
+
62
+ /**
63
+ * Indicates the most significant byte of the multibyte number appears first in the sequence of bytes.
64
+ * The hexadecimal number 0x12345678 has 4 bytes (2 hexadecimal digits per byte). The most significant byte is 0x12. The least significant byte is 0x78. (For the equivalent decimal number, 305419896, the most significant digit is 3, and the least significant digit is 6).
65
+ * @version Egret 2.4
66
+ * @platform Web,Native
67
+ * @language en_US
68
+ */
69
+ /**
70
+ * 表示多字节数字的最高有效字节位于字节序列的最前面。
71
+ * 十六进制数字 0x12345678 包含 4 个字节(每个字节包含 2 个十六进制数字)。最高有效字节为 0x12。最低有效字节为 0x78。(对于等效的十进制数字 305419896,最高有效数字是 3,最低有效数字是 6)。
72
+ * @version Egret 2.4
73
+ * @platform Web,Native
74
+ * @language zh_CN
75
+ */
76
+ public static BIG_ENDIAN: string = 'bigEndian';
77
+
78
+ }
79
+
80
+ export const enum EndianConst {
81
+ LITTLE_ENDIAN = 0,
82
+ BIG_ENDIAN = 1
83
+ }
84
+
85
+ const enum ByteArraySize {
86
+
87
+ SIZE_OF_BOOLEAN = 1,
88
+
89
+ SIZE_OF_INT8 = 1,
90
+
91
+ SIZE_OF_INT16 = 2,
92
+
93
+ SIZE_OF_INT32 = 4,
94
+
95
+ SIZE_OF_UINT8 = 1,
96
+
97
+ SIZE_OF_UINT16 = 2,
98
+
99
+ SIZE_OF_UINT32 = 4,
100
+
101
+ SIZE_OF_FLOAT32 = 4,
102
+
103
+ SIZE_OF_FLOAT64 = 8
104
+ }
105
+ /**
106
+ * The ByteArray class provides methods and attributes for optimized reading and writing as well as dealing with binary data.
107
+ * Note: The ByteArray class is applied to the advanced developers who need to access data at the byte layer.
108
+ * @version Egret 2.4
109
+ * @platform Web,Native
110
+ * @includeExample egret/utils/ByteArray.ts
111
+ * @language en_US
112
+ */
113
+ /**
114
+ * ByteArray 类提供用于优化读取、写入以及处理二进制数据的方法和属性。
115
+ * 注意:ByteArray 类适用于需要在字节层访问数据的高级开发人员。
116
+ * @version Egret 2.4
117
+ * @platform Web,Native
118
+ * @includeExample egret/utils/ByteArray.ts
119
+ * @language zh_CN
120
+ */
121
+ export class ByteArray {
122
+
123
+ /**
124
+ * @private
125
+ */
126
+ protected bufferExtSize = 0; // Buffer expansion size
127
+
128
+ protected data: DataView;
129
+
130
+ protected _bytes: Uint8Array;
131
+ /**
132
+ * @private
133
+ */
134
+ protected _position: number;
135
+
136
+ /**
137
+ *
138
+ * 已经使用的字节偏移量
139
+ * @protected
140
+ * @type {number}
141
+ * @memberOf ByteArray
142
+ */
143
+ protected write_position: number;
144
+
145
+ /**
146
+ * Changes or reads the byte order; egret.EndianConst.BIG_ENDIAN or egret.EndianConst.LITTLE_EndianConst.
147
+ * @default egret.EndianConst.BIG_ENDIAN
148
+ * @version Egret 2.4
149
+ * @platform Web,Native
150
+ * @language en_US
151
+ */
152
+ /**
153
+ * 更改或读取数据的字节顺序;egret.EndianConst.BIG_ENDIAN 或 egret.EndianConst.LITTLE_ENDIAN。
154
+ * @default egret.EndianConst.BIG_ENDIAN
155
+ * @version Egret 2.4
156
+ * @platform Web,Native
157
+ * @language zh_CN
158
+ */
159
+ public get endian() {
160
+ return this.$endian === EndianConst.LITTLE_ENDIAN ? Endian.LITTLE_ENDIAN : Endian.BIG_ENDIAN;
161
+ }
162
+
163
+ public set endian(value: string) {
164
+ this.$endian = value === Endian.LITTLE_ENDIAN ? EndianConst.LITTLE_ENDIAN : EndianConst.BIG_ENDIAN;
165
+ }
166
+
167
+ protected $endian: EndianConst;
168
+
169
+ /**
170
+ * @version Egret 2.4
171
+ * @platform Web,Native
172
+ */
173
+ constructor(buffer?: ArrayBuffer | Uint8Array, bufferExtSize = 0) {
174
+ if (bufferExtSize < 0) {
175
+ bufferExtSize = 0;
176
+ }
177
+ this.bufferExtSize = bufferExtSize;
178
+ let bytes: Uint8Array, wpos = 0;
179
+ if (buffer) {// 有数据,则可写字节数从字节尾开始
180
+ let uint8: Uint8Array;
181
+ if (buffer instanceof Uint8Array) {
182
+ uint8 = buffer;
183
+ wpos = buffer.length;
184
+ } else {
185
+ wpos = buffer.byteLength;
186
+ uint8 = new Uint8Array(buffer);
187
+ }
188
+ if (bufferExtSize === 0) {
189
+ bytes = new Uint8Array(wpos);
190
+ }
191
+ else {
192
+ let multi = (wpos / bufferExtSize | 0) + 1;
193
+ bytes = new Uint8Array(multi * bufferExtSize);
194
+ }
195
+ bytes.set(uint8);
196
+ } else {
197
+ bytes = new Uint8Array(bufferExtSize);
198
+ }
199
+ this.write_position = wpos;
200
+ this._position = 0;
201
+ this._bytes = bytes;
202
+ this.data = new DataView(bytes.buffer);
203
+ this.endian = Endian.BIG_ENDIAN;
204
+ }
205
+
206
+
207
+ /**
208
+ * @deprecated
209
+ * @version Egret 2.4
210
+ * @platform Web,Native
211
+ */
212
+ public setArrayBuffer(buffer: ArrayBuffer): void {
213
+
214
+ }
215
+
216
+ /**
217
+ * 可读的剩余字节数
218
+ *
219
+ * @returns
220
+ *
221
+ * @memberOf ByteArray
222
+ */
223
+ public get readAvailable() {
224
+ return this.write_position - this._position;
225
+ }
226
+
227
+ public get buffer(): ArrayBuffer {
228
+ return this.data.buffer.slice(0, this.write_position);
229
+ }
230
+
231
+ public get rawBuffer(): ArrayBuffer {
232
+ return this.data.buffer;
233
+ }
234
+
235
+ /**
236
+ * @private
237
+ */
238
+ public set buffer(value: ArrayBuffer) {
239
+ let wpos = value.byteLength;
240
+ let uint8 = new Uint8Array(value);
241
+ let bufferExtSize = this.bufferExtSize;
242
+ let bytes: Uint8Array;
243
+ if (bufferExtSize === 0) {
244
+ bytes = new Uint8Array(wpos);
245
+ }
246
+ else {
247
+ let multi = (wpos / bufferExtSize | 0) + 1;
248
+ bytes = new Uint8Array(multi * bufferExtSize);
249
+ }
250
+ bytes.set(uint8);
251
+ this.write_position = wpos;
252
+ this._bytes = bytes;
253
+ this.data = new DataView(bytes.buffer);
254
+ }
255
+
256
+ public get bytes(): Uint8Array {
257
+ return this._bytes;
258
+ }
259
+
260
+ /**
261
+ * @private
262
+ * @version Egret 2.4
263
+ * @platform Web,Native
264
+ */
265
+ public get dataView(): DataView {
266
+ return this.data;
267
+ }
268
+
269
+ /**
270
+ * @private
271
+ */
272
+ public set dataView(value: DataView) {
273
+ this.buffer = value.buffer;
274
+ }
275
+
276
+ /**
277
+ * @private
278
+ */
279
+ public get bufferOffset(): number {
280
+ return this.data.byteOffset;
281
+ }
282
+
283
+ /**
284
+ * The current position of the file pointer (in bytes) to move or return to the ByteArray object. The next time you start reading reading method call in this position, or will start writing in this position next time call a write method.
285
+ * @version Egret 2.4
286
+ * @platform Web,Native
287
+ * @language en_US
288
+ */
289
+ /**
290
+ * 将文件指针的当前位置(以字节为单位)移动或返回到 ByteArray 对象中。下一次调用读取方法时将在此位置开始读取,或者下一次调用写入方法时将在此位置开始写入。
291
+ * @version Egret 2.4
292
+ * @platform Web,Native
293
+ * @language zh_CN
294
+ */
295
+ public get position(): number {
296
+ return this._position;
297
+ }
298
+
299
+ public set position(value: number) {
300
+ this._position = value;
301
+ if (value > this.write_position) {
302
+ this.write_position = value;
303
+ }
304
+ }
305
+
306
+ /**
307
+ * The length of the ByteArray object (in bytes).
308
+          * If the length is set to be larger than the current length, the right-side zero padding byte array.
309
+          * If the length is set smaller than the current length, the byte array is truncated.
310
+ * @version Egret 2.4
311
+ * @platform Web,Native
312
+ * @language en_US
313
+ */
314
+ /**
315
+ * ByteArray 对象的长度(以字节为单位)。
316
+ * 如果将长度设置为大于当前长度的值,则用零填充字节数组的右侧。
317
+ * 如果将长度设置为小于当前长度的值,将会截断该字节数组。
318
+ * @version Egret 2.4
319
+ * @platform Web,Native
320
+ * @language zh_CN
321
+ */
322
+ public get length(): number {
323
+ return this.write_position;
324
+ }
325
+
326
+ public set length(value: number) {
327
+ this.write_position = value;
328
+ if (this.data.byteLength > value) {
329
+ this._position = value;
330
+ }
331
+ this._validateBuffer(value);
332
+ }
333
+
334
+ protected _validateBuffer(value: number) {
335
+ if (this.data.byteLength < value) {
336
+ let be = this.bufferExtSize;
337
+ let tmp: Uint8Array;
338
+ if (be === 0) {
339
+ tmp = new Uint8Array(value);
340
+ }
341
+ else {
342
+ let nLen = ((value / be >> 0) + 1) * be;
343
+ tmp = new Uint8Array(nLen);
344
+ }
345
+ tmp.set(this._bytes);
346
+ this._bytes = tmp;
347
+ this.data = new DataView(tmp.buffer);
348
+ }
349
+ }
350
+
351
+ /**
352
+ * The number of bytes that can be read from the current position of the byte array to the end of the array data.
353
+ * When you access a ByteArray object, the bytesAvailable property in conjunction with the read methods each use to make sure you are reading valid data.
354
+ * @version Egret 2.4
355
+ * @platform Web,Native
356
+ * @language en_US
357
+ */
358
+ /**
359
+ * 可从字节数组的当前位置到数组末尾读取的数据的字节数。
360
+ * 每次访问 ByteArray 对象时,将 bytesAvailable 属性与读取方法结合使用,以确保读取有效的数据。
361
+ * @version Egret 2.4
362
+ * @platform Web,Native
363
+ * @language zh_CN
364
+ */
365
+ public get bytesAvailable(): number {
366
+ return this.data.byteLength - this._position;
367
+ }
368
+
369
+ /**
370
+ * Clears the contents of the byte array and resets the length and position properties to 0.
371
+ * @version Egret 2.4
372
+ * @platform Web,Native
373
+ * @language en_US
374
+ */
375
+ /**
376
+ * 清除字节数组的内容,并将 length 和 position 属性重置为 0。
377
+ * @version Egret 2.4
378
+ * @platform Web,Native
379
+ * @language zh_CN
380
+ */
381
+ public clear(): void {
382
+ let buffer = new ArrayBuffer(this.bufferExtSize);
383
+ this.data = new DataView(buffer);
384
+ this._bytes = new Uint8Array(buffer);
385
+ this._position = 0;
386
+ this.write_position = 0;
387
+ }
388
+
389
+ /**
390
+ * Read a Boolean value from the byte stream. Read a simple byte. If the byte is non-zero, it returns true; otherwise, it returns false.
391
+ * @return If the byte is non-zero, it returns true; otherwise, it returns false.
392
+ * @version Egret 2.4
393
+ * @platform Web,Native
394
+ * @language en_US
395
+ */
396
+ /**
397
+ * 从字节流中读取布尔值。读取单个字节,如果字节非零,则返回 true,否则返回 false
398
+ * @return 如果字节不为零,则返回 true,否则返回 false
399
+ * @version Egret 2.4
400
+ * @platform Web,Native
401
+ * @language zh_CN
402
+ */
403
+ public readBoolean(): boolean {
404
+ if (this.validate(ByteArraySize.SIZE_OF_BOOLEAN)) return !!this._bytes[this.position++];
405
+ }
406
+
407
+ /**
408
+ * Read signed bytes from the byte stream.
409
+ * @return An integer ranging from -128 to 127
410
+ * @version Egret 2.4
411
+ * @platform Web,Native
412
+ * @language en_US
413
+ */
414
+ /**
415
+ * 从字节流中读取带符号的字节
416
+ * @return 介于 -128 和 127 之间的整数
417
+ * @version Egret 2.4
418
+ * @platform Web,Native
419
+ * @language zh_CN
420
+ */
421
+ public readByte(): number {
422
+ if (this.validate(ByteArraySize.SIZE_OF_INT8)) return this.data.getInt8(this.position++);
423
+ }
424
+
425
+ /**
426
+ * Read data byte number specified by the length parameter from the byte stream. Starting from the position specified by offset, read bytes into the ByteArray object specified by the bytes parameter, and write bytes into the target ByteArray
427
+ * @param bytes ByteArray object that data is read into
428
+ * @param offset Offset (position) in bytes. Read data should be written from this position
429
+ * @param length Byte number to be read Default value 0 indicates reading all available data
430
+ * @version Egret 2.4
431
+ * @platform Web,Native
432
+ * @language en_US
433
+ */
434
+ /**
435
+ * 从字节流中读取 length 参数指定的数据字节数。从 offset 指定的位置开始,将字节读入 bytes 参数指定的 ByteArray 对象中,并将字节写入目标 ByteArray 中
436
+ * @param bytes 要将数据读入的 ByteArray 对象
437
+ * @param offset bytes 中的偏移(位置),应从该位置写入读取的数据
438
+ * @param length 要读取的字节数。默认值 0 导致读取所有可用的数据
439
+ * @version Egret 2.4
440
+ * @platform Web,Native
441
+ * @language zh_CN
442
+ */
443
+ public readBytes(bytes: ByteArray, offset: number = 0, length: number = 0): void {
444
+ if (!bytes) {// 由于bytes不返回,所以new新的无意义
445
+ return;
446
+ }
447
+ let pos = this._position;
448
+ let available = this.write_position - pos;
449
+ if (available < 0) {
450
+ throw new Error('1025');
451
+ // return;
452
+ }
453
+ if (length === 0) {
454
+ length = available;
455
+ }
456
+ else if (length > available) {
457
+ throw new Error('1025');
458
+ // return;
459
+ }
460
+ bytes.validateBuffer(offset + length);
461
+ bytes._bytes.set(this._bytes.subarray(pos, pos + length), offset);
462
+ this.position += length;
463
+ }
464
+
465
+ /**
466
+ * Read an IEEE 754 double-precision (64 bit) floating point number from the byte stream
467
+ * @return Double-precision (64 bit) floating point number
468
+ * @version Egret 2.4
469
+ * @platform Web,Native
470
+ * @language en_US
471
+ */
472
+ /**
473
+ * 从字节流中读取一个 IEEE 754 双精度(64 位)浮点数
474
+ * @return 双精度(64 位)浮点数
475
+ * @version Egret 2.4
476
+ * @platform Web,Native
477
+ * @language zh_CN
478
+ */
479
+ public readDouble(): number {
480
+ if (this.validate(ByteArraySize.SIZE_OF_FLOAT64)) {
481
+ let value = this.data.getFloat64(this._position, this.$endian === EndianConst.LITTLE_ENDIAN);
482
+ this.position += ByteArraySize.SIZE_OF_FLOAT64;
483
+ return value;
484
+ }
485
+ }
486
+
487
+ /**
488
+ * Read an IEEE 754 single-precision (32 bit) floating point number from the byte stream
489
+ * @return Single-precision (32 bit) floating point number
490
+ * @version Egret 2.4
491
+ * @platform Web,Native
492
+ * @language en_US
493
+ */
494
+ /**
495
+ * 从字节流中读取一个 IEEE 754 单精度(32 位)浮点数
496
+ * @return 单精度(32 位)浮点数
497
+ * @version Egret 2.4
498
+ * @platform Web,Native
499
+ * @language zh_CN
500
+ */
501
+ public readFloat(): number {
502
+ if (this.validate(ByteArraySize.SIZE_OF_FLOAT32)) {
503
+ let value = this.data.getFloat32(this._position, this.$endian === EndianConst.LITTLE_ENDIAN);
504
+ this.position += ByteArraySize.SIZE_OF_FLOAT32;
505
+ return value;
506
+ }
507
+ }
508
+
509
+ /**
510
+ * Read a 32-bit signed integer from the byte stream.
511
+ * @return A 32-bit signed integer ranging from -2147483648 to 2147483647
512
+ * @version Egret 2.4
513
+ * @platform Web,Native
514
+ * @language en_US
515
+ */
516
+ /**
517
+ * 从字节流中读取一个带符号的 32 位整数
518
+ * @return 介于 -2147483648 和 2147483647 之间的 32 位带符号整数
519
+ * @version Egret 2.4
520
+ * @platform Web,Native
521
+ * @language zh_CN
522
+ */
523
+ public readInt(): number {
524
+ if (this.validate(ByteArraySize.SIZE_OF_INT32)) {
525
+ let value = this.data.getInt32(this._position, this.$endian === EndianConst.LITTLE_ENDIAN);
526
+ this.position += ByteArraySize.SIZE_OF_INT32;
527
+ return value;
528
+ }
529
+ }
530
+
531
+ /**
532
+ * Read a 16-bit signed integer from the byte stream.
533
+ * @return A 16-bit signed integer ranging from -32768 to 32767
534
+ * @version Egret 2.4
535
+ * @platform Web,Native
536
+ * @language en_US
537
+ */
538
+ /**
539
+ * 从字节流中读取一个带符号的 16 位整数
540
+ * @return 介于 -32768 和 32767 之间的 16 位带符号整数
541
+ * @version Egret 2.4
542
+ * @platform Web,Native
543
+ * @language zh_CN
544
+ */
545
+ public readShort(): number {
546
+ if (this.validate(ByteArraySize.SIZE_OF_INT16)) {
547
+ let value = this.data.getInt16(this._position, this.$endian === EndianConst.LITTLE_ENDIAN);
548
+ this.position += ByteArraySize.SIZE_OF_INT16;
549
+ return value;
550
+ }
551
+ }
552
+
553
+ /**
554
+ * Read unsigned bytes from the byte stream.
555
+ * @return A 32-bit unsigned integer ranging from 0 to 255
556
+ * @version Egret 2.4
557
+ * @platform Web,Native
558
+ * @language en_US
559
+ */
560
+ /**
561
+ * 从字节流中读取无符号的字节
562
+ * @return 介于 0 和 255 之间的 32 位无符号整数
563
+ * @version Egret 2.4
564
+ * @platform Web,Native
565
+ * @language zh_CN
566
+ */
567
+ public readUnsignedByte(): number {
568
+ if (this.validate(ByteArraySize.SIZE_OF_UINT8)) return this._bytes[this.position++];
569
+ }
570
+
571
+ /**
572
+ * Read a 32-bit unsigned integer from the byte stream.
573
+ * @return A 32-bit unsigned integer ranging from 0 to 4294967295
574
+ * @version Egret 2.4
575
+ * @platform Web,Native
576
+ * @language en_US
577
+ */
578
+ /**
579
+ * 从字节流中读取一个无符号的 32 位整数
580
+ * @return 介于 0 和 4294967295 之间的 32 位无符号整数
581
+ * @version Egret 2.4
582
+ * @platform Web,Native
583
+ * @language zh_CN
584
+ */
585
+ public readUnsignedInt(): number {
586
+ if (this.validate(ByteArraySize.SIZE_OF_UINT32)) {
587
+ let value = this.data.getUint32(this._position, this.$endian === EndianConst.LITTLE_ENDIAN);
588
+ this.position += ByteArraySize.SIZE_OF_UINT32;
589
+ return value;
590
+ }
591
+ }
592
+
593
+ /**
594
+ * Read a 16-bit unsigned integer from the byte stream.
595
+ * @return A 16-bit unsigned integer ranging from 0 to 65535
596
+ * @version Egret 2.4
597
+ * @platform Web,Native
598
+ * @language en_US
599
+ */
600
+ /**
601
+ * 从字节流中读取一个无符号的 16 位整数
602
+ * @return 介于 0 和 65535 之间的 16 位无符号整数
603
+ * @version Egret 2.4
604
+ * @platform Web,Native
605
+ * @language zh_CN
606
+ */
607
+ public readUnsignedShort(): number {
608
+ if (this.validate(ByteArraySize.SIZE_OF_UINT16)) {
609
+ let value = this.data.getUint16(this._position, this.$endian === EndianConst.LITTLE_ENDIAN);
610
+ this.position += ByteArraySize.SIZE_OF_UINT16;
611
+ return value;
612
+ }
613
+ }
614
+
615
+ /**
616
+ * Read a UTF-8 character string from the byte stream Assume that the prefix of the character string is a short unsigned integer (use byte to express length)
617
+ * @return UTF-8 character string
618
+ * @version Egret 2.4
619
+ * @platform Web,Native
620
+ * @language en_US
621
+ */
622
+ /**
623
+ * 从字节流中读取一个 UTF-8 字符串。假定字符串的前缀是无符号的短整型(以字节表示长度)
624
+ * @return UTF-8 编码的字符串
625
+ * @version Egret 2.4
626
+ * @platform Web,Native
627
+ * @language zh_CN
628
+ */
629
+ public readUTF(): string {
630
+ let length = this.readUnsignedShort();
631
+ if (length > 0) {
632
+ return this.readUTFBytes(length);
633
+ } else {
634
+ return '';
635
+ }
636
+ }
637
+
638
+ /**
639
+ * Read a UTF-8 byte sequence specified by the length parameter from the byte stream, and then return a character string
640
+ * @param Specify a short unsigned integer of the UTF-8 byte length
641
+ * @return A character string consists of UTF-8 bytes of the specified length
642
+ * @version Egret 2.4
643
+ * @platform Web,Native
644
+ * @language en_US
645
+ */
646
+ /**
647
+ * 从字节流中读取一个由 length 参数指定的 UTF-8 字节序列,并返回一个字符串
648
+ * @param length 指明 UTF-8 字节长度的无符号短整型数
649
+ * @return 由指定长度的 UTF-8 字节组成的字符串
650
+ * @version Egret 2.4
651
+ * @platform Web,Native
652
+ * @language zh_CN
653
+ */
654
+ public readUTFBytes(length: number): string {
655
+ if (!this.validate(length)) {
656
+ return;
657
+ }
658
+ let data = this.data;
659
+ let bytes = new Uint8Array(data.buffer, data.byteOffset + this._position, length);
660
+ this.position += length;
661
+ return this.decodeUTF8(bytes);
662
+ }
663
+
664
+ /**
665
+ * Write a Boolean value. A single byte is written according to the value parameter. If the value is true, write 1; if the value is false, write 0.
666
+ * @param value A Boolean value determining which byte is written. If the value is true, write 1; if the value is false, write 0.
667
+ * @version Egret 2.4
668
+ * @platform Web,Native
669
+ * @language en_US
670
+ */
671
+ /**
672
+ * 写入布尔值。根据 value 参数写入单个字节。如果为 true,则写入 1,如果为 false,则写入 0
673
+ * @param value 确定写入哪个字节的布尔值。如果该参数为 true,则该方法写入 1;如果该参数为 false,则该方法写入 0
674
+ * @version Egret 2.4
675
+ * @platform Web,Native
676
+ * @language zh_CN
677
+ */
678
+ public writeBoolean(value: boolean): void {
679
+ this.validateBuffer(ByteArraySize.SIZE_OF_BOOLEAN);
680
+ this._bytes[this.position++] = +value;
681
+ }
682
+
683
+ /**
684
+ * Write a byte into the byte stream
685
+ * The low 8 bits of the parameter are used. The high 24 bits are ignored.
686
+ * @param value A 32-bit integer. The low 8 bits will be written into the byte stream
687
+ * @version Egret 2.4
688
+ * @platform Web,Native
689
+ * @language en_US
690
+ */
691
+ /**
692
+ * 在字节流中写入一个字节
693
+ * 使用参数的低 8 位。忽略高 24 位
694
+ * @param value 一个 32 位整数。低 8 位将被写入字节流
695
+ * @version Egret 2.4
696
+ * @platform Web,Native
697
+ * @language zh_CN
698
+ */
699
+ public writeByte(value: number): void {
700
+ this.validateBuffer(ByteArraySize.SIZE_OF_INT8);
701
+ this._bytes[this.position++] = value & 0xff;
702
+ }
703
+
704
+ /**
705
+ * Write the byte sequence that includes length bytes in the specified byte array, bytes, (starting at the byte specified by offset, using a zero-based index), into the byte stream
706
+ * If the length parameter is omitted, the default length value 0 is used and the entire buffer starting at offset is written. If the offset parameter is also omitted, the entire buffer is written
707
+ * If the offset or length parameter is out of range, they are clamped to the beginning and end of the bytes array.
708
+ * @param bytes ByteArray Object
709
+ * @param offset A zero-based index specifying the position into the array to begin writing
710
+ * @param length An unsigned integer specifying how far into the buffer to write
711
+ * @version Egret 2.4
712
+ * @platform Web,Native
713
+ * @language en_US
714
+ */
715
+ /**
716
+ * 将指定字节数组 bytes(起始偏移量为 offset,从零开始的索引)中包含 length 个字节的字节序列写入字节流
717
+ * 如果省略 length 参数,则使用默认长度 0;该方法将从 offset 开始写入整个缓冲区。如果还省略了 offset 参数,则写入整个缓冲区
718
+ * 如果 offset 或 length 超出范围,它们将被锁定到 bytes 数组的开头和结尾
719
+ * @param bytes ByteArray 对象
720
+ * @param offset 从 0 开始的索引,表示在数组中开始写入的位置
721
+ * @param length 一个无符号整数,表示在缓冲区中的写入范围
722
+ * @version Egret 2.4
723
+ * @platform Web,Native
724
+ * @language zh_CN
725
+ */
726
+ public writeBytes(bytes: ByteArray, offset: number = 0, length: number = 0): void {
727
+ let writeLength: number;
728
+ if (offset < 0) {
729
+ return;
730
+ }
731
+ if (length < 0) {
732
+ return;
733
+ } else if (length === 0) {
734
+ writeLength = bytes.length - offset;
735
+ } else {
736
+ writeLength = Math.min(bytes.length - offset, length);
737
+ }
738
+ if (writeLength > 0) {
739
+ this.validateBuffer(writeLength);
740
+ this._bytes.set(bytes._bytes.subarray(offset, offset + writeLength), this._position);
741
+ this.position = this._position + writeLength;
742
+ }
743
+ }
744
+
745
+ /**
746
+ * Write an IEEE 754 double-precision (64 bit) floating point number into the byte stream
747
+ * @param value Double-precision (64 bit) floating point number
748
+ * @version Egret 2.4
749
+ * @platform Web,Native
750
+ * @language en_US
751
+ */
752
+ /**
753
+ * 在字节流中写入一个 IEEE 754 双精度(64 位)浮点数
754
+ * @param value 双精度(64 位)浮点数
755
+ * @version Egret 2.4
756
+ * @platform Web,Native
757
+ * @language zh_CN
758
+ */
759
+ public writeDouble(value: number): void {
760
+ this.validateBuffer(ByteArraySize.SIZE_OF_FLOAT64);
761
+ this.data.setFloat64(this._position, value, this.$endian === EndianConst.LITTLE_ENDIAN);
762
+ this.position += ByteArraySize.SIZE_OF_FLOAT64;
763
+ }
764
+
765
+ /**
766
+ * Write an IEEE 754 single-precision (32 bit) floating point number into the byte stream
767
+ * @param value Single-precision (32 bit) floating point number
768
+ * @version Egret 2.4
769
+ * @platform Web,Native
770
+ * @language en_US
771
+ */
772
+ /**
773
+ * 在字节流中写入一个 IEEE 754 单精度(32 位)浮点数
774
+ * @param value 单精度(32 位)浮点数
775
+ * @version Egret 2.4
776
+ * @platform Web,Native
777
+ * @language zh_CN
778
+ */
779
+ public writeFloat(value: number): void {
780
+ this.validateBuffer(ByteArraySize.SIZE_OF_FLOAT32);
781
+ this.data.setFloat32(this._position, value, this.$endian === EndianConst.LITTLE_ENDIAN);
782
+ this.position += ByteArraySize.SIZE_OF_FLOAT32;
783
+ }
784
+
785
+ /**
786
+ * Write a 32-bit signed integer into the byte stream
787
+ * @param value An integer to be written into the byte stream
788
+ * @version Egret 2.4
789
+ * @platform Web,Native
790
+ * @language en_US
791
+ */
792
+ /**
793
+ * 在字节流中写入一个带符号的 32 位整数
794
+ * @param value 要写入字节流的整数
795
+ * @version Egret 2.4
796
+ * @platform Web,Native
797
+ * @language zh_CN
798
+ */
799
+ public writeInt(value: number): void {
800
+ this.validateBuffer(ByteArraySize.SIZE_OF_INT32);
801
+ this.data.setInt32(this._position, value, this.$endian === EndianConst.LITTLE_ENDIAN);
802
+ this.position += ByteArraySize.SIZE_OF_INT32;
803
+ }
804
+
805
+ /**
806
+ * Write a 16-bit integer into the byte stream. The low 16 bits of the parameter are used. The high 16 bits are ignored.
807
+ * @param value A 32-bit integer. Its low 16 bits will be written into the byte stream
808
+ * @version Egret 2.4
809
+ * @platform Web,Native
810
+ * @language en_US
811
+ */
812
+ /**
813
+ * 在字节流中写入一个 16 位整数。使用参数的低 16 位。忽略高 16 位
814
+ * @param value 32 位整数,该整数的低 16 位将被写入字节流
815
+ * @version Egret 2.4
816
+ * @platform Web,Native
817
+ * @language zh_CN
818
+ */
819
+ public writeShort(value: number): void {
820
+ this.validateBuffer(ByteArraySize.SIZE_OF_INT16);
821
+ this.data.setInt16(this._position, value, this.$endian === EndianConst.LITTLE_ENDIAN);
822
+ this.position += ByteArraySize.SIZE_OF_INT16;
823
+ }
824
+
825
+ /**
826
+ * Write a 32-bit unsigned integer into the byte stream
827
+ * @param value An unsigned integer to be written into the byte stream
828
+ * @version Egret 2.4
829
+ * @platform Web,Native
830
+ * @language en_US
831
+ */
832
+ /**
833
+ * 在字节流中写入一个无符号的 32 位整数
834
+ * @param value 要写入字节流的无符号整数
835
+ * @version Egret 2.4
836
+ * @platform Web,Native
837
+ * @language zh_CN
838
+ */
839
+ public writeUnsignedInt(value: number): void {
840
+ this.validateBuffer(ByteArraySize.SIZE_OF_UINT32);
841
+ this.data.setUint32(this._position, value, this.$endian === EndianConst.LITTLE_ENDIAN);
842
+ this.position += ByteArraySize.SIZE_OF_UINT32;
843
+ }
844
+
845
+ /**
846
+ * Write a 16-bit unsigned integer into the byte stream
847
+ * @param value An unsigned integer to be written into the byte stream
848
+ * @version Egret 2.5
849
+ * @platform Web,Native
850
+ * @language en_US
851
+ */
852
+ /**
853
+ * 在字节流中写入一个无符号的 16 位整数
854
+ * @param value 要写入字节流的无符号整数
855
+ * @version Egret 2.5
856
+ * @platform Web,Native
857
+ * @language zh_CN
858
+ */
859
+ public writeUnsignedShort(value: number): void {
860
+ this.validateBuffer(ByteArraySize.SIZE_OF_UINT16);
861
+ this.data.setUint16(this._position, value, this.$endian === EndianConst.LITTLE_ENDIAN);
862
+ this.position += ByteArraySize.SIZE_OF_UINT16;
863
+ }
864
+
865
+ /**
866
+ * Write a UTF-8 string into the byte stream. The length of the UTF-8 string in bytes is written first, as a 16-bit integer, followed by the bytes representing the characters of the string
867
+ * @param value Character string value to be written
868
+ * @version Egret 2.4
869
+ * @platform Web,Native
870
+ * @language en_US
871
+ */
872
+ /**
873
+ * 将 UTF-8 字符串写入字节流。先写入以字节表示的 UTF-8 字符串长度(作为 16 位整数),然后写入表示字符串字符的字节
874
+ * @param value 要写入的字符串值
875
+ * @version Egret 2.4
876
+ * @platform Web,Native
877
+ * @language zh_CN
878
+ */
879
+ public writeUTF(value: string): void {
880
+ let utf8bytes: ArrayLike<number> = this.encodeUTF8(value);
881
+ let length: number = utf8bytes.length;
882
+ this.validateBuffer(ByteArraySize.SIZE_OF_UINT16 + length);
883
+ this.data.setUint16(this._position, length, this.$endian === EndianConst.LITTLE_ENDIAN);
884
+ this.position += ByteArraySize.SIZE_OF_UINT16;
885
+ this._writeUint8Array(utf8bytes, false);
886
+ }
887
+
888
+ /**
889
+ * Write a UTF-8 string into the byte stream. Similar to the writeUTF() method, but the writeUTFBytes() method does not prefix the string with a 16-bit length word
890
+ * @param value Character string value to be written
891
+ * @version Egret 2.4
892
+ * @platform Web,Native
893
+ * @language en_US
894
+ */
895
+ /**
896
+ * 将 UTF-8 字符串写入字节流。类似于 writeUTF() 方法,但 writeUTFBytes() 不使用 16 位长度的词为字符串添加前缀
897
+ * @param value 要写入的字符串值
898
+ * @version Egret 2.4
899
+ * @platform Web,Native
900
+ * @language zh_CN
901
+ */
902
+ public writeUTFBytes(value: string): void {
903
+ this._writeUint8Array(this.encodeUTF8(value));
904
+ }
905
+
906
+
907
+ /**
908
+ *
909
+ * @returns
910
+ * @version Egret 2.4
911
+ * @platform Web,Native
912
+ */
913
+ public toString(): string {
914
+ return '[ByteArray] length:' + this.length + ', bytesAvailable:' + this.bytesAvailable;
915
+ }
916
+
917
+ /**
918
+ * @private
919
+ * 将 Uint8Array 写入字节流
920
+ * @param bytes 要写入的Uint8Array
921
+ * @param validateBuffer
922
+ */
923
+ public _writeUint8Array(bytes: Uint8Array | ArrayLike<number>, validateBuffer: boolean = true): void {
924
+ let pos = this._position;
925
+ let npos = pos + bytes.length;
926
+ if (validateBuffer) {
927
+ this.validateBuffer(npos);
928
+ }
929
+ this.bytes.set(bytes, pos);
930
+ this.position = npos;
931
+ }
932
+
933
+ /**
934
+ * @param len
935
+ * @returns
936
+ * @version Egret 2.4
937
+ * @platform Web,Native
938
+ * @private
939
+ */
940
+ public validate(len: number): boolean {
941
+ let bl = this._bytes.length;
942
+ if (bl > 0 && this._position + len <= bl) {
943
+ return true;
944
+ } else {
945
+ console.error(1025);
946
+ }
947
+ }
948
+
949
+ /**********************/
950
+ /* PRIVATE METHODS */
951
+ /**********************/
952
+ /**
953
+ * @private
954
+ * @param len
955
+ * @param needReplace
956
+ */
957
+ protected validateBuffer(len: number): void {
958
+ this.write_position = len > this.write_position ? len : this.write_position;
959
+ len += this._position;
960
+ this._validateBuffer(len);
961
+ }
962
+
963
+ /**
964
+ * @private
965
+ * UTF-8 Encoding/Decoding
966
+ */
967
+ private encodeUTF8(str: string): Uint8Array {
968
+ let pos: number = 0;
969
+ let codePoints = this.stringToCodePoints(str);
970
+ let outputBytes = [];
971
+
972
+ while (codePoints.length > pos) {
973
+ let code_point: number = codePoints[pos++];
974
+
975
+ if (this.inRange(code_point, 0xD800, 0xDFFF)) {
976
+ this.encoderError(code_point);
977
+ }
978
+ else if (this.inRange(code_point, 0x0000, 0x007f)) {
979
+ outputBytes.push(code_point);
980
+ } else {
981
+ let count, offset;
982
+ if (this.inRange(code_point, 0x0080, 0x07FF)) {
983
+ count = 1;
984
+ offset = 0xC0;
985
+ } else if (this.inRange(code_point, 0x0800, 0xFFFF)) {
986
+ count = 2;
987
+ offset = 0xE0;
988
+ } else if (this.inRange(code_point, 0x10000, 0x10FFFF)) {
989
+ count = 3;
990
+ offset = 0xF0;
991
+ }
992
+
993
+ outputBytes.push(this.div(code_point, Math.pow(64, count)) + offset);
994
+
995
+ while (count > 0) {
996
+ let temp = this.div(code_point, Math.pow(64, count - 1));
997
+ outputBytes.push(0x80 + (temp % 64));
998
+ count -= 1;
999
+ }
1000
+ }
1001
+ }
1002
+ return new Uint8Array(outputBytes);
1003
+ }
1004
+
1005
+ /**
1006
+ * @private
1007
+ *
1008
+ * @param data
1009
+ * @returns
1010
+ */
1011
+ private decodeUTF8(data: Uint8Array): string {
1012
+ let fatal: boolean = false;
1013
+ let pos: number = 0;
1014
+ let result: string = '';
1015
+ let code_point: number;
1016
+ let utf8_code_point = 0;
1017
+ let utf8_bytes_needed = 0;
1018
+ let utf8_bytes_seen = 0;
1019
+ let utf8_lower_boundary = 0;
1020
+
1021
+ while (data.length > pos) {
1022
+
1023
+ let _byte = data[pos++];
1024
+
1025
+ if (_byte === this.EOF_byte) {
1026
+ if (utf8_bytes_needed !== 0) {
1027
+ code_point = this.decoderError(fatal);
1028
+ } else {
1029
+ code_point = this.EOF_code_point;
1030
+ }
1031
+ } else {
1032
+
1033
+ if (utf8_bytes_needed === 0) {
1034
+ if (this.inRange(_byte, 0x00, 0x7F)) {
1035
+ code_point = _byte;
1036
+ } else {
1037
+ if (this.inRange(_byte, 0xC2, 0xDF)) {
1038
+ utf8_bytes_needed = 1;
1039
+ utf8_lower_boundary = 0x80;
1040
+ utf8_code_point = _byte - 0xC0;
1041
+ } else if (this.inRange(_byte, 0xE0, 0xEF)) {
1042
+ utf8_bytes_needed = 2;
1043
+ utf8_lower_boundary = 0x800;
1044
+ utf8_code_point = _byte - 0xE0;
1045
+ } else if (this.inRange(_byte, 0xF0, 0xF4)) {
1046
+ utf8_bytes_needed = 3;
1047
+ utf8_lower_boundary = 0x10000;
1048
+ utf8_code_point = _byte - 0xF0;
1049
+ } else {
1050
+ this.decoderError(fatal);
1051
+ }
1052
+ utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
1053
+ code_point = null;
1054
+ }
1055
+ } else if (!this.inRange(_byte, 0x80, 0xBF)) {
1056
+ utf8_code_point = 0;
1057
+ utf8_bytes_needed = 0;
1058
+ utf8_bytes_seen = 0;
1059
+ utf8_lower_boundary = 0;
1060
+ pos--;
1061
+ code_point = this.decoderError(fatal, _byte);
1062
+ } else {
1063
+
1064
+ utf8_bytes_seen += 1;
1065
+ utf8_code_point = utf8_code_point + (_byte - 0x80) * Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
1066
+
1067
+ if (utf8_bytes_seen !== utf8_bytes_needed) {
1068
+ code_point = null;
1069
+ } else {
1070
+
1071
+ let cp = utf8_code_point;
1072
+ let lower_boundary = utf8_lower_boundary;
1073
+ utf8_code_point = 0;
1074
+ utf8_bytes_needed = 0;
1075
+ utf8_bytes_seen = 0;
1076
+ utf8_lower_boundary = 0;
1077
+ if (this.inRange(cp, lower_boundary, 0x10FFFF) && !this.inRange(cp, 0xD800, 0xDFFF)) {
1078
+ code_point = cp;
1079
+ } else {
1080
+ code_point = this.decoderError(fatal, _byte);
1081
+ }
1082
+ }
1083
+
1084
+ }
1085
+ }
1086
+ // Decode string
1087
+ if (code_point !== null && code_point !== this.EOF_code_point) {
1088
+ if (code_point <= 0xFFFF) {
1089
+ if (code_point > 0) result += String.fromCharCode(code_point);
1090
+ } else {
1091
+ code_point -= 0x10000;
1092
+ result += String.fromCharCode(0xD800 + ((code_point >> 10) & 0x3ff));
1093
+ result += String.fromCharCode(0xDC00 + (code_point & 0x3ff));
1094
+ }
1095
+ }
1096
+ }
1097
+ return result;
1098
+ }
1099
+
1100
+ /**
1101
+ * @private
1102
+ *
1103
+ * @param code_point
1104
+ */
1105
+ private encoderError(code_point: any) {
1106
+ console.error(1026, code_point);
1107
+ }
1108
+
1109
+ /**
1110
+ * @private
1111
+ *
1112
+ * @param fatal
1113
+ * @param opt_code_point
1114
+ * @returns
1115
+ */
1116
+ private decoderError(fatal: any, opt_code_point?: any): number {
1117
+ if (fatal) {
1118
+ console.error(1027);
1119
+ }
1120
+ return opt_code_point || 0xFFFD;
1121
+ }
1122
+
1123
+ /**
1124
+ * @private
1125
+ */
1126
+ private EOF_byte: number = -1;
1127
+ /**
1128
+ * @private
1129
+ */
1130
+ private EOF_code_point: number = -1;
1131
+
1132
+ /**
1133
+ * @private
1134
+ *
1135
+ * @param a
1136
+ * @param min
1137
+ * @param max
1138
+ */
1139
+ private inRange(a: number, min: number, max: number) {
1140
+ return min <= a && a <= max;
1141
+ }
1142
+
1143
+ /**
1144
+ * @private
1145
+ *
1146
+ * @param n
1147
+ * @param d
1148
+ */
1149
+ private div(n: number, d: number) {
1150
+ return Math.floor(n / d);
1151
+ }
1152
+
1153
+ /**
1154
+ * @private
1155
+ *
1156
+ * @param string
1157
+ */
1158
+ private stringToCodePoints(str: string) {
1159
+ /** @type {Array.<number>} */
1160
+ let cps = [];
1161
+ // Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
1162
+ let i = 0, n = str.length;
1163
+ while (i < str.length) {
1164
+ let c = str.charCodeAt(i);
1165
+ if (!this.inRange(c, 0xD800, 0xDFFF)) {
1166
+ cps.push(c);
1167
+ } else if (this.inRange(c, 0xDC00, 0xDFFF)) {
1168
+ cps.push(0xFFFD);
1169
+ } else { // (inRange(c, 0xD800, 0xDBFF))
1170
+ if (i === n - 1) {
1171
+ cps.push(0xFFFD);
1172
+ } else {
1173
+ let d = str.charCodeAt(i + 1);
1174
+ if (this.inRange(d, 0xDC00, 0xDFFF)) {
1175
+ let a = c & 0x3FF;
1176
+ let b = d & 0x3FF;
1177
+ i += 1;
1178
+ cps.push(0x10000 + (a << 10) + b);
1179
+ } else {
1180
+ cps.push(0xFFFD);
1181
+ }
1182
+ }
1183
+ }
1184
+ i += 1;
1185
+ }
1186
+ return cps;
1187
+ }
1188
+ }