// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import type { BufReader } from "./buf_reader.ts";
import { readShort } from "./read_short.ts";

/**
 * Read big endian 32bit integer from a {@linkcode BufReader}.
 *
 * @example Usage
 * ```ts
 * import { Buffer } from "@std/io/buffer"
 * import { BufReader } from "@std/io/buf-reader";
 * import { readInt } from "@std/io/read-int";
 * import { assertEquals } from "@std/assert/equals";
 *
 * const buf = new BufReader(new Buffer(new Uint8Array([0x12, 0x34, 0x56, 0x78])));
 * const int = await readInt(buf);
 * assertEquals(int, 0x12345678);
 * ```
 *
 * @param buf The buffer reader to read from
 * @returns The 32bit integer
 *
 * @deprecated This will be removed in 1.0.0. Use the {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API} instead.
 */
export async function readInt(buf: BufReader): Promise<number | null> {
  const high = await readShort(buf);
  if (high === null) return null;
  const low = await readShort(buf);
  if (low === null) throw new Deno.errors.UnexpectedEof();
  return (high << 16) | low;
}

// denoCacheMetadata={"headers":{"access-control-allow-origin":"*","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","alt-svc":"h3=\":443\"; ma=86400","cf-ray":"a223e656f86d7db6-LAX","set-cookie":"__cf_bm=p0wg7s7Y94Iz4Zu9_UehjxOKn8kqJqrG1pxsxX6qXU0-1785241875.0345404-1.0.1.1-RzMlCQPfqreNf_sUm2X9dqycxwzCoHOnYfDv8jTYT8FMSXfcmQDCQoa_096WO9lVKUdajPa63lxAPfc0cMmo77uxP1GLSnBVY3YBrsfRbPXWAmlF8szeyzScbTiTKezR; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","server":"cloudflare","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=1yFx2eE0bSs76YbqKvc%2F1QT5m%2BwnomVUGv62zr6jQSC6idNNvg1DWtnOdcwnvRYlwyqyAWdyFYIr%2BOfhk9GbjpWYnDDbm8gU8iDI47fMgarykkNmf2%2BnOnk%3D\"}]}","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","access-control-expose-headers":"*","x-content-type-options":"nosniff","content-type":"text/typescript","cross-origin-resource-policy":"cross-origin","content-length":"1131","cache-control":"public, max-age=31536000, immutable","date":"Tue, 28 Jul 2026 12:31:15 GMT","x-robots-tag":"noindex","etag":"\"cce6ebd3f005d2419c8044d239857dba\"","vary":"Origin","x-jsr-backend":"modules"},"url":"https://jsr.io/@std/io/0.224.8/read_int.ts","time":1785241875}