@@ -45,11 +45,12 @@ export class BitStream {
4545 private buffer : Array < number > ;
4646 private stream : MultiBufferStream | DataStream ;
4747 private state : State ;
48- private big_endian = true ; // results are returned Big Endian
48+ private endianness : Endianness ;
4949
50- constructor ( stream : MultiBufferStream | DataStream ) {
50+ constructor ( stream : MultiBufferStream | DataStream , endianness ?: Endianness ) {
5151 this . state = new State ( ) ;
5252 this . stream = stream ;
53+ this . endianness = endianness ? endianness : Endianness . BIG_ENDIAN ;
5354 }
5455
5556 appendUint8 ( count = 1 ) : void {
@@ -72,7 +73,8 @@ export class BitStream {
7273 return 0 ;
7374 }
7475 const bit : number =
75- ( this . buffer [ this . state . rbyte ] >> ( this . big_endian ? 7 - this . state . rbit : this . state . rbit ) ) &
76+ ( this . buffer [ this . state . rbyte ] >>
77+ ( this . endianness === Endianness . BIG_ENDIAN ? 7 - this . state . rbit : this . state . rbit ) ) &
7678 0x01 ;
7779 if ( ++ this . state . rbit > 7 ) {
7880 this . state . rbyte ++ ;
@@ -88,7 +90,8 @@ export class BitStream {
8890 return 0 ;
8991 }
9092 const bit : number =
91- ( this . buffer [ this . state . rbyte ] >> ( this . big_endian ? 7 - this . state . rbit : this . state . rbit ) ) &
93+ ( this . buffer [ this . state . rbyte ] >>
94+ ( this . endianness === Endianness . BIG_ENDIAN ? 7 - this . state . rbit : this . state . rbit ) ) &
9295 0x01 ;
9396 return bit ;
9497 }
@@ -125,7 +128,7 @@ export class BitStream {
125128 return ff ;
126129 } else {
127130 for ( res = 0 , i = 0 ; i < bytes ; i ++ ) {
128- if ( this . big_endian )
131+ if ( this . endianness === Endianness . BIG_ENDIAN )
129132 res =
130133 ( res << 8 ) +
131134 ( ( this . buffer [ this . state . rbyte ] << this . state . rbit ) |
@@ -147,63 +150,50 @@ export class BitStream {
147150 return this . rdb ( 1 ) ;
148151 }
149152
150- readUint16 ( ) {
151- return this . big_endian ? this . GetUInt16BE ( this . rdb ( 2 ) ) : this . GetUInt16LE ( this . rdb ( 2 ) ) ;
153+ readUint16 ( endianness ?: Endianness ) {
154+ return ( endianness ?? this . endianness ) === Endianness . BIG_ENDIAN
155+ ? this . GetUInt16BE ( this . rdb ( 2 ) )
156+ : this . GetUInt16LE ( this . rdb ( 2 ) ) ;
152157 }
153- private ByteSwap16 = function ( x : number ) : number {
154- return ( x << 8 ) | ( x >> 8 ) ;
155- } ;
156- private CondByteSwap16BE = function ( val : number ) : number {
157- return this . OSisLittleEndian ( ) ? this . _ByteSwap16 ( val ) : val ;
158- } ;
159- private CondByteSwap16LE = function ( val : number ) {
160- return this . OSisLittleEndian ( ) ? val : this . _ByteSwap16 ( val ) ;
161- } ;
162158 private GetUInt16BE = function ( val : number ) {
163- return this . _CondByteSwap16BE ( val ) ;
159+ return this . OSisLittleEndian ( ) ? this . ByteSwap16 ( val ) : val ;
164160 } ;
165161 private GetUInt16LE = function ( val : number ) {
166- return this . CondByteSwap16LE ( val ) ;
162+ return this . OSisLittleEndian ( ) ? val : this . ByteSwap16 ( val ) ;
163+ } ;
164+ private ByteSwap16 = function ( x : number ) : number {
165+ return ( x << 8 ) | ( x >> 8 ) ;
167166 } ;
168167
169- readUint24 ( ) {
170- return this . big_endian ? this . GetUInt24BE ( this . rdb ( 3 ) ) : this . GetUInt24LE ( this . rdb ( 3 ) ) ;
168+ readUint24 ( endianness ?: Endianness ) {
169+ return ( endianness ?? this . endianness ) === Endianness . BIG_ENDIAN
170+ ? this . GetUInt24BE ( this . rdb ( 3 ) )
171+ : this . GetUInt24LE ( this . rdb ( 3 ) ) ;
171172 }
172173
173174 private ByteSwap24 = function ( x : number ) {
174175 return ( ( x & 0xff0000 ) >> 16 ) | ( x & 0xff00 ) | ( x & ( 0xff << 16 ) ) ;
175176 } ;
176- private CondByteSwap24BE = function ( val : number ) {
177- return this . OSisLittleEndian ( ) ? this . ByteSwap24 ( val ) : val ;
178- } ;
179- private CondByteSwap24LE = function ( val : number ) {
180- return this . OSisLittleEndian ( ) ? val : this . _ByteSwap24 ( val ) ;
181- } ;
182177 private GetUInt24BE = function ( val : number ) {
183- return this . _CondByteSwap24BE ( val ) ;
178+ return this . OSisLittleEndian ( ) ? this . ByteSwap24 ( val ) : val ;
184179 } ;
185180 private GetUInt24LE = function ( val : number ) {
186- return this . _CondByteSwap24LE ( val ) ;
181+ return this . OSisLittleEndian ( ) ? val : this . ByteSwap24 ( val ) ;
187182 } ;
188183
189- readUint32 ( ) {
190- return this . big_endian ? this . GetUInt32BE ( this . rdb ( 4 ) ) : this . _GetUInt32LE ( this . rdb ( 4 ) ) ;
184+ readUint32 ( endianness ?: Endianness ) {
185+ return ( endianness ?? this . endianness ) === Endianness . BIG_ENDIAN
186+ ? this . GetUInt32BE ( this . rdb ( 4 ) )
187+ : this . GetUInt32LE ( this . rdb ( 4 ) ) ;
191188 }
192-
193- private _ByteSwap32 = function ( x : number ) {
189+ private ByteSwap32 = function ( x : number ) {
194190 return ( x << 24 ) | ( ( x << 8 ) & 0x00ff0000 ) | ( ( x >> 8 ) & 0x0000ff00 ) | ( x >> 24 ) ;
195191 } ;
196- private CondByteSwap32BE = function ( val : number ) {
197- return this . OSisLittleEndian ( ) ? this . _ByteSwap32 ( val ) : val ;
198- } ;
199- private CondByteSwap32LE = function ( val : number ) {
200- return this . OSisLittleEndian ( ) ? val : this . _ByteSwap32 ( val ) ;
201- } ;
202192 private GetUInt32BE = function ( val : number ) {
203- return this . CondByteSwap32BE ( val ) ;
193+ return this . OSisLittleEndian ( ) ? this . ByteSwap32 ( val ) : val ;
204194 } ;
205- private _GetUInt32LE = function ( val : number ) {
206- return this . CondByteSwap32LE ( val ) ;
195+ private GetUInt32LE = function ( val : number ) {
196+ return this . OSisLittleEndian ( ) ? val : this . ByteSwap32 ( val ) ;
207197 } ;
208198
209199 readBits ( bits : number ) : number {
@@ -216,7 +206,7 @@ export class BitStream {
216206 return 0 ;
217207 }
218208 let val = 0 ;
219- if ( this . big_endian ) {
209+ if ( this . endianness === Endianness . BIG_ENDIAN ) {
220210 // Read leading bits up to byte boundary
221211 while ( bits > 0 && this . state . rbit !== 0 ) {
222212 val = ( val << 1 ) | this . readBit ( ) ;
@@ -299,9 +289,9 @@ export class BitStream {
299289 while ( ! this . state . aligned ) this . skipBit ( ) ;
300290 }
301291
302- private OSisLittleEndian ( ) : boolean {
292+ private OSisLittleEndian = function ( ) : boolean {
303293 return this . stream . endianness === Endianness . LITTLE_ENDIAN ;
304- }
294+ } ;
305295
306296 currentReadByteOffset ( ) : number {
307297 return this . state . readByteOffset ( ) ;
0 commit comments