diff options
author | Chris Knight <cknight1234@gmail.com> | 2020-04-17 21:12:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-17 16:12:14 -0400 |
commit | 957050cd0235c40ccf70c3b253273e0ca5488490 (patch) | |
tree | e9c4a241d759c3d34b986180b7eb34032d33ced5 /std/io/util.ts | |
parent | 41f836dc915cfc0902fa911be06c3a62bef68222 (diff) |
feature: synchronous buffered writer (#4693)
Diffstat (limited to 'std/io/util.ts')
-rw-r--r-- | std/io/util.ts | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/std/io/util.ts b/std/io/util.ts index 4479e2c38..28688ae91 100644 --- a/std/io/util.ts +++ b/std/io/util.ts @@ -5,14 +5,20 @@ type Reader = Deno.Reader; import * as path from "../path/mod.ts"; import { encode } from "../encoding/utf8.ts"; -// `off` is the offset into `dst` where it will at which to begin writing values -// from `src`. -// Returns the number of bytes copied. +/** + * Copy bytes from one Uint8Array to another. Bytes from `src` which don't fit + * into `dst` will not be copied. + * + * @param dst Destination byte array + * @param src Source byte array + * @param off Offset into `dst` at which to begin writing values from `src`. + * @return number of bytes copied + */ export function copyBytes(dst: Uint8Array, src: Uint8Array, off = 0): number { off = Math.max(0, Math.min(off, dst.byteLength)); - const r = dst.byteLength - off; - if (src.byteLength > r) { - src = src.subarray(0, r); + const dstBytesAvailable = dst.byteLength - off; + if (src.byteLength > dstBytesAvailable) { + src = src.subarray(0, dstBytesAvailable); } dst.set(src, off); return src.byteLength; |