@@ -15,6 +15,12 @@ class BinaryStream {
1515 }
1616 }
1717
18+ /*
19+ *******************************
20+ * Stream Management Functions *
21+ *******************************
22+ */
23+
1824 /**
1925 * Read a set of bytes from the buffer
2026 * @param len {number}
@@ -42,6 +48,37 @@ class BinaryStream {
4248 this . offset = offset ;
4349 }
4450
51+ /**
52+ * Increase the stream's offset
53+ * @param v {number}
54+ * @param ret {boolean}
55+ * @return {number }
56+ */
57+ increaseOffset ( v , ret = false ) {
58+ return ( ret === true ? ( this . offset += v ) : ( this . offset += v ) - v ) ;
59+ }
60+
61+ /**
62+ * Append data to stream's buffer
63+ * @param buf {*}
64+ * @return {BinaryStream }
65+ */
66+ append ( buf ) {
67+ if ( buf instanceof Buffer ) {
68+ this . buffer = Buffer . concat ( [ this . buffer , buf ] ) ;
69+ this . offset += buf . length ;
70+ } else if ( typeof buf === "string" ) {
71+ buf = Buffer . from ( buf , "hex" ) ;
72+ this . buffer = Buffer . concat ( [ this . buffer , buf ] ) ;
73+ this . offset += buf . length ;
74+ } else if ( Array . isArray ( buf ) ) {
75+ buf = Buffer . from ( buf ) ;
76+ this . buffer = Buffer . concat ( [ this . buffer , buf ] ) ;
77+ this . offset += buf . length ;
78+ }
79+ return this ;
80+ }
81+
4582 /**
4683 * Get the read/write offset of the stream
4784 * @return {number }
@@ -66,6 +103,12 @@ class BinaryStream {
66103 return this . buffer . length ;
67104 }
68105
106+ /*
107+ *******************************
108+ * Buffer Management Functions *
109+ *******************************
110+ */
111+
69112 /**
70113 * Get the amount of remaining bytes that can be read
71114 * @return {number }
@@ -84,37 +127,6 @@ class BinaryStream {
84127 return buf ;
85128 }
86129
87- /**
88- * Increase the stream's offset
89- * @param v {number}
90- * @param ret {boolean}
91- * @return {number }
92- */
93- increaseOffset ( v , ret = false ) {
94- return ( ret === true ? ( this . offset += v ) : ( this . offset += v ) - v ) ;
95- }
96-
97- /**
98- * Append data to stream's buffer
99- * @param buf {*}
100- * @return {BinaryStream }
101- */
102- append ( buf ) {
103- if ( buf instanceof Buffer ) {
104- this . buffer = Buffer . concat ( [ this . buffer , buf ] ) ;
105- this . offset += buf . length ;
106- } else if ( typeof buf === "string" ) {
107- buf = Buffer . from ( buf , "hex" ) ;
108- this . buffer = Buffer . concat ( [ this . buffer , buf ] ) ;
109- this . offset += buf . length ;
110- } else if ( Array . isArray ( buf ) ) {
111- buf = Buffer . from ( buf ) ;
112- this . buffer = Buffer . concat ( [ this . buffer , buf ] ) ;
113- this . offset += buf . length ;
114- }
115- return this ;
116- }
117-
118130 /**
119131 * Reads a byte boolean
120132 * @return {boolean }
@@ -167,7 +179,7 @@ class BinaryStream {
167179 */
168180 writeShort ( v ) {
169181 let buf = Buffer . alloc ( 2 ) ;
170- buf . writeUInt16BE ( v ) ;
182+ buf . writeUInt16BE ( v , 0 ) ;
171183 this . append ( buf ) ;
172184
173185 return this ;
@@ -188,7 +200,7 @@ class BinaryStream {
188200 */
189201 writeSignedShort ( v ) {
190202 let buf = Buffer . alloc ( 2 ) ;
191- buf . writeInt16BE ( v ) ;
203+ buf . writeInt16BE ( v , 0 ) ;
192204 this . append ( buf ) ;
193205
194206 return this ;
@@ -209,7 +221,7 @@ class BinaryStream {
209221 */
210222 writeLShort ( v ) {
211223 let buf = Buffer . alloc ( 2 ) ;
212- buf . writeUInt16LE ( v ) ;
224+ buf . writeUInt16LE ( v , 0 ) ;
213225 this . append ( buf ) ;
214226
215227 return this ;
@@ -230,7 +242,7 @@ class BinaryStream {
230242 */
231243 writeSignedLShort ( v ) {
232244 let buf = Buffer . alloc ( 2 ) ;
233- buf . writeInt16LE ( v ) ;
245+ buf . writeInt16LE ( v , 0 ) ;
234246 this . append ( buf ) ;
235247
236248 return this ;
@@ -293,7 +305,7 @@ class BinaryStream {
293305 */
294306 writeInt ( v ) {
295307 let buf = Buffer . alloc ( 4 ) ;
296- buf . writeInt32BE ( v ) ;
308+ buf . writeInt32BE ( v , 0 ) ;
297309 this . append ( buf ) ;
298310
299311 return this ;
@@ -314,7 +326,7 @@ class BinaryStream {
314326 */
315327 writeLInt ( v ) {
316328 let buf = Buffer . alloc ( 4 ) ;
317- buf . writeInt32LE ( v ) ;
329+ buf . writeInt32LE ( v , 0 ) ;
318330 this . append ( buf ) ;
319331
320332 return this ;
@@ -341,7 +353,7 @@ class BinaryStream {
341353 */
342354 writeFloat ( v ) {
343355 let buf = Buffer . alloc ( 8 ) ;
344- let bytes = buf . writeFloatBE ( v ) ;
356+ let bytes = buf . writeFloatBE ( v , 0 ) ;
345357 this . append ( buf . slice ( 0 , bytes ) ) ;
346358
347359 return this ;
@@ -368,7 +380,7 @@ class BinaryStream {
368380 */
369381 writeLFloat ( v ) {
370382 let buf = Buffer . alloc ( 8 ) ;
371- let bytes = buf . writeFloatLE ( v ) ;
383+ let bytes = buf . writeFloatLE ( v , 0 ) ;
372384 this . append ( buf . slice ( 0 , bytes ) ) ;
373385
374386 return this ;
@@ -387,7 +399,7 @@ class BinaryStream {
387399 */
388400 writeDouble ( v ) {
389401 let buf = Buffer . alloc ( 8 ) ;
390- buf . writeDoubleBE ( v ) ;
402+ buf . writeDoubleBE ( v , 0 ) ;
391403 this . append ( buf ) ;
392404
393405 return this ;
@@ -406,7 +418,7 @@ class BinaryStream {
406418 */
407419 writeLDouble ( v ) {
408420 let buf = Buffer . alloc ( 8 ) ;
409- buf . writeDoubleLE ( v ) ;
421+ buf . writeDoubleLE ( v , 0 ) ;
410422 this . append ( buf ) ;
411423
412424 return this ;
@@ -611,7 +623,7 @@ class BinaryStream {
611623 * @return {BinaryStream }
612624 */
613625 writeString ( v ) {
614- this . append ( Buffer . from ( v ) ) ;
626+ this . append ( Buffer . alloc ( v . length ) . write ( v , "utf8" ) ) ;
615627 return this ;
616628 }
617629
0 commit comments