feat: expose writeAll() and writeAllSync() (#2298)

Symmetric with `readAll()` and `readAllSync()`. Also used in `xeval`.
Also correct usage in `writeFile()`/`writeFileSync()`.
This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-07-23 08:16:39 -07:00 committed by Ryan Dahl
parent 70de8dd51d
commit e49d1e16ca
5 changed files with 46 additions and 17 deletions

View file

@ -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<void> {
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));
}
}