summaryrefslogtreecommitdiff
path: root/io/ioutil.ts
diff options
context:
space:
mode:
authorAndy Hayden <andyhayden1@gmail.com>2019-01-12 13:50:04 -0800
committerRyan Dahl <ry@tinyclouds.org>2019-01-12 16:50:04 -0500
commitf626b04ebe320a96a220af29595c6ca84cf9a10a (patch)
treeea059a796fed0650060398a8d347ae001d6f621c /io/ioutil.ts
parent7d6a0f64f20004f89f5e2cbfcf7941f0a8dafd21 (diff)
Reorgnanize repos, examples and tests (denoland/deno_std#105)
Original: https://github.com/denoland/deno_std/commit/c5e6e015b5be19027f60c19ca86283d12f9258f3
Diffstat (limited to 'io/ioutil.ts')
-rw-r--r--io/ioutil.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/io/ioutil.ts b/io/ioutil.ts
new file mode 100644
index 000000000..68d6e5190
--- /dev/null
+++ b/io/ioutil.ts
@@ -0,0 +1,36 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { BufReader } from "./bufio.ts";
+
+/* Read big endian 16bit short from BufReader */
+export async function readShort(buf: BufReader): Promise<number> {
+ const [high, low] = [await buf.readByte(), await buf.readByte()];
+ return (high << 8) | low;
+}
+
+/* Read big endian 32bit integer from BufReader */
+export async function readInt(buf: BufReader): Promise<number> {
+ const [high, low] = [await readShort(buf), await readShort(buf)];
+ return (high << 16) | low;
+}
+
+const BIT32 = 0xffffffff;
+/* Read big endian 64bit long from BufReader */
+export async function readLong(buf: BufReader): Promise<number> {
+ const [high, low] = [await readInt(buf), await readInt(buf)];
+ // ECMAScript doesn't support 64bit bit ops.
+ return high ? high * (BIT32 + 1) + low : low;
+}
+
+/* Slice number into 64bit big endian byte array */
+export function sliceLongToBytes(d: number, dest = new Array(8)): number[] {
+ let mask = 0xff;
+ let low = (d << 32) >>> 32;
+ let high = (d - low) / (BIT32 + 1);
+ let shift = 24;
+ for (let i = 0; i < 4; i++) {
+ dest[i] = (high >>> shift) & mask;
+ dest[i + 4] = (low >>> shift) & mask;
+ shift -= 8;
+ }
+ return dest;
+}