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

fix: Make std/io copyN write the whole read buffer #4978

Merged
merged 3 commits into from
Apr 29, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion std/io/ioutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export async function copyN(
const nread = result ?? 0;
bytesRead += nread;
if (nread > 0) {
const n = await dest.write(buf.slice(0, nread));
let n = 0;
while (n < nread) {
n += await dest.write(buf.slice(n, nread));
}
assert(n === nread, "could not write");
}
if (result === null) {
Expand Down
15 changes: 14 additions & 1 deletion std/io/ioutil_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
sliceLongToBytes,
} from "./ioutil.ts";
import { BufReader } from "./bufio.ts";
import { stringsReader } from "./util.ts";
import { stringsReader, tempFile } from "./util.ts";
import * as path from "../path/mod.ts";

class BinaryReader implements Reader {
index = 0;
Expand Down Expand Up @@ -85,3 +86,15 @@ Deno.test("testCopyN2", async function (): Promise<void> {
assertEquals(n, 10);
assertEquals(w.toString(), "abcdefghij");
});

Deno.test("copyNWriteAllData", async function (): Promise<void> {
const { filepath, file } = await tempFile(path.resolve("io"));
const size = 16 * 1024 + 1;
const data = "a".repeat(32 * 1024);
const r = stringsReader(data);
const n = await copyN(r, file, size); // Over max file possible buffer
file.close();
await Deno.remove(filepath);

assertEquals(n, size);
});