diff --git a/PooledStream.Test/TestPooledMemoryStream.cs b/PooledStream.Test/TestPooledMemoryStream.cs index b03ff4d..0f92b8e 100644 --- a/PooledStream.Test/TestPooledMemoryStream.cs +++ b/PooledStream.Test/TestPooledMemoryStream.cs @@ -47,7 +47,7 @@ public void TestWriteMiddle() public void TestReadOnly() { var data = new byte[] { 1, 2, 3, 4 }; - using (var stm = new PooledMemoryStream(ArrayPool.Shared, data, 0, data.Length)) + using (var stm = new PooledMemoryStream(ArrayPool.Shared, data)) { Assert.True(stm.CanRead); Assert.False(stm.CanWrite); diff --git a/PooledStream/PooledMemoryStream.cs b/PooledStream/PooledMemoryStream.cs index a8fe9a0..cf80a9c 100644 --- a/PooledStream/PooledMemoryStream.cs +++ b/PooledStream/PooledMemoryStream.cs @@ -5,21 +5,20 @@ namespace PooledStream using System.Buffers; public class PooledMemoryStream : Stream { - ArrayPool m_Pool; - byte[] _currentbuffer = null; - bool _CanWrite = false; - long _Length = 0; - long _Position = 0; - bool _FromPool = false; + /// create writable memory stream with default parameters + /// buffer is allocated from ArrayPool.Shared public PooledMemoryStream() : this(ArrayPool.Shared) { } + /// create writable memory stream with specified ArrayPool + /// buffer is allocated from ArrayPool public PooledMemoryStream(ArrayPool pool) : this(pool, 4096) { - m_Pool = pool; } + /// create writable memory stream with ensuring buffer length + /// buffer is allocated from ArrayPool public PooledMemoryStream(ArrayPool pool, int capacity) { m_Pool = pool; @@ -28,12 +27,15 @@ public PooledMemoryStream(ArrayPool pool, int capacity) _Length = 0; _CanWrite = true; } - public PooledMemoryStream(ArrayPool pool, byte[] data, int offset, int length) + /// create readonly MemoryStream without buffer copy + /// data will be read from 'data' parameter + public PooledMemoryStream(ArrayPool pool, byte[] data) { m_Pool = pool; _currentbuffer = data; - _Length = length; + _Length = data.Length; _CanWrite = false; + _FromPool = false; } public override bool CanRead { @@ -65,13 +67,20 @@ public override void Flush() public override int Read(byte[] buffer, int offset, int count) { int readlen = count > (int)(_Length - _Position) ? (int)(_Length - _Position) : count; - Buffer.BlockCopy(_currentbuffer - , (int)_Position - , buffer, offset - , readlen) - ; - _Position += readlen; - return readlen; + if (readlen > 0) + { + Buffer.BlockCopy(_currentbuffer + , (int)_Position + , buffer, offset + , readlen) + ; + _Position += readlen; + return readlen; + } + else + { + return 0; + } } public override long Seek(long offset, SeekOrigin origin) @@ -91,7 +100,7 @@ public override long Seek(long offset, SeekOrigin origin) default: throw new InvalidOperationException("unknown SeekOrigin"); } - if(_Position < 0 || _Position > _Length) + if (_Position < 0 || _Position > _Length) { _Position = oldValue; throw new IndexOutOfRangeException(); @@ -178,10 +187,16 @@ public byte[] ToArray() return ret; } /// Create ArraySegment for current stream data without allocation buffer - /// after disposing stream, manupilating buffer(read or write) may cause undefined behavior + /// After disposing stream, manupilating returned value(read or write) may cause undefined behavior public ArraySegment ToUnsafeArraySegment() { return new ArraySegment(_currentbuffer, 0, (int)_Length); } + ArrayPool m_Pool; + byte[] _currentbuffer = null; + bool _CanWrite = false; + long _Length = 0; + long _Position = 0; + bool _FromPool = false; } } \ No newline at end of file