Skip to content

Commit

Permalink
add document comment
Browse files Browse the repository at this point in the history
  • Loading branch information
itn3000 committed Jul 7, 2017
1 parent a2e2218 commit 6bc57fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion PooledStream.Test/TestPooledMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void TestWriteMiddle()
public void TestReadOnly()
{
var data = new byte[] { 1, 2, 3, 4 };
using (var stm = new PooledMemoryStream(ArrayPool<byte>.Shared, data, 0, data.Length))
using (var stm = new PooledMemoryStream(ArrayPool<byte>.Shared, data))
{
Assert.True(stm.CanRead);
Assert.False(stm.CanWrite);
Expand Down
51 changes: 33 additions & 18 deletions PooledStream/PooledMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ namespace PooledStream
using System.Buffers;
public class PooledMemoryStream : Stream
{
ArrayPool<byte> m_Pool;
byte[] _currentbuffer = null;
bool _CanWrite = false;
long _Length = 0;
long _Position = 0;
bool _FromPool = false;
/// <summary>create writable memory stream with default parameters</summary>
/// <remarks>buffer is allocated from ArrayPool<byte>.Shared</remarks>
public PooledMemoryStream()
: this(ArrayPool<byte>.Shared)
{
}
/// <summary>create writable memory stream with specified ArrayPool</summary>
/// <remarks>buffer is allocated from ArrayPool</remarks>
public PooledMemoryStream(ArrayPool<byte> pool)
: this(pool, 4096)
{
m_Pool = pool;
}
/// <summary>create writable memory stream with ensuring buffer length</summary>
/// <remarks>buffer is allocated from ArrayPool</remarks>
public PooledMemoryStream(ArrayPool<byte> pool, int capacity)
{
m_Pool = pool;
Expand All @@ -28,12 +27,15 @@ public PooledMemoryStream(ArrayPool<byte> pool, int capacity)
_Length = 0;
_CanWrite = true;
}
public PooledMemoryStream(ArrayPool<byte> pool, byte[] data, int offset, int length)
/// <summary>create readonly MemoryStream without buffer copy</summary>
/// <remarks>data will be read from 'data' parameter</summary>
public PooledMemoryStream(ArrayPool<byte> pool, byte[] data)
{
m_Pool = pool;
_currentbuffer = data;
_Length = length;
_Length = data.Length;
_CanWrite = false;
_FromPool = false;
}
public override bool CanRead
{
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand Down Expand Up @@ -178,10 +187,16 @@ public byte[] ToArray()
return ret;
}
/// <summary>Create ArraySegment for current stream data without allocation buffer</summary>
/// <remarks>after disposing stream, manupilating buffer(read or write) may cause undefined behavior</remarks>
/// <remarks>After disposing stream, manupilating returned value(read or write) may cause undefined behavior</remarks>
public ArraySegment<byte> ToUnsafeArraySegment()
{
return new ArraySegment<byte>(_currentbuffer, 0, (int)_Length);
}
ArrayPool<byte> m_Pool;
byte[] _currentbuffer = null;
bool _CanWrite = false;
long _Length = 0;
long _Position = 0;
bool _FromPool = false;
}
}

0 comments on commit 6bc57fd

Please sign in to comment.