---
title: "Input and Output Streams"
url: https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams
language: matlab
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: MATLAB. 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), [JavaScript](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/js.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/input-and-output-streams/swift.md).

An OutputStream marshals Slice values into bytes with the [Ice encoding](https://docs.zeroc.com/ice/3.8/encoding.md), and an InputStream 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 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.

```matlab
item = Inventory.Item('widget', 12);

outputStream = Ice.OutputStream();
outputStream.startEncapsulation(Ice.FormatType.CompactFormat);
Inventory.Item.ice_write(outputStream, item);
outputStream.endEncapsulation();
bytes = outputStream.finished();

inputStream = Ice.InputStream(communicator, communicator.getEncoding(), bytes);
inputStream.startEncapsulation();
unmarshaled = Inventory.Item.ice_read(inputStream);
inputStream.endEncapsulation();
```

`Ice.OutputStream` takes an optional encoding version and class format, and `Ice.InputStream` takes the communicator, the encoding version and the bytes to unmarshal. `startEncapsulation` on an `OutputStream` takes the class format of the encapsulation. `finished` returns a `uint8` row vector.

## 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.

## Writing and Reading Slice Types

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

| Slice type | Write | Read |
| --- | --- | --- |
| Structure or enumeration `T` | `T.ice_write(outputStream, value)` | `T.ice_read(inputStream)` |
| Sequence or dictionary `T` | `T.write(outputStream, value)` | `T.read(inputStream)` |
| Interface `I` (proxy) | `outputStream.writeProxy(proxy)` | `IPrx.ice_read(inputStream)` |
| Class `C` | `outputStream.writeValue(instance)` | `inputStream.readValue(callback, 'C')` |
| Exception |  | `inputStream.throwException()` |

For a sequence of a built-in type such as `int` or `string`, use the stream's own methods, such as `writeIntSeq` and `readIntSeq`. `readValue` passes the instance to the callback, and takes the class name qualified with its package. `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.md)
- [Slice Loaders](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/matlab.md)
