Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/deno/cli/js/buffer.ts>Buffer

A modern runtime for JavaScript and TypeScript.
Go to Latest
class Buffer
import { Buffer } from "https://deno.land/x/deno@v0.28.0/cli/js/buffer.ts";

A Buffer is a variable-sized buffer of bytes with read() and write() methods. Based on https://golang.org/pkg/bytes/#Buffer

Constructors

new
Buffer(ab?: ArrayBuffer)

Properties

private
buf: Uint8Array
private
off: number
readonly
capacity: number

Returns the capacity of the buffer's underlying byte slice, that is, the total space allocated for the buffer's data.

readonly
length: number

length is a getter that returns the number of bytes of the unread portion of the buffer

Methods

private
_grow(n: number): number

_grow() grows the buffer to guarantee space for n more bytes. It returns the index where bytes should be written. If the buffer can't grow it will throw with ErrTooLarge.

private
_reslice(len: number): void
private
_tryGrowByReslice(n: number): number

_tryGrowByReslice() is a version of grow for the fast-case where the internal buffer only needs to be resliced. It returns the index where bytes should be written and whether it succeeded. It returns -1 if a reslice was not needed.

bytes(): Uint8Array

bytes() returns a slice holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification (that is, only until the next call to a method like read(), write(), reset(), or truncate()). The slice aliases the buffer content at least until the next buffer modification, so immediate changes to the slice will affect the result of future reads.

empty(): boolean

empty() returns whether the unread portion of the buffer is empty.

grow(n: number): void

grow() grows the buffer's capacity, if necessary, to guarantee space for another n bytes. After grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, grow() will panic. If the buffer can't grow it will throw ErrTooLarge. Based on https://golang.org/pkg/bytes/#Buffer.Grow

read(p: Uint8Array): Promise<number | EOF>
readFrom(r: Reader): Promise<number>

readFrom() reads data from r until EOF and appends it to the buffer, growing the buffer as needed. It returns the number of bytes read. If the buffer becomes too large, readFrom will panic with ErrTooLarge. Based on https://golang.org/pkg/bytes/#Buffer.ReadFrom

Sync version of readFrom

readSync(p: Uint8Array): number | EOF

readSync() reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, eof in the response will be true.

reset(): void

reset() resets the buffer to be empty, but it retains the underlying storage for use by future writes. reset() is the same as truncate(0)

toString(): string

toString() returns the contents of the unread portion of the buffer as a string. Warning - if multibyte characters are present when data is flowing through the buffer, this method may result in incorrect strings due to a character being split.

truncate(n: number): void

truncate() discards all but the first n unread bytes from the buffer but continues to use the same allocated storage. It throws if n is negative or greater than the length of the buffer.

write(p: Uint8Array): Promise<number>
writeSync(p: Uint8Array): number