# NativeStream

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

## Definition

* **Type:** Struct
* **Namespace:** [Unity.Collections](/engine/6000.7/script-reference/unity/collections.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 NativeStream : 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 with [NativeList\<T>](/engine/6000.7/script-reference/unity/collections/nativelist1.md), for example).

**All writing to a stream should be completed before the stream is first read. Do not write to a stream after the first read.** Violating these rules won't "necessarily" cause any problems, but they are the intended usage pattern.

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                                          |
| ------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| [NativeStream(System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle)](/engine/6000.7/script-reference/unity/collections/nativestream/ctor.md) | Initializes and returns an instance of NativeStream. |

## Properties

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

## Methods

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

## Static Methods

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

## Structs

| Struct                                                                                          | Description                                                                                                       |
| ----------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| [NativeStream.Reader](/engine/6000.7/script-reference/unity/collections/nativestream/reader.md) | Reads data from a buffer of a [NativeStream](/engine/6000.7/script-reference/unity/collections/nativestream.md).  |
| [NativeStream.Writer](/engine/6000.7/script-reference/unity/collections/nativestream/writer.md) | Writes data into a buffer of a [NativeStream](/engine/6000.7/script-reference/unity/collections/nativestream.md). |
