@@ -75,7 +75,7 @@ export class DynamicBuffer {
7575 /**
7676 * A value to pre-fill the new buffer with.
7777 */
78- private fill ?: string | Buffer | number ;
78+ private fillVal ?: string | Buffer | number ;
7979
8080 /**
8181 * The current size of the buffer.
@@ -175,7 +175,7 @@ export class DynamicBuffer {
175175 }
176176
177177 this . used = 0 ;
178- this . fill = options ?. fill || 0 ;
178+ this . fillVal = options ?. fill || 0 ;
179179 this . encoding = options ?. encoding || 'utf8' ;
180180 this . factor = options ?. factor || this . DefaultResizeFactor ;
181181
@@ -184,7 +184,7 @@ export class DynamicBuffer {
184184 }
185185
186186 if ( this . size > 0 ) {
187- this . buffer = Buffer . alloc ( this . size , this . fill , this . encoding ) ;
187+ this . buffer = Buffer . alloc ( this . size , this . fillVal , this . encoding ) ;
188188 }
189189
190190 if ( data ) {
@@ -428,6 +428,41 @@ export class DynamicBuffer {
428428 return this . compare ( otherBuffer , 0 , otherBuffer . length , 0 , this . length ) === 0 ;
429429 }
430430
431+ /**
432+ * Fills this buffer with the specified value, and the entire buffer will be filled if the offset
433+ * and end are not given.
434+ *
435+ * ```js
436+ * const buf = new DynamicBuffer('hello');
437+ * buf.fill('x');
438+ * console.log(buf.toString());
439+ * // xxxxx
440+ * ```
441+ *
442+ * @param value The value with which to be fill this buffer.
443+ * @param offset Number of bytes to skip before starting to fill this buffer, default 0.
444+ * @param end Where to stop filling this buffer (not inclusive), default `buf.length`.
445+ * @param encoding The encoding for value if value is a string, default `'utf8'`.
446+ * @returns A reference to this buffer.
447+ */
448+ fill (
449+ value : string | Buffer | Uint8Array | number ,
450+ offset : number = 0 ,
451+ end : number = this . length ,
452+ encoding : BufferEncoding = 'utf8' ,
453+ ) : DynamicBuffer {
454+ if ( ! this . buffer || this . length === 0 || end - offset <= 0 ) {
455+ return this ;
456+ }
457+
458+ rangeCheck ( 'offset' , offset , 0 , this . length - 1 ) ;
459+ rangeCheck ( 'end' , end , 0 , this . length ) ;
460+
461+ this . buffer . fill ( value , offset , end , encoding ) ;
462+
463+ return this ;
464+ }
465+
431466 /**
432467 * Returns a boolean value to indicate whether this buffer includes a certain value among it.
433468 *
@@ -740,7 +775,7 @@ export class DynamicBuffer {
740775 return Buffer . alloc ( 0 ) ;
741776 }
742777
743- const newBuffer = Buffer . alloc ( endOffset - startOffset , this . fill , this . encoding ) ;
778+ const newBuffer = Buffer . alloc ( endOffset - startOffset , this . fillVal , this . encoding ) ;
744779 this . buffer . copy ( newBuffer , 0 , startOffset , endOffset ) ;
745780
746781 return newBuffer ;
@@ -985,7 +1020,7 @@ export class DynamicBuffer {
9851020 * @param newSize The size of new buffer.
9861021 */
9871022 private resize ( newSize : number ) : void {
988- const newBuffer = Buffer . alloc ( newSize , this . fill , this . encoding ) ;
1023+ const newBuffer = Buffer . alloc ( newSize , this . fillVal , this . encoding ) ;
9891024
9901025 if ( this . buffer && this . used > 0 ) {
9911026 this . buffer . copy ( newBuffer , 0 , 0 , this . used ) ;
0 commit comments