提交 81933b0f 编写于 作者: K Kevin (Kun) "Kassimo" Qian 提交者: Ryan Dahl

Add BufReader.readFull (denoland/deno_std#24)


Original: https://github.com/denoland/deno_std/commit/abeb19890e5d6a0cd3461c4aefc920006ef284bd
上级 6afc9dca
......@@ -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) {
......
......@@ -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");
}
});
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册