-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathioutil.ts
37 lines (33 loc) · 906 Bytes
/
ioutil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright 2018 Yusuke Sakurai. All rights reserved. MIT license.
import {BufReader} from "https://deno.land/x/net/bufio.ts";
export async function readShort(buf: BufReader) {
const [high, low] = [
await buf.readByte(),
await buf.readByte()
];
return (high << 8) | low;
}
export async function readInt(buf: BufReader) {
const [high, low] = [
await readShort(buf),
await readShort(buf)
];
return (high << 16) | low
}
export async function readLong(buf: BufReader) {
const [high, low] = [
await readInt(buf),
await readInt(buf)
];
return (high << 32) | low
}
export function sliceLongToBytes(d: number, dest = new Array(8)): number[] {
let mask = 0xff;
let shift = 58;
for (let i = 0; i < 8; i++) {
dest[i] = (d >>> shift) & mask;
shift -= 8;
mask >>>= 8;
}
return dest;
}