std@0.116.0/io

Deno standard library
GitHub Stars
2187
Go to latest
import * as mod from "https://deno.land/std@0.116.0/io/mod.ts";

Classes

Buffer

A variable-sized buffer of bytes with read() and write() methods.

BufferFullError
BufReader

BufReader implements buffering for a Reader object.

BufWriter

BufWriter implements buffering for an deno.Writer object. If an error occurs writing to a Writer, no more data will be accepted and all subsequent writes, and flush(), will return the error. After all data has been written, the client should call the flush() method to guarantee all data has been forwarded to the underlying deno.Writer.

BufWriterSync

BufWriterSync implements buffering for a deno.WriterSync object. If an error occurs writing to a WriterSync, no more data will be accepted and all subsequent writes, and flush(), will return the error. After all data has been written, the client should call the flush() method to guarantee all data has been forwarded to the underlying deno.WriterSync.

LimitedReader

A LimitedReader reads from reader but limits the amount of data returned to just limit bytes. Each call to read updates limit to reflect the new amount remaining. read returns null when limit <= 0 or when the underlying reader returns null.

MultiReader

Reader utility for combining multiple readers

PartialReadError
StringReader

Reader utility for strings

StringWriter

Writer utility for buffering string chunks

Variables

copy 👎 deprecated
iterateReader 👎 deprecated
iterateReaderSync 👎 deprecated
readableStreamFromIterable 👎 deprecated
readableStreamFromReader 👎 deprecated
readAll 👎 deprecated
readAllSync 👎 deprecated
readerFromIterable 👎 deprecated
readerFromStreamReader 👎 deprecated
writableStreamFromWriter 👎 deprecated
writeAll 👎 deprecated
writeAllSync 👎 deprecated
writerFromStreamWriter 👎 deprecated

Functions

copyN

Copy N size at the most. If read size is lesser than N, then returns nread

readDelim

Read delimited bytes from a Reader.

readInt

Read big endian 32bit integer from BufReader

readLines

Read strings line-by-line from a Reader.

readLong

Read big endian 64bit long from BufReader

readShort

Read big endian 16bit short from BufReader

readStringDelim

Read delimited strings from a Reader.

sliceLongToBytes

Slice number into 64bit big endian byte array

Interfaces

ReadLineResult

Result type returned by of BufReader.readLine().

std/io

readLines

Read reader[like file], line by line:

import { readLines } from "https://deno.land/std@0.116.0/io/mod.ts";
import * as path from "https://deno.land/std@0.116.0/path/mod.ts";

const filename = path.join(Deno.cwd(), "std/io/README.md");
let fileReader = await Deno.open(filename);

for await (let line of readLines(fileReader)) {
  console.log(line);
}

readStringDelim

Read reader[like file] chunk by chunk, splitting based on delimiter.

import { readStringDelim } from "https://deno.land/std@0.116.0/io/mod.ts";
import * as path from "https://deno.land/std@0.116.0/path/mod.ts";

const filename = path.join(Deno.cwd(), "std/io/README.md");
let fileReader = await Deno.open(filename);

for await (let line of readStringDelim(fileReader, "\n")) {
  console.log(line);
}

StringReader

Create a Reader object for string.

import { StringReader } from "https://deno.land/std@0.116.0/io/mod.ts";

const data = new Uint8Array(6);
const r = new StringReader("abcdef");
const res0 = await r.read(data);
const res1 = await r.read(new Uint8Array(6));

// Number of bytes read
console.log(res0); // 6
console.log(res1); // null, no byte left to read. EOL

// text

console.log(new TextDecoder().decode(data)); // abcdef

Output:

6
null
abcdef

StringWriter

Create a Writer object for string.

import {
  copyN,
  StringReader,
  StringWriter,
} from "https://deno.land/std@0.116.0/io/mod.ts";
import { copy } from "https://deno.land/std@0.116.0/streams/mod.ts";

const w = new StringWriter("base");
const r = new StringReader("0123456789");
await copyN(r, w, 4); // copy 4 bytes

// Number of bytes read
console.log(w.toString()); //base0123

await copy(r, w); // copy all
console.log(w.toString()); // base0123456789

Output:

base0123
base0123456789