@@ -37,7 +37,7 @@ class BinaryStream {
3737 if ( buffer instanceof ArrayBuffer ) {
3838 this . view = new DataView ( buffer ) ;
3939 } else if ( buffer instanceof Uint8Array ) {
40- this . view = new DataView ( buffer . buffer ) ;
40+ this . view = new DataView ( buffer . buffer as ArrayBuffer ) ;
4141 } else {
4242 this . view = new DataView ( new Uint8Array ( buffer ) . buffer ) ;
4343 }
@@ -50,7 +50,7 @@ class BinaryStream {
5050 * @returns The DataView object.
5151 */
5252 getDataView ( ) : DataView {
53- return this . view ;
53+ return this . view as DataView ;
5454 }
5555
5656 /**
@@ -60,8 +60,8 @@ class BinaryStream {
6060 * @returns The updated BinaryStream instance.
6161 */
6262 resize ( size : number , canShrink : boolean = true ) : this {
63- if ( canShrink || ( ! canShrink && size > this . view . buffer . byteLength ) ) {
64- this . view = new DataView ( this . view . buffer . transfer ( size ) ) ;
63+ if ( canShrink || ( ! canShrink && size > this . length ) ) {
64+ this . view = new DataView ( this . arrayBuffer . transfer ( size ) ) ;
6565 }
6666 return this ;
6767 }
@@ -111,7 +111,7 @@ class BinaryStream {
111111 throw new RangeError ( `Buffer overrun, cannot read past the end of the buffer. Start: ${ start } , End: ${ end } , Length: ${ this . length } ` ) ;
112112 }
113113
114- return new Uint8Array ( this . view . buffer . slice ( start , end ) ) ;
114+ return new Uint8Array ( this . arrayBuffer . slice ( start , end ) ) ;
115115 }
116116
117117 /**
@@ -168,7 +168,7 @@ class BinaryStream {
168168 * @returns A copy of the ArrayBuffer.
169169 */
170170 get buffer ( ) : Uint8Array {
171- return new Uint8Array ( this . view . buffer ) ;
171+ return new Uint8Array ( this . arrayBuffer ) ;
172172 }
173173
174174 /**
@@ -180,7 +180,7 @@ class BinaryStream {
180180 }
181181
182182 get arrayBuffer ( ) : ArrayBuffer {
183- return this . view . buffer ;
183+ return this . view . buffer as ArrayBuffer ;
184184 }
185185
186186 /**
@@ -189,7 +189,7 @@ class BinaryStream {
189189 * @returns The created Blob object.
190190 */
191191 blob ( type : string = "application/octet-stream" ) : Blob {
192- return new Blob ( [ this . view . buffer ] , { type } ) ;
192+ return new Blob ( [ this . arrayBuffer ] , { type } ) ;
193193 }
194194
195195 /**
@@ -199,6 +199,8 @@ class BinaryStream {
199199 getRemainingBytes ( ) : number {
200200 return this . length - this . offset ;
201201 }
202+ /** @inheritdoc BinaryStream.getRemainingBytes */
203+ rest = this . getRemainingBytes ;
202204
203205 /**
204206 * Reads the remaining amount of bytes.
@@ -207,6 +209,8 @@ class BinaryStream {
207209 readRemainingBytes ( ) : Uint8Array {
208210 return this . read ( this . getRemainingBytes ( ) ) ;
209211 }
212+ /** @inheritdoc BinaryStream.readRemainingBytes */
213+ readRest = this . readRemainingBytes ;
210214
211215 /**
212216 * Reads a boolean value.
@@ -818,15 +822,19 @@ class BinaryStream {
818822 * @param offset - The optional offset at which to write the value.
819823 * @returns The updated BinaryStream instance.
820824 */
821- writeUVarInt ( v : number , offset : number = this . offset ) : this {
822- for ( let i = 0 ; i < 5 ; i ++ ) {
823- if ( ( v >> 7 ) !== 0 ) {
824- this . writeByte ( v | 0x80 , offset ++ ) ;
825- } else {
826- this . writeByte ( v & 0x7f , offset ++ ) ;
825+ writeUVarInt ( v : number , offset : number = - 1 ) : this {
826+ if ( offset >= 0 ) {
827+ this . offset = offset ;
828+ }
829+
830+ while ( true ) {
831+ if ( ( v & ~ 0x7f ) == 0 ) {
832+ this . writeByte ( v ) ;
827833 break ;
828834 }
829- v >>= 7 ;
835+
836+ this . writeByte ( ( v & 0x7f ) | 0x80 ) ;
837+ v >>>= 7 ;
830838 }
831839
832840 return this ;
@@ -997,7 +1005,7 @@ class BinaryStream {
9971005
9981006 default :
9991007 if ( encoding === "binary" ) encoding = "latin1" ;
1000- return new TextDecoder ( encoding ) . decode ( this . view . buffer ) ;
1008+ return new TextDecoder ( encoding ) . decode ( this . arrayBuffer ) ;
10011009 }
10021010
10031011 }
@@ -1010,7 +1018,7 @@ class BinaryStream {
10101018 split ( bytes : number ) : Uint8Array [ ] {
10111019 let buffers : ArrayBuffer [ ] = [ ] ;
10121020 for ( let i = 0 ; i < this . length ; i += bytes ) {
1013- buffers . push ( this . view . buffer . slice ( i , bytes ) ) ;
1021+ buffers . push ( this . arrayBuffer . slice ( i , i + bytes ) ) ;
10141022 }
10151023 return buffers . map ( buffer => new Uint8Array ( buffer ) ) ;
10161024 }
@@ -1050,22 +1058,6 @@ class BinaryStream {
10501058 throw new Error ( `Unsupported encoding: ${ encoding } ` ) ;
10511059 }
10521060 }
1053-
1054- /**
1055- * Writes a string to the buffer
1056- */
1057- writeString ( str : string , offset ?: number ) : this {
1058- let buffer = new TextEncoder ( ) . encode ( str ) ;
1059- return this . writeUInt32BE ( buffer . length , offset ) . write ( buffer ) ;
1060- }
1061-
1062- /**
1063- * Reads a string from the buffer
1064- */
1065- readString ( offset ?: number ) : string {
1066- let length = this . readUInt32BE ( offset ) ;
1067- return new TextDecoder ( ) . decode ( this . read ( length ) ) ;
1068- }
10691061}
10701062
10711063export {
0 commit comments