summaryrefslogtreecommitdiff
path: root/std/io
diff options
context:
space:
mode:
Diffstat (limited to 'std/io')
-rw-r--r--std/io/bufio_test.ts15
-rw-r--r--std/io/ioutil_test.ts16
-rw-r--r--std/io/readers.ts18
-rw-r--r--std/io/util.ts7
4 files changed, 25 insertions, 31 deletions
diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts
index 671ed2115..b03640d52 100644
--- a/std/io/bufio_test.ts
+++ b/std/io/bufio_test.ts
@@ -17,7 +17,8 @@ import {
readLines,
} from "./bufio.ts";
import * as iotest from "./_iotest.ts";
-import { charCode, copyBytes, stringsReader } from "./util.ts";
+import { StringReader } from "./readers.ts";
+import { charCode, copyBytes } from "./util.ts";
const encoder = new TextEncoder();
@@ -38,7 +39,7 @@ async function readBytes(buf: BufReader): Promise<string> {
Deno.test("bufioReaderSimple", async function (): Promise<void> {
const data = "hello world";
- const b = new BufReader(stringsReader(data));
+ const b = new BufReader(new StringReader(data));
const s = await readBytes(b);
assertEquals(s, data);
});
@@ -119,7 +120,7 @@ Deno.test("bufioBufReader", async function (): Promise<void> {
for (const readmaker of readMakers) {
for (const bufreader of bufreaders) {
for (const bufsize of bufsizes) {
- const read = readmaker.fn(stringsReader(text));
+ const read = readmaker.fn(new StringReader(text));
const buf = new BufReader(read, bufsize);
const s = await bufreader.fn(buf);
const debugStr =
@@ -136,7 +137,7 @@ Deno.test("bufioBufferFull", async function (): Promise<void> {
const longString =
"And now, hello, world! It is the time for all good men to come to the" +
" aid of their party";
- const buf = new BufReader(stringsReader(longString), MIN_READ_BUFFER_SIZE);
+ const buf = new BufReader(new StringReader(longString), MIN_READ_BUFFER_SIZE);
const decoder = new TextDecoder();
try {
@@ -156,7 +157,7 @@ Deno.test("bufioBufferFull", async function (): Promise<void> {
Deno.test("bufioReadString", async function (): Promise<void> {
const string = "And now, hello world!";
- const buf = new BufReader(stringsReader(string), MIN_READ_BUFFER_SIZE);
+ const buf = new BufReader(new StringReader(string), MIN_READ_BUFFER_SIZE);
const line = await buf.readString(",");
assert(line !== null);
@@ -248,7 +249,7 @@ Deno.test("bufioPeek", async function (): Promise<void> {
const p = new Uint8Array(10);
// string is 16 (minReadBufferSize) long.
const buf = new BufReader(
- stringsReader("abcdefghijklmnop"),
+ new StringReader("abcdefghijklmnop"),
MIN_READ_BUFFER_SIZE
);
@@ -453,7 +454,7 @@ Deno.test(
const decoder = new TextDecoder();
const data = "abcdefghijklmnopqrstuvwxyz";
const bufSize = 25;
- const b = new BufReader(stringsReader(data), bufSize);
+ const b = new BufReader(new StringReader(data), bufSize);
const r1 = (await b.readLine()) as ReadLineResult;
assert(r1 !== null);
diff --git a/std/io/ioutil_test.ts b/std/io/ioutil_test.ts
index cf8eaf437..977c7022c 100644
--- a/std/io/ioutil_test.ts
+++ b/std/io/ioutil_test.ts
@@ -9,8 +9,9 @@ import {
readShort,
sliceLongToBytes,
} from "./ioutil.ts";
+import { StringReader } from "./readers.ts";
import { BufReader } from "./bufio.ts";
-import { stringsReader, tempFile } from "./util.ts";
+import { tempFile } from "./util.ts";
import * as path from "../path/mod.ts";
class BinaryReader implements Reader {
@@ -73,7 +74,7 @@ Deno.test("testSliceLongToBytes2", function (): void {
Deno.test("testCopyN1", async function (): Promise<void> {
const w = new Buffer();
- const r = stringsReader("abcdefghij");
+ const r = new StringReader("abcdefghij");
const n = await copyN(r, w, 3);
assertEquals(n, 3);
assertEquals(new TextDecoder().decode(w.bytes()), "abc");
@@ -81,7 +82,7 @@ Deno.test("testCopyN1", async function (): Promise<void> {
Deno.test("testCopyN2", async function (): Promise<void> {
const w = new Buffer();
- const r = stringsReader("abcdefghij");
+ const r = new StringReader("abcdefghij");
const n = await copyN(r, w, 11);
assertEquals(n, 10);
assertEquals(new TextDecoder().decode(w.bytes()), "abcdefghij");
@@ -91,10 +92,17 @@ Deno.test("copyNWriteAllData", async function (): Promise<void> {
const { filepath, file } = await tempFile(path.resolve("io"));
const size = 16 * 1024 + 1;
const data = "a".repeat(32 * 1024);
- const r = stringsReader(data);
+ const r = new StringReader(data);
const n = await copyN(r, file, size); // Over max file possible buffer
file.close();
await Deno.remove(filepath);
assertEquals(n, size);
});
+
+Deno.test("testStringReaderEof", async function (): Promise<void> {
+ const r = new StringReader("abc");
+ assertEquals(await r.read(new Uint8Array()), 0);
+ assertEquals(await r.read(new Uint8Array(4)), 3);
+ assertEquals(await r.read(new Uint8Array(1)), null);
+});
diff --git a/std/io/readers.ts b/std/io/readers.ts
index 201b87cd8..d43655263 100644
--- a/std/io/readers.ts
+++ b/std/io/readers.ts
@@ -7,22 +7,12 @@
type Reader = Deno.Reader;
import { encode } from "../encoding/utf8.ts";
+const { Buffer } = Deno;
/** Reader utility for strings */
-export class StringReader implements Reader {
- private offs = 0;
- private buf = new Uint8Array(encode(this.s));
-
- constructor(private readonly s: string) {}
-
- read(p: Uint8Array): Promise<number | null> {
- const n = Math.min(p.byteLength, this.buf.byteLength - this.offs);
- p.set(this.buf.slice(this.offs, this.offs + n));
- this.offs += n;
- if (n === 0) {
- return Promise.resolve(null);
- }
- return Promise.resolve(n);
+export class StringReader extends Buffer {
+ constructor(private readonly s: string) {
+ super(encode(s).buffer);
}
}
diff --git a/std/io/util.ts b/std/io/util.ts
index 237747a89..47e48a981 100644
--- a/std/io/util.ts
+++ b/std/io/util.ts
@@ -1,9 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-const { Buffer, mkdir, open } = Deno;
+const { mkdir, open } = Deno;
type File = Deno.File;
type Reader = Deno.Reader;
import * as path from "../path/mod.ts";
-import { encode } from "../encoding/utf8.ts";
/**
* Copy bytes from one Uint8Array to another. Bytes from `src` which don't fit
@@ -28,10 +27,6 @@ export function charCode(s: string): number {
return s.charCodeAt(0);
}
-export function stringsReader(s: string): Reader {
- return new Buffer(encode(s).buffer);
-}
-
/** Create or open a temporal file at specified directory with prefix and
* postfix
* */