using System.Collections; namespace System.IO { class MemoryStream : Stream { List mMemory ~ delete _; int mPosition = 0; public override int64 Position { get { return mPosition; } set { mPosition = (.)value; } } public override int64 Length { get { return mMemory.Count; } } public override bool CanRead { get { return true; } } public override bool CanWrite { get { return true; } } public this() { mMemory = new List(); } public this(List memory) { mMemory = memory; } public override Result TryRead(Span data) { let count = data.Length; if (count == 0) return .Ok(0); int readBytes = Math.Min(count, mMemory.Count - mPosition); if (readBytes <= 0) return .Ok(readBytes); Internal.MemCpy(data.Ptr, &mMemory[mPosition], readBytes); mPosition += readBytes; return .Ok(readBytes); } public override Result TryWrite(Span data) { let count = data.Length; if (count == 0) return .Ok(0); int growSize = mPosition + count - mMemory.Count; if (growSize > 0) mMemory.GrowUnitialized(growSize); Internal.MemCpy(&mMemory[mPosition], data.Ptr, count); mPosition += count; return .Ok(count); } public override Result Close() { return .Ok; } } }