11// tslint:disable: jsdoc-format
22/**
3- *** Copyright (c) 2016-present,
4- *** Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. All rights reserved.
5- ***
6- *** This file is part of Catapult.
7- ***
8- *** Catapult is free software: you can redistribute it and/or modify
9- *** it under the terms of the GNU Lesser General Public License as published by
10- *** the Free Software Foundation, either version 3 of the License, or
11- *** (at your option) any later version.
12- ***
13- *** Catapult is distributed in the hope that it will be useful,
14- *** but WITHOUT ANY WARRANTY; without even the implied warranty of
15- *** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16- *** GNU Lesser General Public License for more details.
17- ***
18- *** You should have received a copy of the GNU Lesser General Public License
19- *** along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20- **/
3+ *** Copyright (c) 2016-present,
4+ *** Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp. All rights reserved.
5+ ***
6+ *** This file is part of Catapult.
7+ ***
8+ *** Catapult is free software: you can redistribute it and/or modify
9+ *** it under the terms of the GNU Lesser General Public License as published by
10+ *** the Free Software Foundation, either version 3 of the License, or
11+ *** (at your option) any later version.
12+ ***
13+ *** Catapult is distributed in the hope that it will be useful,
14+ *** but WITHOUT ANY WARRANTY; without even the implied warranty of
15+ *** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+ *** GNU Lesser General Public License for more details.
17+ ***
18+ *** You should have received a copy of the GNU Lesser General Public License
19+ *** along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20+ **/
2121
2222/**
2323 * Generator utility class.
2424 */
2525export class GeneratorUtils {
2626
27- /**
27+ /**
2828 * Converts a (64bit) uint8 array into a number array.
2929 * @param {Uint8Array } input A uint8 array.
3030 * @returns {number[] } The uint64 representation of the input.
3131 */
32- public static bufferToUint64 ( input : Uint8Array ) : number [ ] {
32+ public static bufferToUint64 ( input : Uint8Array ) : number [ ] {
3333 if ( 8 !== input . length ) {
3434 throw Error ( `byte array has unexpected size '${ input . length } '` ) ;
35- }
36- input = input . reverse ( ) ;
37- return [ GeneratorUtils . readUint32At ( input , 0 ) , GeneratorUtils . readUint32At ( input , 4 ) ] ;
38- }
39-
40- /**
35+ }
36+ input = input . reverse ( ) ;
37+ const view = new DataView ( input . buffer ) ;
38+ return [ view . getUint32 ( 4 ) , view . getUint32 ( 0 ) ] ;
39+ }
40+
41+ /**
4142 * Read buffer into 32bits integer at given index.
4243 * @param {Uint8Array } bytes A uint8 array.
43- * @param {number } index Index.
44+ * @param {number } index Index.
4445 * @returns {number } 32bits integer.
4546 */
46- public static readUint32At ( bytes : Uint8Array , index : number ) : number {
47- return ( bytes [ index ] + ( bytes [ index + 1 ] << 8 ) + ( bytes [ index + 2 ] << 16 ) + ( bytes [ index + 3 ] << 24 ) ) >>> 0 ;
48- }
47+ public static readUint32At ( bytes : Uint8Array , index : number ) : number {
48+ return ( bytes [ index ] + ( bytes [ index + 1 ] << 8 ) + ( bytes [ index + 2 ] << 16 ) + ( bytes [ index + 3 ] << 24 ) ) >>> 0 ;
49+ }
4950
50- /**
51+ /**
5152 * Write uint to buffer
5253 * @param {number } uintValue A uint8 array.
53- * @param {number } bufferSize Buffer size.
54+ * @param {number } bufferSize Buffer size.
5455 * @returns {Uint8Array }
5556 */
56- public static uintToBuffer ( uintValue : number , bufferSize : number ) : Uint8Array {
57- const buffer = new ArrayBuffer ( bufferSize ) ;
58- const dataView = new DataView ( buffer ) ;
59- if ( 1 === bufferSize )
60- dataView . setUint8 ( 0 , uintValue ) ;
61-
62- else if ( 2 === bufferSize )
63- dataView . setUint16 ( 0 , uintValue , true ) ;
64-
65- else if ( 4 === bufferSize )
66- dataView . setUint32 ( 0 , uintValue , true ) ;
67-
68- else
69- throw new Error ( 'Unexpected bufferSize' ) ;
70-
71- return new Uint8Array ( buffer ) ;
72- }
57+ public static uintToBuffer ( uintValue : number , bufferSize : number ) : Uint8Array {
58+ const buffer = new ArrayBuffer ( bufferSize ) ;
59+ const dataView = new DataView ( buffer ) ;
60+ if ( 1 === bufferSize ) {
61+ dataView . setUint8 ( 0 , uintValue ) ;
62+ } else if ( 2 === bufferSize ) {
63+ dataView . setUint16 ( 0 , uintValue , true ) ;
64+ } else if ( 4 === bufferSize ) {
65+ dataView . setUint32 ( 0 , uintValue , true ) ;
66+ } else {
67+ throw new Error ( 'Unexpected bufferSize' ) ;
68+ }
69+
70+ return new Uint8Array ( buffer ) ;
71+ }
7372
74- /**
73+ /**
7574 * Write uint to buffer
7675 * @param {Uint8Array } buffer A uint8 array.
7776 * @returns {number }
7877 */
79- public static bufferToUint ( buffer : Uint8Array ) : number {
80- const dataView = new DataView ( buffer . buffer ) ;
81- if ( 1 === buffer . byteLength )
82- return dataView . getUint8 ( 0 ) ;
83-
84- else if ( 2 === buffer . byteLength )
85- return dataView . getUint16 ( 0 , true ) ;
86-
87- else if ( 4 === buffer . byteLength )
88- return dataView . getUint32 ( 0 , true ) ;
89-
90- throw new Error ( 'Unexpected buffer size' ) ;
91- } ;
78+ public static bufferToUint ( buffer : Uint8Array ) : number {
79+ const dataView = new DataView ( buffer . buffer ) ;
80+ if ( 1 === buffer . byteLength ) {
81+ return dataView . getUint8 ( 0 ) ;
82+ } else if ( 2 === buffer . byteLength ) {
83+ return dataView . getUint16 ( 0 , true ) ;
84+ } else if ( 4 === buffer . byteLength ) {
85+ return dataView . getUint32 ( 0 , true ) ;
86+ }
9287
93- /**
88+ throw new Error ( 'Unexpected buffer size' ) ;
89+ }
90+
91+ /**
9492 * Write Uint64 to buffer
9593 * @param {number } uintValue Uint64 (number[]).
9694 * @returns {Uint8Array }
9795 */
98- public static uint64ToBuffer ( uintValue : number [ ] ) : Uint8Array {
99- const uint32Array = new Uint32Array ( uintValue ) ;
100- return new Uint8Array ( uint32Array . buffer ) ;
101- }
96+ public static uint64ToBuffer ( uintValue : number [ ] ) : Uint8Array {
97+ const uint32Array = new Uint32Array ( uintValue ) ;
98+ return new Uint8Array ( uint32Array . buffer ) ;
99+ }
102100
103- /**
101+ /**
104102 * Concatenate two arrays
105103 * @param {Uint8Array } array1 A Uint8Array.
106- * @param {Uint8Array } array2 A Uint8Array.
104+ * @param {Uint8Array } array2 A Uint8Array.
107105 * @returns {Uint8Array }
108106 */
109- public static concatTypedArrays ( array1 : Uint8Array , array2 : Uint8Array ) : Uint8Array {
110- const newArray = new Uint8Array ( array1 . length + array2 . length ) ;
111- newArray . set ( array1 ) ;
112- newArray . set ( array2 , array1 . length ) ;
113- return newArray ;
114- }
107+ public static concatTypedArrays ( array1 : Uint8Array , array2 : Uint8Array ) : Uint8Array {
108+ const newArray = new Uint8Array ( array1 . length + array2 . length ) ;
109+ newArray . set ( array1 ) ;
110+ newArray . set ( array2 , array1 . length ) ;
111+ return newArray ;
112+ }
115113
116- /** Converts an unsigned byte to a signed byte with the same binary representation.
114+ /** Converts an unsigned byte to a signed byte with the same binary representation.
117115 * @param {number } input An unsigned byte.
118116 * @returns {number } A signed byte with the same binary representation as the input.
119117 *
@@ -125,17 +123,17 @@ export class GeneratorUtils {
125123 return input << 24 >> 24 ;
126124 }
127125
128- /** Get bytes by given sub array size.
129- * @param {Uint8Array } binary Binary bytes array.
126+ /** Get bytes by given sub array size.
127+ * @param {Uint8Array } binary Binary bytes array.
130128 * @param {number } size Subarray size.
131- * @returns {Uint8Array }
129+ * @returns {Uint8Array }
132130 *
133131 */
134- public static getBytes ( binary : Uint8Array , size : number ) : Uint8Array {
135- if ( size > binary . length )
136- throw new RangeError ( ) ;
137-
138- const bytes = binary . slice ( 0 , size ) ;
139- return bytes ;
140- }
132+ public static getBytes ( binary : Uint8Array , size : number ) : Uint8Array {
133+ if ( size > binary . length ) {
134+ throw new RangeError ( ) ;
135+ }
136+ const bytes = binary . slice ( 0 , size ) ;
137+ return bytes ;
138+ }
141139}
0 commit comments