* Reads the next `p.length` bytes from the buffer or until the buffer is * drained. Returns the number of bytes read. If the buffer has no data to * return, the return is EOF (`null`). * * @example Usage * ```ts * import { Buffer } from "@std/io/buffer"; * import { assertEquals
(p: Uint8Array)
| 242 | * @returns The number of bytes read. |
| 243 | */ |
| 244 | readSync(p: Uint8Array): number | null { |
| 245 | if (this.empty()) { |
| 246 | // Buffer is empty, reset to recover space. |
| 247 | this.reset(); |
| 248 | if (p.byteLength === 0) { |
| 249 | // this edge case is tested in 'bufferReadEmptyAtEOF' test |
| 250 | return 0; |
| 251 | } |
| 252 | return null; |
| 253 | } |
| 254 | const nread = copy(this.#buf.subarray(this.#off), p); |
| 255 | this.#off += nread; |
| 256 | return nread; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Reads the next `p.length` bytes from the buffer or until the buffer is |