2018-11-08 17:26:20 +00:00
|
|
|
// Based on https://github.com/golang/go/blob/891682/src/bufio/bufio.go
|
2018-11-07 19:17:36 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-11-09 18:25:30 +00:00
|
|
|
import { Reader, ReadResult, Writer } from "deno";
|
2018-11-08 04:19:08 +00:00
|
|
|
import { assert, charCode, copyBytes } from "./util.ts";
|
2018-11-07 18:16:07 +00:00
|
|
|
|
|
|
|
const DEFAULT_BUF_SIZE = 4096;
|
|
|
|
const MIN_BUF_SIZE = 16;
|
|
|
|
const MAX_CONSECUTIVE_EMPTY_READS = 100;
|
2018-11-08 04:19:08 +00:00
|
|
|
const CR = charCode("\r");
|
|
|
|
const LF = charCode("\n");
|
2018-11-07 18:16:07 +00:00
|
|
|
|
2018-11-09 22:23:01 +00:00
|
|
|
export type BufState =
|
|
|
|
| null
|
|
|
|
| "EOF"
|
|
|
|
| "BufferFull"
|
|
|
|
| "ShortWrite"
|
|
|
|
| "NoProgress"
|
|
|
|
| Error;
|
2018-11-07 19:17:36 +00:00
|
|
|
|
2018-11-08 02:01:32 +00:00
|
|
|
/** BufReader implements buffering for a Reader object. */
|
|
|
|
export class BufReader implements Reader {
|
2018-11-07 18:16:07 +00:00
|
|
|
private buf: Uint8Array;
|
2018-11-08 02:01:32 +00:00
|
|
|
private rd: Reader; // Reader provided by caller.
|
2018-11-07 18:16:07 +00:00
|
|
|
private r = 0; // buf read position.
|
|
|
|
private w = 0; // buf write position.
|
|
|
|
private lastByte: number;
|
|
|
|
private lastCharSize: number;
|
2018-11-08 07:05:06 +00:00
|
|
|
private err: BufState;
|
2018-11-07 18:16:07 +00:00
|
|
|
|
2018-11-08 02:01:32 +00:00
|
|
|
constructor(rd: Reader, size = DEFAULT_BUF_SIZE) {
|
2018-11-07 18:16:07 +00:00
|
|
|
if (size < MIN_BUF_SIZE) {
|
|
|
|
size = MIN_BUF_SIZE;
|
|
|
|
}
|
2018-11-07 19:17:36 +00:00
|
|
|
this._reset(new Uint8Array(size), rd);
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the size of the underlying buffer in bytes. */
|
2018-11-09 18:25:30 +00:00
|
|
|
size(): number {
|
2018-11-07 18:16:07 +00:00
|
|
|
return this.buf.byteLength;
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:01:32 +00:00
|
|
|
buffered(): number {
|
|
|
|
return this.w - this.r;
|
|
|
|
}
|
|
|
|
|
2018-11-08 07:05:06 +00:00
|
|
|
private _readErr(): BufState {
|
2018-11-08 01:28:01 +00:00
|
|
|
const err = this.err;
|
|
|
|
this.err = null;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-11-07 18:16:07 +00:00
|
|
|
// Reads a new chunk into the buffer.
|
2018-11-08 07:05:06 +00:00
|
|
|
private async _fill(): Promise<void> {
|
2018-11-07 18:16:07 +00:00
|
|
|
// Slide existing data to beginning.
|
|
|
|
if (this.r > 0) {
|
|
|
|
this.buf.copyWithin(0, this.r, this.w);
|
|
|
|
this.w -= this.r;
|
|
|
|
this.r = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.w >= this.buf.byteLength) {
|
|
|
|
throw Error("bufio: tried to fill full buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read new data: try a limited number of times.
|
|
|
|
for (let i = MAX_CONSECUTIVE_EMPTY_READS; i > 0; i--) {
|
2018-11-08 02:01:32 +00:00
|
|
|
let rr: ReadResult;
|
2018-11-08 01:28:01 +00:00
|
|
|
try {
|
|
|
|
rr = await this.rd.read(this.buf.subarray(this.w));
|
|
|
|
} catch (e) {
|
|
|
|
this.err = e;
|
2018-11-08 07:05:06 +00:00
|
|
|
return;
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
2018-11-08 04:19:08 +00:00
|
|
|
assert(rr.nread >= 0, "negative read");
|
2018-11-08 01:28:01 +00:00
|
|
|
this.w += rr.nread;
|
|
|
|
if (rr.eof) {
|
2018-11-08 07:05:06 +00:00
|
|
|
this.err = "EOF";
|
|
|
|
return;
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
2018-11-08 01:28:01 +00:00
|
|
|
if (rr.nread > 0) {
|
2018-11-08 07:05:06 +00:00
|
|
|
return;
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 07:05:06 +00:00
|
|
|
this.err = "NoProgress";
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Discards any buffered data, resets all state, and switches
|
|
|
|
* the buffered reader to read from r.
|
|
|
|
*/
|
2018-11-08 02:01:32 +00:00
|
|
|
reset(r: Reader): void {
|
2018-11-07 18:16:07 +00:00
|
|
|
this._reset(this.buf, r);
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:01:32 +00:00
|
|
|
private _reset(buf: Uint8Array, rd: Reader): void {
|
2018-11-07 18:16:07 +00:00
|
|
|
this.buf = buf;
|
|
|
|
this.rd = rd;
|
|
|
|
this.lastByte = -1;
|
2018-11-08 07:05:06 +00:00
|
|
|
// this.lastRuneSize = -1;
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 19:17:36 +00:00
|
|
|
/** reads data into p.
|
|
|
|
* It returns the number of bytes read into p.
|
|
|
|
* The bytes are taken from at most one Read on the underlying Reader,
|
|
|
|
* hence n may be less than len(p).
|
|
|
|
* At EOF, the count will be zero and err will be io.EOF.
|
|
|
|
* To read exactly len(p) bytes, use io.ReadFull(b, p).
|
|
|
|
*/
|
2018-11-09 22:23:01 +00:00
|
|
|
async read(p: Uint8Array): Promise<ReadResult> {
|
2018-11-08 02:01:32 +00:00
|
|
|
let rr: ReadResult = { nread: p.byteLength, eof: false };
|
2018-11-07 19:17:36 +00:00
|
|
|
if (rr.nread === 0) {
|
2018-11-08 01:28:01 +00:00
|
|
|
if (this.err) {
|
|
|
|
throw this._readErr();
|
|
|
|
}
|
2018-11-07 19:17:36 +00:00
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.r === this.w) {
|
2018-11-08 01:28:01 +00:00
|
|
|
if (this.err) {
|
|
|
|
throw this._readErr();
|
2018-11-07 19:17:36 +00:00
|
|
|
}
|
|
|
|
if (p.byteLength >= this.buf.byteLength) {
|
|
|
|
// Large read, empty buffer.
|
|
|
|
// Read directly into p to avoid copy.
|
|
|
|
rr = await this.rd.read(p);
|
2018-11-08 04:19:08 +00:00
|
|
|
assert(rr.nread >= 0, "negative read");
|
2018-11-07 19:17:36 +00:00
|
|
|
if (rr.nread > 0) {
|
|
|
|
this.lastByte = p[rr.nread - 1];
|
|
|
|
// this.lastRuneSize = -1;
|
|
|
|
}
|
2018-11-08 01:28:01 +00:00
|
|
|
if (this.err) {
|
|
|
|
throw this._readErr();
|
|
|
|
}
|
2018-11-07 19:17:36 +00:00
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
// One read.
|
|
|
|
// Do not use this.fill, which will loop.
|
|
|
|
this.r = 0;
|
|
|
|
this.w = 0;
|
2018-11-08 04:19:08 +00:00
|
|
|
try {
|
|
|
|
rr = await this.rd.read(this.buf);
|
|
|
|
} catch (e) {
|
|
|
|
this.err = e;
|
2018-11-07 19:17:36 +00:00
|
|
|
}
|
2018-11-08 04:19:08 +00:00
|
|
|
assert(rr.nread >= 0, "negative read");
|
2018-11-07 19:17:36 +00:00
|
|
|
if (rr.nread === 0) {
|
2018-11-08 01:28:01 +00:00
|
|
|
if (this.err) {
|
|
|
|
throw this._readErr();
|
|
|
|
}
|
2018-11-07 19:17:36 +00:00
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
this.w += rr.nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy as much as we can
|
|
|
|
rr.nread = copyBytes(p as Uint8Array, this.buf.subarray(this.r, this.w), 0);
|
|
|
|
this.r += rr.nread;
|
|
|
|
this.lastByte = this.buf[this.r - 1];
|
|
|
|
// this.lastRuneSize = -1;
|
|
|
|
return rr;
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 03:57:45 +00:00
|
|
|
/** reads exactly len(p) bytes into p.
|
|
|
|
* Ported from https://golang.org/pkg/io/#ReadFull
|
|
|
|
* It returns the number of bytes copied and an error if fewer bytes were read.
|
|
|
|
* The error is EOF only if no bytes were read.
|
|
|
|
* If an EOF happens after reading some but not all the bytes,
|
|
|
|
* readFull returns ErrUnexpectedEOF. ("EOF" for current impl)
|
|
|
|
* On return, n == len(p) if and only if err == nil.
|
|
|
|
* If r returns an error having read at least len(buf) bytes,
|
|
|
|
* the error is dropped.
|
|
|
|
*/
|
|
|
|
async readFull(p: Uint8Array): Promise<[number, BufState]> {
|
|
|
|
let rr = await this.read(p);
|
|
|
|
let nread = rr.nread;
|
|
|
|
if (rr.eof) {
|
|
|
|
return [nread, nread < p.length ? "EOF" : null];
|
|
|
|
}
|
|
|
|
while (!rr.eof && nread < p.length) {
|
|
|
|
rr = await this.read(p.subarray(nread));
|
|
|
|
nread += rr.nread;
|
|
|
|
}
|
|
|
|
return [nread, nread < p.length ? "EOF" : null];
|
|
|
|
}
|
|
|
|
|
2018-11-07 18:16:07 +00:00
|
|
|
/** Returns the next byte [0, 255] or -1 if EOF. */
|
|
|
|
async readByte(): Promise<number> {
|
|
|
|
while (this.r === this.w) {
|
2018-11-08 07:05:06 +00:00
|
|
|
await this._fill(); // buffer is empty.
|
|
|
|
if (this.err == "EOF") {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-11-08 01:28:01 +00:00
|
|
|
if (this.err != null) {
|
|
|
|
throw this._readErr();
|
|
|
|
}
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
|
|
|
const c = this.buf[this.r];
|
|
|
|
this.r++;
|
|
|
|
this.lastByte = c;
|
|
|
|
return c;
|
|
|
|
}
|
2018-11-08 01:28:01 +00:00
|
|
|
|
|
|
|
/** readString() reads until the first occurrence of delim in the input,
|
|
|
|
* returning a string containing the data up to and including the delimiter.
|
|
|
|
* If ReadString encounters an error before finding a delimiter,
|
|
|
|
* it returns the data read before the error and the error itself (often io.EOF).
|
|
|
|
* ReadString returns err != nil if and only if the returned data does not end in
|
|
|
|
* delim.
|
|
|
|
* For simple uses, a Scanner may be more convenient.
|
|
|
|
*/
|
|
|
|
async readString(delim: string): Promise<string> {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
}
|
2018-11-08 04:19:08 +00:00
|
|
|
|
|
|
|
/** readLine() is a low-level line-reading primitive. Most callers should use
|
|
|
|
* readBytes('\n') or readString('\n') instead or use a Scanner.
|
|
|
|
*
|
|
|
|
* readLine tries to return a single line, not including the end-of-line bytes.
|
|
|
|
* If the line was too long for the buffer then isPrefix is set and the
|
|
|
|
* beginning of the line is returned. The rest of the line will be returned
|
|
|
|
* from future calls. isPrefix will be false when returning the last fragment
|
|
|
|
* of the line. The returned buffer is only valid until the next call to
|
|
|
|
* ReadLine. ReadLine either returns a non-nil line or it returns an error,
|
|
|
|
* never both.
|
|
|
|
*
|
|
|
|
* The text returned from ReadLine does not include the line end ("\r\n" or "\n").
|
|
|
|
* No indication or error is given if the input ends without a final line end.
|
|
|
|
* Calling UnreadByte after ReadLine will always unread the last byte read
|
|
|
|
* (possibly a character belonging to the line end) even if that byte is not
|
|
|
|
* part of the line returned by ReadLine.
|
|
|
|
*/
|
2018-11-08 07:05:06 +00:00
|
|
|
async readLine(): Promise<[Uint8Array, boolean, BufState]> {
|
|
|
|
let [line, err] = await this.readSlice(LF);
|
2018-11-08 04:19:08 +00:00
|
|
|
|
2018-11-08 07:05:06 +00:00
|
|
|
if (err === "BufferFull") {
|
2018-11-08 04:19:08 +00:00
|
|
|
// Handle the case where "\r\n" straddles the buffer.
|
|
|
|
if (line.byteLength > 0 && line[line.byteLength - 1] === CR) {
|
|
|
|
// Put the '\r' back on buf and drop it from line.
|
|
|
|
// Let the next call to ReadLine check for "\r\n".
|
|
|
|
assert(this.r > 0, "bufio: tried to rewind past start of buffer");
|
|
|
|
this.r--;
|
|
|
|
line = line.subarray(0, line.byteLength - 1);
|
|
|
|
}
|
2018-11-08 07:05:06 +00:00
|
|
|
return [line, true, null];
|
2018-11-08 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line.byteLength === 0) {
|
2018-11-08 07:05:06 +00:00
|
|
|
return [line, false, err];
|
2018-11-08 04:19:08 +00:00
|
|
|
}
|
2018-11-08 07:05:06 +00:00
|
|
|
err = null;
|
2018-11-08 04:19:08 +00:00
|
|
|
|
|
|
|
if (line[line.byteLength - 1] == LF) {
|
|
|
|
let drop = 1;
|
|
|
|
if (line.byteLength > 1 && line[line.byteLength - 2] === CR) {
|
|
|
|
drop = 2;
|
|
|
|
}
|
|
|
|
line = line.subarray(0, line.byteLength - drop);
|
|
|
|
}
|
2018-11-08 07:05:06 +00:00
|
|
|
return [line, false, err];
|
2018-11-08 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** readSlice() reads until the first occurrence of delim in the input,
|
|
|
|
* returning a slice pointing at the bytes in the buffer. The bytes stop
|
|
|
|
* being valid at the next read. If readSlice() encounters an error before
|
|
|
|
* finding a delimiter, it returns all the data in the buffer and the error
|
|
|
|
* itself (often io.EOF). readSlice() fails with error ErrBufferFull if the
|
|
|
|
* buffer fills without a delim. Because the data returned from readSlice()
|
|
|
|
* will be overwritten by the next I/O operation, most clients should use
|
|
|
|
* readBytes() or readString() instead. readSlice() returns err != nil if and
|
|
|
|
* only if line does not end in delim.
|
|
|
|
*/
|
|
|
|
async readSlice(delim: number): Promise<[Uint8Array, BufState]> {
|
|
|
|
let s = 0; // search start index
|
|
|
|
let line: Uint8Array;
|
2018-11-08 07:05:06 +00:00
|
|
|
let err: BufState;
|
2018-11-08 04:19:08 +00:00
|
|
|
while (true) {
|
|
|
|
// Search buffer.
|
|
|
|
let i = this.buf.subarray(this.r + s, this.w).indexOf(delim);
|
|
|
|
if (i >= 0) {
|
|
|
|
i += s;
|
|
|
|
line = this.buf.subarray(this.r, this.r + i + 1);
|
|
|
|
this.r += i + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pending error?
|
|
|
|
if (this.err) {
|
|
|
|
line = this.buf.subarray(this.r, this.w);
|
|
|
|
this.r = this.w;
|
2018-11-08 07:05:06 +00:00
|
|
|
err = this._readErr();
|
2018-11-08 04:19:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer full?
|
|
|
|
if (this.buffered() >= this.buf.byteLength) {
|
|
|
|
this.r = this.w;
|
|
|
|
line = this.buf;
|
2018-11-08 07:05:06 +00:00
|
|
|
err = "BufferFull";
|
2018-11-08 04:19:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = this.w - this.r; // do not rescan area we scanned before
|
|
|
|
|
|
|
|
await this._fill(); // buffer is not full
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle last byte, if any.
|
|
|
|
let i = line.byteLength - 1;
|
|
|
|
if (i >= 0) {
|
|
|
|
this.lastByte = line[i];
|
|
|
|
// this.lastRuneSize = -1
|
|
|
|
}
|
|
|
|
|
2018-11-08 07:05:06 +00:00
|
|
|
return [line, err];
|
2018-11-08 04:19:08 +00:00
|
|
|
}
|
2018-11-08 09:01:20 +00:00
|
|
|
|
|
|
|
/** Peek returns the next n bytes without advancing the reader. The bytes stop
|
|
|
|
* being valid at the next read call. If Peek returns fewer than n bytes, it
|
|
|
|
* also returns an error explaining why the read is short. The error is
|
|
|
|
* ErrBufferFull if n is larger than b's buffer size.
|
|
|
|
*/
|
|
|
|
async peek(n: number): Promise<[Uint8Array, BufState]> {
|
|
|
|
if (n < 0) {
|
|
|
|
throw Error("negative count");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (
|
|
|
|
this.w - this.r < n &&
|
|
|
|
this.w - this.r < this.buf.byteLength &&
|
|
|
|
this.err == null
|
|
|
|
) {
|
|
|
|
await this._fill(); // this.w - this.r < len(this.buf) => buffer is not full
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > this.buf.byteLength) {
|
|
|
|
return [this.buf.subarray(this.r, this.w), "BufferFull"];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 <= n <= len(this.buf)
|
|
|
|
let err: BufState;
|
|
|
|
let avail = this.w - this.r;
|
|
|
|
if (avail < n) {
|
|
|
|
// not enough data in buffer
|
|
|
|
n = avail;
|
|
|
|
err = this._readErr();
|
|
|
|
if (!err) {
|
|
|
|
err = "BufferFull";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.buf.subarray(this.r, this.r + n), err];
|
|
|
|
}
|
2018-11-07 18:16:07 +00:00
|
|
|
}
|
2018-11-08 23:15:26 +00:00
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
export class BufWriter implements Writer {
|
2018-11-09 18:25:30 +00:00
|
|
|
buf: Uint8Array;
|
|
|
|
n: number = 0;
|
2018-11-09 22:23:01 +00:00
|
|
|
err: null | BufState = null;
|
2018-11-09 18:25:30 +00:00
|
|
|
|
|
|
|
constructor(private wr: Writer, size = DEFAULT_BUF_SIZE) {
|
|
|
|
if (size <= 0) {
|
|
|
|
size = DEFAULT_BUF_SIZE;
|
|
|
|
}
|
|
|
|
this.buf = new Uint8Array(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Size returns the size of the underlying buffer in bytes. */
|
|
|
|
size(): number {
|
|
|
|
return this.buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Discards any unflushed buffered data, clears any error, and
|
|
|
|
* resets b to write its output to w.
|
|
|
|
*/
|
|
|
|
reset(w: Writer): void {
|
|
|
|
this.err = null;
|
|
|
|
this.n = 0;
|
|
|
|
this.wr = w;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Flush writes any buffered data to the underlying io.Writer. */
|
2018-11-09 22:23:01 +00:00
|
|
|
async flush(): Promise<BufState> {
|
2018-11-09 18:25:30 +00:00
|
|
|
if (this.err != null) {
|
2018-11-09 22:23:01 +00:00
|
|
|
return this.err;
|
2018-11-09 18:25:30 +00:00
|
|
|
}
|
|
|
|
if (this.n == 0) {
|
2018-11-09 22:23:01 +00:00
|
|
|
return null;
|
2018-11-09 18:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let n: number;
|
2018-11-09 22:23:01 +00:00
|
|
|
let err: BufState = null;
|
2018-11-09 18:25:30 +00:00
|
|
|
try {
|
|
|
|
n = await this.wr.write(this.buf.subarray(0, this.n));
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < this.n && err == null) {
|
2018-11-09 22:23:01 +00:00
|
|
|
err = "ShortWrite";
|
2018-11-09 18:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err != null) {
|
|
|
|
if (n > 0 && n < this.n) {
|
|
|
|
this.buf.copyWithin(0, n, this.n);
|
|
|
|
}
|
|
|
|
this.n -= n;
|
|
|
|
this.err = err;
|
2018-11-09 22:23:01 +00:00
|
|
|
return err;
|
2018-11-09 18:25:30 +00:00
|
|
|
}
|
|
|
|
this.n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns how many bytes are unused in the buffer. */
|
|
|
|
available(): number {
|
|
|
|
return this.buf.byteLength - this.n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** buffered returns the number of bytes that have been written into the
|
|
|
|
* current buffer.
|
|
|
|
*/
|
|
|
|
buffered(): number {
|
|
|
|
return this.n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Writes the contents of p into the buffer.
|
|
|
|
* Returns the number of bytes written.
|
|
|
|
*/
|
|
|
|
async write(p: Uint8Array): Promise<number> {
|
|
|
|
let nn = 0;
|
|
|
|
let n: number;
|
|
|
|
while (p.byteLength > this.available() && !this.err) {
|
|
|
|
if (this.buffered() == 0) {
|
|
|
|
// Large write, empty buffer.
|
|
|
|
// Write directly from p to avoid copy.
|
|
|
|
try {
|
|
|
|
n = await this.wr.write(p);
|
|
|
|
} catch (e) {
|
|
|
|
this.err = e;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n = copyBytes(this.buf, p, this.n);
|
|
|
|
this.n += n;
|
2018-12-11 22:56:32 +00:00
|
|
|
await this.flush();
|
2018-11-09 18:25:30 +00:00
|
|
|
}
|
|
|
|
nn += n;
|
|
|
|
p = p.subarray(n);
|
|
|
|
}
|
|
|
|
if (this.err) {
|
|
|
|
throw this.err;
|
|
|
|
}
|
|
|
|
n = copyBytes(this.buf, p, this.n);
|
|
|
|
this.n += n;
|
|
|
|
nn += n;
|
|
|
|
return nn;
|
|
|
|
}
|
2018-11-08 23:15:26 +00:00
|
|
|
}
|