Reads data from `r` until EOF (`null`) and appends it to the buffer, * growing the buffer as needed. It returns the number of bytes read. If the * buffer becomes too large, `.readFromSync()` will throw an error. * * Based on Go Lang's * {@link https://golang.org/pkg/bytes/#Buffer.Read
(r: ReaderSync)
| 472 | * @returns The number of bytes read. |
| 473 | */ |
| 474 | readFromSync(r: ReaderSync): number { |
| 475 | let n = 0; |
| 476 | const tmp = new Uint8Array(MIN_READ); |
| 477 | while (true) { |
| 478 | const shouldGrow = this.capacity - this.length < MIN_READ; |
| 479 | // read into tmp buffer if there's not enough room |
| 480 | // otherwise read directly into the internal buffer |
| 481 | const buf = shouldGrow |
| 482 | ? tmp |
| 483 | : new Uint8Array(this.#buf.buffer, this.length); |
| 484 | |
| 485 | const nread = r.readSync(buf); |
| 486 | if (nread === null) { |
| 487 | return n; |
| 488 | } |
| 489 | |
| 490 | // write will grow if needed |
| 491 | if (shouldGrow) this.writeSync(buf.subarray(0, nread)); |
| 492 | else this.#reslice(this.length + nread); |
| 493 | |
| 494 | n += nread; |
| 495 | } |
| 496 | } |
| 497 | } |
no test coverage detected