# Module /io

Buffered and standard stream I/O (Buf-Reader, Buf-Writer, Stdin, Stdout, Stderr, read-line, write-line, flush, and ask).

```tangu
(use /io)
```

## Functions & symbols

### *err*

`io/*err* -> Any`

Active standard error sink used by diagnostics and tracing, rebindable via `let-ds`.

```tangu
(use {*err* Buffer} /io)
(let err (Buffer))
(let-ds [*err* err] (write-line *err* "warn"))
```

### *in*

`io/*in* -> Any`

Active standard input source used by `(tty/ask)` and rebindable via `let-ds` to any `Read` / `Read-Text` value.

```tangu
(use {*in* Buffer} /io)
(let-ds [*in* (Buffer "hello\n")] (read-line *in*))
```

### *out*

`io/*out* -> Any`

Active standard output sink used by `say`, `echo`, `writeln`, `dump`, and `(tty/ask)`, rebindable via `let-ds` to any `Write` / `Write-Text` value.

```tangu
(use {*out* Buffer} /io)
(let buf (Buffer))
(let-ds [*out* buf] (say "hi"))
```

### Buf-Reader

`(io/Buf-Reader source) -> Buf-Reader`

Wraps a `Read` source with an internal read buffer implementing `Read` and `Read-Text`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `source` | `Any` | no | Underlying `Read` source |
| `buffer-size` | `Int` | no | Optional internal chunk size in bytes |

### Buf-Writer

`(io/Buf-Writer sink) -> Buf-Writer`

Wraps a `Write` sink with an internal write buffer implementing `Write`, `Write-Text`, and `Flush`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `sink` | `Any` | no | Underlying `Write` sink |
| `buffer-size` | `Int` | no | Optional internal flush threshold in bytes |

### Buffer

`(io/Buffer) -> Buffer`

Constructs an in-memory growable byte queue implementing `Read`, `Read-Text`, `Write`, `Write-Text`, `Flush`, and `Countable`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `init-or-capacity` | `String | Slab | Int` | no | Optional initial String/Slab contents or preallocated byte capacity |

```tangu
(let buf (Buffer "line1\nline2"))
(read-line buf)
```

### Tee-Writer

`(io/Tee-Writer ...sinks) -> Tee-Writer`

Constructs a multi-sink fan-out writer implementing `Write`, `Write-Text`, and `Flush`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `sinks` | `Any` | no | Zero or more downstream `Write` / `Write-Text` sinks |

```tangu
(let tee (Tee-Writer buf1 buf2))
(write-line tee "hello")
```

### flush

`(io/flush writer) -> None`

Flushes all buffered output bytes in `writer`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `writer` | `Buf-Writer | File | Buffer` | no | Writable stream |

```tangu
(io/flush w)
```

### read

`(io/read reader buf) -> Int`

Reads bytes from `reader` into `buf` and returns byte count read.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `reader` | `Buf-Reader | File | Buffer` | no | Readable stream implementing Read |
| `buf` | `Slab` | no | Mutable byte Slab |

```tangu
(io/read r slab)
```

### read-all

`(io/read-all reader) -> Slab`

Reads all remaining bytes from `reader` until EOF into a `Slab`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `reader` | `Buf-Reader | File | Buffer` | no | Readable stream |

```tangu
(io/read-all r)
```

### read-byte

`(io/read-byte reader) -> (Or U8 None)`

Reads a single byte (`U8`) from `reader`, returning `()` at EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `reader` | `Buf-Reader | File | Buffer` | no | Readable stream |

```tangu
(io/read-byte r)
```

### read-char

`(io/read-char reader) -> (Or Char None)`

Reads a single UTF-8 `Char` from `reader`, returning `()` at EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `reader` | `Buf-Reader | File` | no | Readable text stream |

```tangu
(io/read-char r)
```

### read-line

`(io/read-line reader) -> (Or String None)`

Reads the next line of UTF-8 text from `reader`, returning `()` at EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `reader` | `Buf-Reader | File` | no | Readable text stream |

```tangu
(io/read-line r)
```

### read-string

`(io/read-string reader) -> String`

Reads all remaining UTF-8 text from `reader` until EOF.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `reader` | `Buf-Reader | File` | no | Readable text stream |

```tangu
(io/read-string r)
```

### with-output-to-string

`(io/with-output-to-string func) -> String`

Captures everything written to `*out*` during `(func)` and returns it as a `String`, restoring `*out*` on exit.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `func` | `Func` | no | Zero-argument function executed while `*out*` is bound to a fresh `Buffer` |

```tangu
(with-output-to-string (Func () (say "captured")))
```

### write

`(io/write writer data) -> Int`

Writes bytes or string data to `writer` and returns bytes written.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `writer` | `Buf-Writer | File | Buffer` | no | Writable stream |
| `data` | `String | Slab` | no | Bytes or text to write |

```tangu
(io/write w "data")
```

### write-byte

`(io/write-byte writer byte) -> None`

Writes one byte to `writer`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `writer` | `Buf-Writer | File | Buffer` | no | Writable stream |
| `byte` | `U8 | Int` | no | Single byte |

```tangu
(io/write-byte w 10u8)
```

### write-line

`(io/write-line writer line) -> None`

Writes `line` followed by a newline to `writer`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `writer` | `Buf-Writer | File` | no | Writable text stream |
| `line` | `String` | no | Line string |

```tangu
(io/write-line w "hello")
```

### write-string

`(io/write-string writer str) -> None`

Writes `str` directly to `writer` without adding a newline.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `writer` | `Buf-Writer | File` | no | Writable text stream |
| `str` | `String` | no | UTF-8 string |

```tangu
(io/write-string w "part")
```
