Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions js/buffer.ts
Expand Up @@ -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));
}
}
24 changes: 23 additions & 1 deletion js/buffer_test.ts
Expand Up @@ -5,7 +5,7 @@
// https://github.com/golang/go/blob/master/LICENSE
import { assertEquals, test } from "./test_util.ts";

const { Buffer, readAll, readAllSync } = Deno;
const { Buffer, readAll, readAllSync, writeAll, writeAllSync } = Deno;
type Buffer = Deno.Buffer;

// N controls how many iterations of certain checks are performed.
Expand Down Expand Up @@ -253,3 +253,25 @@ test(function testReadAllSync(): void {
assertEquals(testBytes[i], actualBytes[i]);
}
});

test(async function testWriteAll(): Promise<void> {
init();
const writer = new Buffer();
await writeAll(writer, testBytes);
const actualBytes = writer.bytes();
assertEquals(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEquals(testBytes[i], actualBytes[i]);
}
});

test(function testWriteAllSync(): void {
init();
const writer = new Buffer();
writeAllSync(writer, testBytes);
const actualBytes = writer.bytes();
assertEquals(testBytes.byteLength, actualBytes.byteLength);
for (let i = 0; i < testBytes.length; ++i) {
assertEquals(testBytes[i], actualBytes[i]);
}
});
2 changes: 1 addition & 1 deletion js/deno.ts
Expand Up @@ -38,7 +38,7 @@ export {
ReadWriteCloser,
ReadWriteSeeker
} from "./io";
export { Buffer, readAll, readAllSync } from "./buffer";
export { Buffer, readAll, readAllSync, writeAll, writeAllSync } from "./buffer";
export { mkdirSync, mkdir } from "./mkdir";
export {
makeTempDirSync,
Expand Down
5 changes: 3 additions & 2 deletions js/write_file.ts
Expand Up @@ -2,6 +2,7 @@
import { stat, statSync } from "./stat";
import { open, openSync } from "./files";
import { chmod, chmodSync } from "./chmod";
import { writeAll, writeAllSync } from "./buffer";

/** Options for writing to a file.
* `perm` would change the file's permission if set.
Expand Down Expand Up @@ -40,7 +41,7 @@ export function writeFileSync(
chmodSync(filename, options.perm);
}

file.writeSync(data);
writeAllSync(file, data);
file.close();
}

Expand Down Expand Up @@ -70,6 +71,6 @@ export async function writeFile(
await chmod(filename, options.perm);
}

await file.write(data);
await writeAll(file, data);
file.close();
}
14 changes: 1 addition & 13 deletions js/xeval.ts
@@ -1,22 +1,10 @@
import { Buffer } from "./buffer";
import { Buffer, writeAll } from "./buffer";
import { stdin } from "./files";
import { TextEncoder, TextDecoder } from "./text_encoding";
import { Reader, EOF } from "./io";

export type XevalFunc = (v: string) => void;

async function writeAll(buffer: Buffer, arr: Uint8Array): Promise<void> {
let bytesWritten = 0;
while (bytesWritten < arr.length) {
try {
const nwritten = await buffer.write(arr.subarray(bytesWritten));
bytesWritten += nwritten;
} catch {
return;
}
}
}

// TODO(kevinkassimo): Move this utility to deno_std.
// Import from there once doable.
// Read from reader until EOF and emit string chunks separated
Expand Down