---
title: "Input and Output Streams"
url: https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams
language: cpp
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: C++. Other languages: [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), [JavaScript](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/js.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/cpp/classIce_1_1OutputStream.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/cpp/classIce_1_1InputStream.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:

- write the in-parameters of a dynamic invocation (`ice_invoke`) and read its results;
- 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.

```cpp
Inventory::Item item{"widget", 12};

Ice::OutputStream out{communicator};
out.startEncapsulation();
out.write(item);
out.endEncapsulation();
std::vector<std::byte> bytes;
out.finished(bytes);

Ice::InputStream in{communicator, bytes};
in.startEncapsulation();
Inventory::Item unmarshaled;
in.read(unmarshaled);
in.endEncapsulation();
```

`finished(std::vector<std::byte>& bytes)` copies the marshaled bytes into `bytes`. The `finished()` overload without parameters returns a pair of pointers to the stream's own buffer instead; these pointers remain valid for the lifetime of the stream.

## 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. The in-parameters you pass to `ice_invoke` and the results it returns are such encapsulations, which you marshal and unmarshal as in the example above. A servant that [dispatches requests](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/cpp.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/cpp/classIce_1_1OutputStream.html) and [InputStream](https://code.zeroc.com/ice/3.8/api/cpp/classIce_1_1InputStream.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:

`write` and `read` are templates that accept any type the Slice compiler generates:

| Slice type | Write | Read |
| --- | --- | --- |
| Structure | `out.write(item)` | `in.read(item)` |
| Enumeration | `out.write(color)` | `in.read(color)` |
| Sequence or dictionary | `out.write(items)` | `in.read(items)` |
| Proxy (`std::optional<P>`) | `out.write(proxy)` | `in.read(proxy)` |
| Class (`std::shared_ptr`) | `out.write(instance)` | `in.read(instance)` |
| Exception | `out.writeException(error)` | `in.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/cpp.md)
- [Slice Loaders](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/cpp.md)
