MCPcopy Create free account
hub / github.com/denoland/std / readFrom

Method readFrom

io/buffer.ts:428–450  ·  view source on GitHub ↗

* Reads data from `r` until EOF (`null`) and appends it to the buffer, * growing the buffer as needed. It resolves to the number of bytes read. * If the buffer becomes too large, `.readFrom()` will reject with an error. * * Based on Go Lang's * {@link https://golang.org/pkg/bytes/#Buf

(r: Reader)

Source from the content-addressed store, hash-verified

426 * @returns The number of bytes read.
427 */
428 async readFrom(r: Reader): Promise<number> {
429 let n = 0;
430 const tmp = new Uint8Array(MIN_READ);
431 while (true) {
432 const shouldGrow = this.capacity - this.length < MIN_READ;
433 // read into tmp buffer if there's not enough room
434 // otherwise read directly into the internal buffer
435 const buf = shouldGrow
436 ? tmp
437 : new Uint8Array(this.#buf.buffer, this.length);
438
439 const nread = await r.read(buf);
440 if (nread === null) {
441 return n;
442 }
443
444 // write will grow if needed
445 if (shouldGrow) this.writeSync(buf.subarray(0, nread));
446 else this.#reslice(this.length + nread);
447
448 n += nread;
449 }
450 }
451
452 /** Reads data from `r` until EOF (`null`) and appends it to the buffer,
453 * growing the buffer as needed. It returns the number of bytes read. If the

Callers 2

fnFunction · 0.95
buffer_test.tsFile · 0.80

Calls 3

writeSyncMethod · 0.95
#resliceMethod · 0.95
readMethod · 0.65

Tested by

no test coverage detected