diff options
| author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2018-12-17 22:57:45 -0500 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2018-12-17 22:57:45 -0500 |
| commit | 81933b0f04b865cab3b42a257b333f37ccc441b7 (patch) | |
| tree | 3fb268618ba4014a3433095de7e01f3f8cd12724 /bufio.ts | |
| parent | 6afc9dca3d6c550b859a7f6bede0ec9527b0ba34 (diff) | |
Add BufReader.readFull (denoland/deno_std#24)
Original: https://github.com/denoland/deno_std/commit/abeb19890e5d6a0cd3461c4aefc920006ef284bd
Diffstat (limited to 'bufio.ts')
| -rw-r--r-- | bufio.ts | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -162,6 +162,30 @@ export class BufReader implements Reader { return rr; } + /** reads exactly len(p) bytes into p. + * Ported from https://golang.org/pkg/io/#ReadFull + * It returns the number of bytes copied and an error if fewer bytes were read. + * The error is EOF only if no bytes were read. + * If an EOF happens after reading some but not all the bytes, + * readFull returns ErrUnexpectedEOF. ("EOF" for current impl) + * On return, n == len(p) if and only if err == nil. + * If r returns an error having read at least len(buf) bytes, + * the error is dropped. + */ + async readFull(p: Uint8Array): Promise<[number, BufState]> { + let rr = await this.read(p); + let nread = rr.nread; + if (rr.eof) { + return [nread, nread < p.length ? "EOF" : null]; + } + while (!rr.eof && nread < p.length) { + rr = await this.read(p.subarray(nread)); + nread += rr.nread; + } + return [nread, nread < p.length ? "EOF" : null]; + } + + /** Returns the next byte [0, 255] or -1 if EOF. */ async readByte(): Promise<number> { while (this.r === this.w) { |
