ByteArray.ts 39 KB

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