mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
c85b1c06a9
Original: b04fda30c8
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
import { BufReader, EOF, 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 { nread, eof } = await r.read(buf);
|
|
bytesRead += nread;
|
|
if (nread > 0) {
|
|
const n = await dest.write(buf.slice(0, nread));
|
|
assert(n === nread, "could not write");
|
|
}
|
|
if (eof) {
|
|
break;
|
|
}
|
|
}
|
|
return bytesRead;
|
|
}
|
|
|
|
/** Read big endian 16bit short from BufReader */
|
|
export async function readShort(buf: BufReader): Promise<number | EOF> {
|
|
const high = await buf.readByte();
|
|
if (high === EOF) return EOF;
|
|
const low = await buf.readByte();
|
|
if (low === EOF) throw new UnexpectedEOFError();
|
|
return (high << 8) | low;
|
|
}
|
|
|
|
/** Read big endian 32bit integer from BufReader */
|
|
export async function readInt(buf: BufReader): Promise<number | EOF> {
|
|
const high = await readShort(buf);
|
|
if (high === EOF) return EOF;
|
|
const low = await readShort(buf);
|
|
if (low === 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 | EOF> {
|
|
const high = await readInt(buf);
|
|
if (high === EOF) return EOF;
|
|
const low = await readInt(buf);
|
|
if (low === 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;
|
|
}
|