提交 e749b37b 编写于 作者: D DanSnow 提交者: Ryan Dahl

Add deno.readAll() (#1234)

上级 122ccce8
......@@ -221,3 +221,11 @@ export class Buffer implements Reader, Writer {
}
}
}
/** Read `r` until EOF and return the content as `Uint8Array`.
*/
export async function readAll(r: Reader): Promise<Uint8Array> {
const buf = new Buffer();
await buf.readFrom(r);
return buf.bytes();
}
import { Buffer, readAll } from "deno";
// This code has been ported almost directly from Go's src/bytes/buffer_test.go
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE
import { test, assert, assertEqual } from "./test_util.ts";
import { Buffer } from "deno";
import { assert, assertEqual, test } from "./test_util.ts";
// N controls how many iterations of certain checks are performed.
const N = 100;
......@@ -193,3 +193,13 @@ test(async function bufferTestGrow() {
}
}
});
test(async function testReadAll() {
init();
const reader = new Buffer(testBytes.buffer as ArrayBuffer);
const actualBytes = await readAll(reader);
assertEqual(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEqual(testBytes[i], actualBytes[i]);
}
});
......@@ -20,7 +20,7 @@ export {
ReadWriteCloser,
ReadWriteSeeker
} from "./io";
export { Buffer } from "./buffer";
export { Buffer, readAll } from "./buffer";
export { mkdirSync, mkdir } from "./mkdir";
export { makeTempDirSync, makeTempDir } from "./make_temp_dir";
export { chmodSync, chmod } from "./chmod";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册