"use strict";
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.readAllSync = exports.readAll = void 0;
const concat_js_1 = require("../../bytes/0.218.2/concat.js");
const _constants_js_1 = require("./_constants.js");
/**
 * Read {@linkcode Reader} `r` until EOF (`null`) and resolve to the content as
 * {@linkcode Uint8Array}.
 *
 * @example
 * ```ts
 * import { readAll } from "@std/io/read_all";
 *
 * // Example from stdin
 * const stdinContent = await readAll(Deno.stdin);
 *
 * // Example from file
 * using file = await Deno.open("my_file.txt", {read: true});
 * const myFileContent = await readAll(file);
 * ```
 */
async function readAll(reader) {
    const chunks = [];
    while (true) {
        let chunk = new Uint8Array(_constants_js_1.DEFAULT_CHUNK_SIZE);
        const n = await reader.read(chunk);
        if (n === null) {
            break;
        }
        if (n < _constants_js_1.DEFAULT_CHUNK_SIZE) {
            chunk = chunk.subarray(0, n);
        }
        chunks.push(chunk);
    }
    return (0, concat_js_1.concat)(chunks);
}
exports.readAll = readAll;
/**
 * Synchronously reads {@linkcode ReaderSync} `r` until EOF (`null`) and returns
 * the content as {@linkcode Uint8Array}.
 *
 * @example
 * ```ts
 * import { readAllSync } from "@std/io/read_all";
 *
 * // Example from stdin
 * const stdinContent = readAllSync(Deno.stdin);
 *
 * // Example from file
 * using file = Deno.openSync("my_file.txt", {read: true});
 * const myFileContent = readAllSync(file);
 * ```
 */
function readAllSync(reader) {
    const chunks = [];
    while (true) {
        const chunk = new Uint8Array(_constants_js_1.DEFAULT_CHUNK_SIZE);
        const n = reader.readSync(chunk);
        if (n === null) {
            break;
        }
        if (n < _constants_js_1.DEFAULT_CHUNK_SIZE) {
            chunks.push(chunk.subarray(0, n));
            break;
        }
        chunks.push(chunk);
    }
    return (0, concat_js_1.concat)(chunks);
}
exports.readAllSync = readAllSync;
