2024-01-12 07:34:12 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// This module is browser compatible.
|
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { concat } from "@std/bytes/concat";
|
2024-01-12 07:34:12 +00:00
|
|
|
import { DEFAULT_CHUNK_SIZE } from "./_constants.ts";
|
|
|
|
import type { Reader, ReaderSync } from "./types.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read {@linkcode Reader} `r` until EOF (`null`) and resolve to the content as
|
|
|
|
* {@linkcode Uint8Array}.
|
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { readAll } from "@std/io/read-all";
|
2024-01-12 07:34:12 +00:00
|
|
|
*
|
|
|
|
* // Example from stdin
|
|
|
|
* const stdinContent = await readAll(Deno.stdin);
|
|
|
|
*
|
|
|
|
* // Example from file
|
2024-01-14 21:35:50 +00:00
|
|
|
* using file = await Deno.open("my_file.txt", {read: true});
|
2024-01-12 07:34:12 +00:00
|
|
|
* const myFileContent = await readAll(file);
|
|
|
|
* ```
|
2024-08-08 14:20:43 +00:00
|
|
|
*
|
|
|
|
* @param reader The reader to read from
|
|
|
|
* @returns The content as Uint8Array
|
2024-01-12 07:34:12 +00:00
|
|
|
*/
|
|
|
|
export async function readAll(reader: Reader): Promise<Uint8Array> {
|
|
|
|
const chunks: Uint8Array[] = [];
|
|
|
|
while (true) {
|
|
|
|
let chunk = new Uint8Array(DEFAULT_CHUNK_SIZE);
|
|
|
|
const n = await reader.read(chunk);
|
|
|
|
if (n === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n < DEFAULT_CHUNK_SIZE) {
|
|
|
|
chunk = chunk.subarray(0, n);
|
|
|
|
}
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
return concat(chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronously reads {@linkcode ReaderSync} `r` until EOF (`null`) and returns
|
|
|
|
* the content as {@linkcode Uint8Array}.
|
|
|
|
*
|
2024-08-08 14:20:43 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { readAllSync } from "@std/io/read-all";
|
2024-01-12 07:34:12 +00:00
|
|
|
*
|
|
|
|
* // Example from stdin
|
|
|
|
* const stdinContent = readAllSync(Deno.stdin);
|
|
|
|
*
|
|
|
|
* // Example from file
|
2024-01-14 21:35:50 +00:00
|
|
|
* using file = Deno.openSync("my_file.txt", {read: true});
|
2024-01-12 07:34:12 +00:00
|
|
|
* const myFileContent = readAllSync(file);
|
|
|
|
* ```
|
2024-08-08 14:20:43 +00:00
|
|
|
*
|
|
|
|
* @param reader The reader to read from
|
|
|
|
* @returns The content as Uint8Array
|
2024-01-12 07:34:12 +00:00
|
|
|
*/
|
|
|
|
export function readAllSync(reader: ReaderSync): Uint8Array {
|
|
|
|
const chunks: Uint8Array[] = [];
|
|
|
|
while (true) {
|
|
|
|
const chunk = new Uint8Array(DEFAULT_CHUNK_SIZE);
|
|
|
|
const n = reader.readSync(chunk);
|
|
|
|
if (n === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n < DEFAULT_CHUNK_SIZE) {
|
|
|
|
chunks.push(chunk.subarray(0, n));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
return concat(chunks);
|
|
|
|
}
|