1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# std/io
## Bufio
**Uses:**
### readLines
Read reader[like file], line by line
```ts title="readLines"
import { readLines } from "https://deno.land/std/io/mod.ts";
import * as path from "https://deno.land/std/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);
}
```
**Output:**
````text
# std/io
## readLines
```ts
import * as path from "https://deno.land/std/path/mod.ts";
## Rest of the file
````
### readStringDelim
Read reader`[like file]` chunk by chunk, splitting based on delimiter.
```ts title="readStringDelim"
import { readStringDelim } from "https://deno.land/std/io/mod.ts";
import * as path from "https://deno.land/std/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);
}
```
**Output:**
````text
# std/io
## readLines
```ts
import * as path from "https://deno.land/std/path/mod.ts";
## Rest of the file
````
## Reader
### StringReader
Create a `Reader` object for `string`.
```ts
import { StringReader } from "https://deno.land/std/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:**
```text
6
null
abcdef
```
## Writer
### StringWriter
Create a `Writer` object for `string`.
```ts
import {
StringWriter,
StringReader,
copyN,
} from "https://deno.land/std/io/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 Deno.copy(r, w); // copy all
console.log(w.toString()); // base0123456789
```
**Output:**
```text
base0123
base0123456789
```
## Streams
### fromStreamReader
Creates a `Reader` from a `ReadableStreamDefaultReader`
```ts
import { fromStreamReader } from "https://deno.land/std/io/mod.ts";
const res = await fetch("https://deno.land");
const file = await Deno.open("./deno.land.html", { create: true, write: true });
const reader = fromStreamReader(res.body!.getReader());
await Deno.copy(reader, file);
file.close();
```
### fromStreamWriter
Creates a `Writer` from a `WritableStreamDefaultWriter`
```ts
import { fromStreamWriter } from "https://deno.land/std/io/mod.ts";
const file = await Deno.open("./deno.land.html", { read: true });
const writableStream = new WritableStream({
write(chunk): void {
console.log(chunk);
},
});
const writer = fromStreamWriter(writableStream.getWriter());
await Deno.copy(file, writer);
file.close();
```
|