@@ -258,7 +258,7 @@ export class DynamicBuffer {
258258 length ?: number ,
259259 encoding ?: BufferEncoding ,
260260 ) : number {
261- const count = this . writeString ( data , this . used , length , encoding ) ;
261+ const count = this . writeData ( data , this . used , length , encoding ) ;
262262 this . used += count ;
263263
264264 return count ;
@@ -790,7 +790,7 @@ export class DynamicBuffer {
790790
791791 this . buffer ?. copy ( this . buffer , lengthToWrite , 0 , this . used ) ;
792792
793- this . writeString ( data , 0 , lengthToWrite , encoding ) ;
793+ this . writeData ( data , 0 , lengthToWrite , encoding ) ;
794794
795795 this . used += lengthToWrite ;
796796
@@ -1711,7 +1711,7 @@ export class DynamicBuffer {
17111711 ) : number {
17121712 checkRange ( 'offset' , offset , 0 ) ;
17131713
1714- const count = this . writeString ( data , offset , length , encoding ) ;
1714+ const count = this . writeData ( data , offset , length , encoding ) ;
17151715 this . used = offset + count ;
17161716
17171717 return count ;
@@ -2343,22 +2343,24 @@ export class DynamicBuffer {
23432343 }
23442344
23452345 /**
2346- * Write a string into internal buffer with the specified offset.
2346+ * Write data into internal buffer with the specified offset, and the type of data should be one
2347+ * of string, Buffer, or Uint8Array.
23472348 *
2348- * @param data String to write to buffer.
2349+ * @param data Data to write to buffer.
23492350 * @param offset Number of bytes to skip before starting to write data.
2350- * @param length Maximum number of bytes to write, default the length of string.
2351- * @param encoding The character encoding to use, default from buffer encoding.
2351+ * @param length Maximum number of bytes to write, default the length of the data.
2352+ * @param encoding The character encoding to use if the data is a string, default from buffer
2353+ * encoding.
23522354 * @returns Number of bytes written.
23532355 */
2354- private writeString (
2355- data : string ,
2356+ private writeData (
2357+ data : string | Buffer | Int8Array ,
23562358 offset : number ,
23572359 length ?: number ,
23582360 encoding ?: BufferEncoding ,
2359- ) : number {
2360- if ( typeof data !== 'string' ) {
2361- throw new TypeError ( 'argument must be a string' ) ;
2361+ ) {
2362+ if ( typeof data !== 'string' && ! ( data instanceof Buffer ) && ! ( data instanceof Uint8Array ) ) {
2363+ throw new TypeError ( 'argument must be a string, Buffer, or a Int8Array ' ) ;
23622364 }
23632365
23642366 let lengthToWrite = data . length || 0 ;
@@ -2372,7 +2374,13 @@ export class DynamicBuffer {
23722374
23732375 this . ensureSize ( lengthToWrite + offset ) ;
23742376
2375- const count = this . buffer ?. write ( data , offset , lengthToWrite , encoding || this . encoding ) ;
2377+ let count : number | undefined = 0 ;
2378+ if ( typeof data === 'string' ) {
2379+ count = this . buffer ?. write ( data , offset , lengthToWrite , encoding || this . encoding ) ;
2380+ } else if ( this . buffer ) {
2381+ data . copy ( this . buffer , offset , 0 , length ) ;
2382+ count = length ;
2383+ }
23762384
23772385 return count || 0 ;
23782386 }
0 commit comments