A C# Stream that reads and writes to a Unity NativeArray<byte>
You might use this in the same way as a regular C# MemoryStream. However if you use a NativeArray (and a NativeArrayIoStream around it) then the NativeArray will bypass the managed heap, reducing fragmentation.
Just create the NativeArrayIoStream, passing in an existing NativeArray:
NativeArray<byte> array = ...;
using (var stream = new NativeArrayIoStream(array)) {
// e.g.
someJsonDocument.WriteTo(stream)
}
Dispose()-ing the stream does not dispose the underlying array. This is by design. Adding a flag to do so is a possible feature request.