From e49d1e16ca2fca45e959c1add9b5a1d6866dbb90 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Tue, 23 Jul 2019 08:16:39 -0700 Subject: feat: expose writeAll() and writeAllSync() (#2298) Symmetric with `readAll()` and `readAllSync()`. Also used in `xeval`. Also correct usage in `writeFile()`/`writeFileSync()`. --- js/buffer.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'js/buffer.ts') diff --git a/js/buffer.ts b/js/buffer.ts index 1f597282d..9525e6954 100644 --- a/js/buffer.ts +++ b/js/buffer.ts @@ -274,3 +274,21 @@ export function readAllSync(r: SyncReader): Uint8Array { buf.readFromSync(r); return buf.bytes(); } + +/** Write all the content of `arr` to `w`. + */ +export async function writeAll(w: Writer, arr: Uint8Array): Promise { + let nwritten = 0; + while (nwritten < arr.length) { + nwritten += await w.write(arr.subarray(nwritten)); + } +} + +/** Write synchronously all the content of `arr` to `w`. + */ +export function writeAllSync(w: SyncWriter, arr: Uint8Array): void { + let nwritten = 0; + while (nwritten < arr.length) { + nwritten += w.writeSync(arr.subarray(nwritten)); + } +} -- cgit v1.2.3