1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

Add BufReader.readFull (denoland/deno_std#24)

Original: abeb19890e
This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-12-17 22:57:45 -05:00 committed by Ryan Dahl
parent 6afc9dca3d
commit 81933b0f04
3 changed files with 47 additions and 1 deletions

View file

@ -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) {

View file

@ -321,3 +321,25 @@ test(async function bufioWriter() {
}
}
});
test(async function bufReaderReadFull() {
const enc = new TextEncoder();
const dec = new TextDecoder();
const text = "Hello World";
const data = new Buffer(enc.encode(text));
const bufr = new BufReader(data, 3);
{
const buf = new Uint8Array(6);
const [nread, err] = await bufr.readFull(buf);
assertEqual(nread, 6);
assert(!err);
assertEqual(dec.decode(buf), "Hello ");
}
{
const buf = new Uint8Array(6);
const [nread, err] = await bufr.readFull(buf);
assertEqual(nread, 5);
assertEqual(err, "EOF");
assertEqual(dec.decode(buf.subarray(0, 5)), "World");
}
});

View file

@ -10,7 +10,7 @@ const fileServer = run({
args: ["deno", "--allow-net", "file_server.ts", "."]
});
// I am also too lazy to do this properly LOL
runTests(new Promise(res => setTimeout(res, 1000)));
runTests(new Promise(res => setTimeout(res, 5000)));
(async () => {
await completePromise;
fileServer.close();