---
url: >-
  https://adk.nht.io/api/@nhtio/adk/batteries/storage/in_memory/classes/InMemorySpoolReader.md
description: >-
  Sync in-memory {@link @nhtio/adk!SpoolReader} over a byte-faithful
  `Uint8Array` body.
---

# Class: InMemorySpoolReader

Defined in: [batteries/storage/in\_memory/index.ts:43](https://github.com/NHTIO/ADK/blob/v1.20260605.0/src/batteries/storage/in_memory/index.ts#L43)

Sync in-memory [@nhtio/adk!SpoolReader](../../../../spooled_artifact/interfaces/SpoolReader.md) over a byte-faithful `Uint8Array` body.

## Remarks

Stores the raw bytes and decodes them as UTF-8 once at construction, then splits the decoded
string on `\n` and caches the resulting line array. All four `SpoolReader` methods resolve
synchronously from the cache — no I/O happens after construction. `byteLength()` reports the
true stored byte count (not the decoded character count), so it stays correct for multi-byte
content; `line()`/`readAll()` operate on the decoded text.

The reader accepts a `string` or a `Uint8Array`. A `string` is encoded as UTF-8 for the byte
count; a `Uint8Array` is held byte-faithfully (no lossy re-encode) and decoded for text reads.

Empty input yields a reader with `lineCount() === 0` and `byteLength() === 0`. A trailing
newline produces a final empty line: `"a\nb\n".split('\n') === ['a', 'b', '']`. This matches
the JavaScript `String.prototype.split` contract and lets a `lineCount()` consumer
distinguish "two lines, no trailing newline" from "two lines, trailing newline".

## Implements

* [`SpoolReader`](../../../../spooled_artifact/interfaces/SpoolReader.md)

## Constructors

### Constructor

```ts
new InMemorySpoolReader(content: string | Uint8Array<ArrayBufferLike>): InMemorySpoolReader;
```

Defined in: [batteries/storage/in\_memory/index.ts:48](https://github.com/NHTIO/ADK/blob/v1.20260605.0/src/batteries/storage/in_memory/index.ts#L48)

#### Parameters

| Parameter | Type                                          |
| --------- | --------------------------------------------- |
| `content` | `string` | `Uint8Array`<`ArrayBufferLike`> |

#### Returns

`InMemorySpoolReader`

## Methods

### byteLength()

```ts
byteLength(): number;
```

Defined in: [batteries/storage/in\_memory/index.ts:63](https://github.com/NHTIO/ADK/blob/v1.20260605.0/src/batteries/storage/in_memory/index.ts#L63)

Returns the total number of bytes in the underlying data.

#### Returns

`number`

The byte length of the underlying data.

#### Remarks

Used for reporting and token-estimation purposes. Byte length is distinct from character
length for multi-byte encodings.

#### Implementation of

[`SpoolReader`](../../../../spooled_artifact/interfaces/SpoolReader.md).[`byteLength`](../../../../spooled_artifact/interfaces/SpoolReader.md#bytelength)

***

### line()

```ts
line(index: number): string | undefined;
```

Defined in: [batteries/storage/in\_memory/index.ts:59](https://github.com/NHTIO/ADK/blob/v1.20260605.0/src/batteries/storage/in_memory/index.ts#L59)

Returns the line at the given 0-based index, or `undefined` when out of range.

#### Parameters

| Parameter | Type     | Description         |
| --------- | -------- | ------------------- |
| `index`   | `number` | 0-based line index. |

#### Returns

`string` | `undefined`

The raw line string (without trailing newline), or `undefined`.

#### Implementation of

[`SpoolReader`](../../../../spooled_artifact/interfaces/SpoolReader.md).[`line`](../../../../spooled_artifact/interfaces/SpoolReader.md#line)

***

### lineCount()

```ts
lineCount(): number;
```

Defined in: [batteries/storage/in\_memory/index.ts:67](https://github.com/NHTIO/ADK/blob/v1.20260605.0/src/batteries/storage/in_memory/index.ts#L67)

Returns the total number of lines in the underlying data.

#### Returns

`number`

The total line count.

#### Remarks

Required so consumers know when to stop iterating; the line count must remain stable for the
lifetime of the reader.

#### Implementation of

[`SpoolReader`](../../../../spooled_artifact/interfaces/SpoolReader.md).[`lineCount`](../../../../spooled_artifact/interfaces/SpoolReader.md#linecount)

***

### readAll()

```ts
readAll(): string;
```

Defined in: [batteries/storage/in\_memory/index.ts:71](https://github.com/NHTIO/ADK/blob/v1.20260605.0/src/batteries/storage/in_memory/index.ts#L71)

Returns the full underlying content as a single decoded string, byte-faithful to the source.

#### Returns

`string`

The full underlying content as a single string.

#### Remarks

Unlike [SpoolReader.line](../../../../spooled_artifact/interfaces/SpoolReader.md#line), this method preserves trailing newlines and any non-`\n`
line terminators (e.g. `\r\n`) present in the original bytes. It is the primitive that
powers `SpooledArtifact.asString()` — the round-trip-faithful alternative to assembling
the artifact body from per-line reads.

Implementations should make this O(n) in the size of the underlying data and may cache the
result if the read source is durable. Streaming implementations may choose not to cache.

#### Implementation of

[`SpoolReader`](../../../../spooled_artifact/interfaces/SpoolReader.md).[`readAll`](../../../../spooled_artifact/interfaces/SpoolReader.md#readall)
