# UnsafeStream

> A set of untyped, append-only buffers. Allows for concurrent reading and concurrent writing without synchronization.

## Definition

* **Type:** Struct
* **Namespace:** [Unity.Collections.LowLevel.Unsafe](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe.md)
* **Assembly:** UnityEngine.ManagedKernelModule
* **Implements:** [INativeDisposable](/engine/6000.7/script-reference/unity/collections/inativedisposable.md), [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public struct UnsafeStream : INativeDisposable, IDisposable
```

## Remarks

As long as each individual buffer is written in one thread and read in one thread, multiple threads can read and write the stream concurrently, "e.g." while thread "A" reads from buffer "X" of a stream, thread "B" can read from buffer "Y" of the same stream.

Each buffer is stored as a chain of blocks. When a write exceeds a buffer's current capacity, another block is allocated and added to the end of the chain. Effectively, expanding the buffer never requires copying the existing data (unlike, for example, with [NativeList\<T>](/engine/6000.7/script-reference/unity/collections/nativelist1.md)).

**All writing to a stream should be completed before the stream is first read. Do not write to a stream after the first read.**

Writing is done with [NativeStream.Writer](/engine/6000.7/script-reference/unity/collections/nativestream/writer.md), and reading is done with [NativeStream.Reader](/engine/6000.7/script-reference/unity/collections/nativestream/reader.md). An individual reader or writer cannot be used concurrently across threads. Each thread must use its own.

The data written to an individual buffer can be heterogeneous in type, and the data written to different buffers of a stream can be entirely different in type, number, and order. Just make sure that the code reading from a particular buffer knows what to expect to read from it.

## Constructors

| Constructor                                                                                                                                                             | Description                                          |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| [UnsafeStream(System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle)](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/ctor.md) | Initializes and returns an instance of UnsafeStream. |

## Properties

| Property                                                                                                       | Description                                                       |
| -------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| [ForEachCount](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/foreachcount.md) | The number of buffers in this stream.                             |
| [IsCreated](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/iscreated.md)       | Whether this stream has been allocated (and not yet deallocated). |

## Methods

| Method                                                                                                           | Description                                                      |
| ---------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| [AsReader](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/asreader.md)           | Returns a reader of this stream.                                 |
| [AsWriter](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/aswriter.md)           | Returns a writer of this stream.                                 |
| [Count](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/count.md)                 | Returns the total number of items in the buffers of this stream. |
| [Dispose](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/dispose.md)             | Releases all resources (memory).                                 |
| [IsEmpty](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/isempty.md)             | Returns true if this stream is empty.                            |
| [ToNativeArray](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/tonativearray.md) | Returns a new NativeArray copy of this stream's data.            |

## Static Methods

| Method                                                                                                                   | Description                                           |
| ------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------- |
| [ScheduleConstruct](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/scheduleconstruct.md) | Creates and schedules a job to allocate a new stream. |

## Structs

| Struct                                                                                                                    | Description                                                                                                                        |
| ------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| [UnsafeStream.Reader](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/reader.md)           | Reads data from a buffer of an [UnsafeStream](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream.md).  |
| [UnsafeStream.ReaderState](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/readerstate.md) | State of a [UnsafeStream.Reader](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/reader.md).        |
| [UnsafeStream.Writer](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream/writer.md)           | Writes data into a buffer of an [UnsafeStream](/engine/6000.7/script-reference/unity/collections/lowlevel/unsafe/unsafestream.md). |
