diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-10-10 05:31:23 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-10 05:31:23 -0400 |
commit | e7562eed8c816cd0d97aab6b818d7c8453dbaa2b (patch) | |
tree | c5a9f536e79d2c8d2d02897511a9138acaf35394 /std/io/ioutil.ts | |
parent | 3882c9d19a641e0c919f1350d87c6d7ee280cf78 (diff) | |
parent | 93f7f00c956c14620ef031626f124b57397ca867 (diff) |
Merge deno_std in main repo (#3091)
The history of deno_std is persevered but rewritten to update links to issues and PRs
Fixes denoland/deno_std#603
Diffstat (limited to 'std/io/ioutil.ts')
m--------- | std | 0 | ||||
-rw-r--r-- | std/io/ioutil.ts | 79 |
2 files changed, 79 insertions, 0 deletions
diff --git a/std b/std deleted file mode 160000 -Subproject 43aafbf33285753e7b42230f0eb7969b300f71c diff --git a/std/io/ioutil.ts b/std/io/ioutil.ts new file mode 100644 index 000000000..f1ca54e14 --- /dev/null +++ b/std/io/ioutil.ts @@ -0,0 +1,79 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { BufReader, UnexpectedEOFError } from "./bufio.ts"; +type Reader = Deno.Reader; +type Writer = Deno.Writer; +import { assert } from "../testing/asserts.ts"; + +/** copy N size at the most. + * If read size is lesser than N, then returns nread + * */ +export async function copyN( + dest: Writer, + r: Reader, + size: number +): Promise<number> { + let bytesRead = 0; + let buf = new Uint8Array(1024); + while (bytesRead < size) { + if (size - bytesRead < 1024) { + buf = new Uint8Array(size - bytesRead); + } + const result = await r.read(buf); + const nread = result === Deno.EOF ? 0 : result; + bytesRead += nread; + if (nread > 0) { + const n = await dest.write(buf.slice(0, nread)); + assert(n === nread, "could not write"); + } + if (result === Deno.EOF) { + break; + } + } + return bytesRead; +} + +/** Read big endian 16bit short from BufReader */ +export async function readShort(buf: BufReader): Promise<number | Deno.EOF> { + const high = await buf.readByte(); + if (high === Deno.EOF) return Deno.EOF; + const low = await buf.readByte(); + if (low === Deno.EOF) throw new UnexpectedEOFError(); + return (high << 8) | low; +} + +/** Read big endian 32bit integer from BufReader */ +export async function readInt(buf: BufReader): Promise<number | Deno.EOF> { + const high = await readShort(buf); + if (high === Deno.EOF) return Deno.EOF; + const low = await readShort(buf); + if (low === Deno.EOF) throw new UnexpectedEOFError(); + return (high << 16) | low; +} + +const MAX_SAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER); + +/** Read big endian 64bit long from BufReader */ +export async function readLong(buf: BufReader): Promise<number | Deno.EOF> { + const high = await readInt(buf); + if (high === Deno.EOF) return Deno.EOF; + const low = await readInt(buf); + if (low === Deno.EOF) throw new UnexpectedEOFError(); + const big = (BigInt(high) << 32n) | BigInt(low); + // We probably should provide a similar API that returns BigInt values. + if (big > MAX_SAFE_INTEGER) { + throw new RangeError( + "Long value too big to be represented as a Javascript number." + ); + } + return Number(big); +} + +/** Slice number into 64bit big endian byte array */ +export function sliceLongToBytes(d: number, dest = new Array(8)): number[] { + let big = BigInt(d); + for (let i = 0; i < 8; i++) { + dest[7 - i] = Number(big & 0xffn); + big >>= 8n; + } + return dest; +} |