2020-04-29 06:40:03 -07:00
|
|
|
using System.Collections;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
namespace System.IO
|
|
|
|
{
|
|
|
|
class MemoryStream : Stream
|
|
|
|
{
|
2021-10-04 09:41:48 -07:00
|
|
|
List<uint8> mMemory ~ delete _;
|
2019-08-23 11:56:54 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 09:41:48 -07:00
|
|
|
public this()
|
|
|
|
{
|
|
|
|
mMemory = new List<uint8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public this(List<uint8> memory)
|
|
|
|
{
|
|
|
|
mMemory = memory;
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
public override Result<int> TryRead(Span<uint8> 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<int> TryWrite(Span<uint8> 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);
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:04:17 -04:00
|
|
|
public override Result<void> Close()
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
2021-04-11 07:04:17 -04:00
|
|
|
return .Ok;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|