---
title: "Input and Output Streams"
url: https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams
language: js
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Input and Output Streams

Programming language: JavaScript. Other languages: [C++](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/java.md), [MATLAB](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/matlab.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/swift.md).

An [OutputStream](https://code.zeroc.com/ice/3.8/api/javascript/Ice/OutputStream.html) marshals Slice values into bytes with the [Ice encoding](https://docs.zeroc.com/ice/3.8/encoding.md), and an [InputStream](https://code.zeroc.com/ice/3.8/api/javascript/Ice/InputStream.html) unmarshals them. The code generated by the Slice compilers uses these streams to marshal and unmarshal the parameters of operations.

Your application uses these streams directly to:

- read the parameters and write the results in a servant that dispatches requests without generated code;
- store Slice values in a file or a database, or send them through a channel other than an Ice connection.

## Marshaling and Unmarshaling a Value

The examples on this page use the following Slice structure:

```slice
module Inventory
{
    struct Item
    {
        string name;
        int quantity;
    }
}
```

To marshal an `Item`, you create an `OutputStream`, start an [encapsulation](#encapsulations), write the value, end the encapsulation, and call `finished` to get the marshaled bytes. To unmarshal it, you create an `InputStream` over these bytes and read the encapsulation and the value in the same order.

```js
const item = new Inventory.Item("widget", 12);

const outputStream = new Ice.OutputStream(communicator);
outputStream.startEncapsulation();
Inventory.Item.write(outputStream, item);
outputStream.endEncapsulation();
const bytes = outputStream.finished();

const inputStream = new Ice.InputStream(communicator, bytes);
inputStream.startEncapsulation();
const unmarshaled = Inventory.Item.read(inputStream);
inputStream.endEncapsulation();
```

`finished` returns a `Uint8Array`.

## Encapsulations

An encapsulation holds marshaled values preceded by their size and their encoding version, as described in [Encoding for Encapsulations](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md#encoding-for-encapsulations). The Ice protocol carries the parameters of a request and the results of a reply in encapsulations. A servant that [dispatches requests](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/js.md) without generated code reads the parameters from the encapsulation at the start of the request's input stream, and creates its response with the dispatcher API, which puts the results it writes in an encapsulation.

## Writing and Reading Slice Types

See [OutputStream](https://code.zeroc.com/ice/3.8/api/javascript/Ice/OutputStream.html) and [InputStream](https://code.zeroc.com/ice/3.8/api/javascript/Ice/InputStream.html) for the constructors, the methods for the built-in types, and the errors the streams report.

The following table shows how you write and read the types you define in Slice:

| Slice type | Write | Read |
| --- | --- | --- |
| Structure `S` | `S.write(outputStream, value)` | `S.read(inputStream)` |
| Enumeration `E` | `outputStream.writeEnum(value)` | `inputStream.readEnum(E)` |
| Sequence or dictionary `T` | `THelper.write(outputStream, value)` | `THelper.read(inputStream)` |
| Interface `I` (proxy) | `outputStream.writeProxy(proxy)` | `inputStream.readProxy(IPrx)` |
| Class `C` | `outputStream.writeValue(instance)` | `inputStream.readValue(callback, C)` |
| Exception | `outputStream.writeException(error)` | `inputStream.throwException()` |

`throwException` reads an exception and throws it.

## See Also

- [Ice Encoding](https://docs.zeroc.com/ice/3.8/encoding.md)
- [Basic Data Encoding](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md)
- [Dispatcher API](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/js.md)
- [Slice Loaders](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/js.md)
