* Discards all but the first `n` unread bytes from the buffer but * continues to use the same allocated storage. It throws if `n` is * negative or greater than the length of the buffer. * * @example Usage * ```ts * import { Buffer } from "@std/io/buffer"; * import { assertEquals
(n: number)
| 173 | * @param n The number of bytes to keep. |
| 174 | */ |
| 175 | truncate(n: number) { |
| 176 | if (n === 0) { |
| 177 | this.reset(); |
| 178 | return; |
| 179 | } |
| 180 | if (n < 0 || n > this.length) { |
| 181 | throw new Error("Buffer truncation out of range"); |
| 182 | } |
| 183 | this.#reslice(this.#off + n); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Resets the contents |