@@ -9,7 +9,7 @@ export class Bufferfish {
99 private inner : Uint8Array
1010 private position : number
1111 private reading : boolean
12- private capacity : number
12+ private max_capacity : number
1313
1414 private textDecoder : TextDecoder | undefined
1515 private textEncoder : TextEncoder | undefined
@@ -18,7 +18,7 @@ export class Bufferfish {
1818 this . inner = new Uint8Array ( bf )
1919 this . position = 0
2020 this . reading = false
21- this . capacity = 1024
21+ this . max_capacity = 1024
2222
2323 this . textDecoder = undefined
2424 this . textEncoder = undefined
@@ -32,12 +32,12 @@ export class Bufferfish {
3232 */
3333 private write ( bf : Uint8Array ) : number | Error {
3434 if (
35- this . capacity > 0 &&
36- ( bf . length > this . capacity ||
37- this . inner . length + bf . length > this . capacity )
35+ this . max_capacity > 0 &&
36+ ( bf . length > this . max_capacity ||
37+ this . inner . length + bf . length > this . max_capacity )
3838 ) {
3939 return new Error (
40- `Bufferfish capacity exceeded (${ this . capacity } bytes)` ,
40+ `Bufferfish capacity exceeded (${ this . max_capacity } bytes)` ,
4141 )
4242 }
4343
@@ -80,7 +80,42 @@ export class Bufferfish {
8080 * A value of 0 will allow the buffer to grow indefinitely.
8181 */
8282 public setMaxCapacity ( capacity : number ) : void {
83- this . capacity = capacity
83+ this . max_capacity = capacity
84+ }
85+
86+ /**
87+ * Returns true if the buffer is empty.
88+ */
89+ public isEmpty = ( ) : boolean => {
90+ return this . inner . length === 0
91+ }
92+
93+ /**
94+ * Returns the current length (in bytes) of the buffer.
95+ */
96+ public length = ( ) : number => {
97+ return this . inner . length
98+ }
99+
100+ /**
101+ * Clears the buffer and resets the cursor to the start position.
102+ */
103+ public clear = ( ) : void => {
104+ this . inner = new Uint8Array ( 0 )
105+ this . position = 0
106+ this . reading = false
107+ }
108+
109+ /**
110+ * Resizes the internal buffer to the specified size (in bytes).
111+ * This resets the buffer state and clears any existing data.
112+ */
113+ public truncate = ( length : number ) : void => {
114+ this . clear ( )
115+ this . inner = this . inner . subarray ( 0 , length )
116+ this . position = 0
117+ this . max_capacity = length
118+ this . reading = false
84119 }
85120
86121 /**
@@ -95,7 +130,7 @@ export class Bufferfish {
95130
96131 if ( this . position >= this . inner . length || value === undefined ) {
97132 return new Error (
98- `peek of 1 byte exceeds the max capacity of ${ this . capacity } bytes on this Bufferfish` ,
133+ `peek of 1 byte exceeds the max capacity of ${ this . max_capacity } bytes on this Bufferfish` ,
99134 )
100135 }
101136
@@ -113,7 +148,7 @@ export class Bufferfish {
113148
114149 if ( this . position + n > this . inner . length ) {
115150 return new Error (
116- `peek of ${ n } bytes exceeds the max capacity of ${ this . capacity } bytes on this Bufferfish` ,
151+ `peek of ${ n } bytes exceeds the max capacity of ${ this . max_capacity } bytes on this Bufferfish` ,
117152 )
118153 }
119154
0 commit comments