"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readRangeSync = exports.readRange = void 0;
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
const dntShim = __importStar(require("../../../../../_dnt.shims.js"));
const copy_js_1 = require("../../bytes/0.218.2/copy.js");
const assert_js_1 = require("../../assert/0.218.2/assert.js");
const DEFAULT_BUFFER_SIZE = 32 * 1024;
/**
 * Read a range of bytes from a file or other resource that is readable and
 * seekable.  The range start and end are inclusive of the bytes within that
 * range.
 *
 * ```ts
 * import { assertEquals } from "@std/assert/assert_equals";
 * import { readRange } from "@std/io/read_range";
 *
 * // Read the first 10 bytes of a file
 * const file = await Deno.open("example.txt", { read: true });
 * const bytes = await readRange(file, { start: 0, end: 9 });
 * assertEquals(bytes.length, 10);
 * ```
 *
 * @deprecated (will be removed after 1.0.0) Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
 */
async function readRange(r, range) {
    // byte ranges are inclusive, so we have to add one to the end
    let length = range.end - range.start + 1;
    (0, assert_js_1.assert)(length > 0, "Invalid byte range was passed.");
    await r.seek(range.start, dntShim.Deno.SeekMode.Start);
    const result = new Uint8Array(length);
    let off = 0;
    while (length) {
        const p = new Uint8Array(Math.min(length, DEFAULT_BUFFER_SIZE));
        const nread = await r.read(p);
        (0, assert_js_1.assert)(nread !== null, "Unexpected EOF reach while reading a range.");
        (0, assert_js_1.assert)(nread > 0, "Unexpected read of 0 bytes while reading a range.");
        (0, copy_js_1.copy)(p, result, off);
        off += nread;
        length -= nread;
        (0, assert_js_1.assert)(length >= 0, "Unexpected length remaining after reading range.");
    }
    return result;
}
exports.readRange = readRange;
/**
 * Read a range of bytes synchronously from a file or other resource that is
 * readable and seekable.  The range start and end are inclusive of the bytes
 * within that range.
 *
 * ```ts
 * import { assertEquals } from "@std/assert/assert_equals";
 * import { readRangeSync } from "@std/io/read_range";
 *
 * // Read the first 10 bytes of a file
 * const file = Deno.openSync("example.txt", { read: true });
 * const bytes = readRangeSync(file, { start: 0, end: 9 });
 * assertEquals(bytes.length, 10);
 * ```
 *
 * @deprecated (will be removed after 1.0.0) Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
 */
function readRangeSync(r, range) {
    // byte ranges are inclusive, so we have to add one to the end
    let length = range.end - range.start + 1;
    (0, assert_js_1.assert)(length > 0, "Invalid byte range was passed.");
    r.seekSync(range.start, dntShim.Deno.SeekMode.Start);
    const result = new Uint8Array(length);
    let off = 0;
    while (length) {
        const p = new Uint8Array(Math.min(length, DEFAULT_BUFFER_SIZE));
        const nread = r.readSync(p);
        (0, assert_js_1.assert)(nread !== null, "Unexpected EOF reach while reading a range.");
        (0, assert_js_1.assert)(nread > 0, "Unexpected read of 0 bytes while reading a range.");
        (0, copy_js_1.copy)(p, result, off);
        off += nread;
        length -= nread;
        (0, assert_js_1.assert)(length >= 0, "Unexpected length remaining after reading range.");
    }
    return result;
}
exports.readRangeSync = readRangeSync;
