@@ -5,21 +5,20 @@ namespace PooledStream
5
5
using System . Buffers ;
6
6
public class PooledMemoryStream : Stream
7
7
{
8
- ArrayPool < byte > m_Pool ;
9
- byte [ ] _currentbuffer = null ;
10
- bool _CanWrite = false ;
11
- long _Length = 0 ;
12
- long _Position = 0 ;
13
- bool _FromPool = false ;
8
+ /// <summary>create writable memory stream with default parameters</summary>
9
+ /// <remarks>buffer is allocated from ArrayPool<byte>.Shared</remarks>
14
10
public PooledMemoryStream ( )
15
11
: this ( ArrayPool < byte > . Shared )
16
12
{
17
13
}
14
+ /// <summary>create writable memory stream with specified ArrayPool</summary>
15
+ /// <remarks>buffer is allocated from ArrayPool</remarks>
18
16
public PooledMemoryStream ( ArrayPool < byte > pool )
19
17
: this ( pool , 4096 )
20
18
{
21
- m_Pool = pool ;
22
19
}
20
+ /// <summary>create writable memory stream with ensuring buffer length</summary>
21
+ /// <remarks>buffer is allocated from ArrayPool</remarks>
23
22
public PooledMemoryStream ( ArrayPool < byte > pool , int capacity )
24
23
{
25
24
m_Pool = pool ;
@@ -28,12 +27,15 @@ public PooledMemoryStream(ArrayPool<byte> pool, int capacity)
28
27
_Length = 0 ;
29
28
_CanWrite = true ;
30
29
}
31
- public PooledMemoryStream ( ArrayPool < byte > pool , byte [ ] data , int offset , int length )
30
+ /// <summary>create readonly MemoryStream without buffer copy</summary>
31
+ /// <remarks>data will be read from 'data' parameter</summary>
32
+ public PooledMemoryStream ( ArrayPool < byte > pool , byte [ ] data )
32
33
{
33
34
m_Pool = pool ;
34
35
_currentbuffer = data ;
35
- _Length = length ;
36
+ _Length = data . Length ;
36
37
_CanWrite = false ;
38
+ _FromPool = false ;
37
39
}
38
40
public override bool CanRead
39
41
{
@@ -65,13 +67,20 @@ public override void Flush()
65
67
public override int Read ( byte [ ] buffer , int offset , int count )
66
68
{
67
69
int readlen = count > ( int ) ( _Length - _Position ) ? ( int ) ( _Length - _Position ) : count ;
68
- Buffer . BlockCopy ( _currentbuffer
69
- , ( int ) _Position
70
- , buffer , offset
71
- , readlen )
72
- ;
73
- _Position += readlen ;
74
- return readlen ;
70
+ if ( readlen > 0 )
71
+ {
72
+ Buffer . BlockCopy ( _currentbuffer
73
+ , ( int ) _Position
74
+ , buffer , offset
75
+ , readlen )
76
+ ;
77
+ _Position += readlen ;
78
+ return readlen ;
79
+ }
80
+ else
81
+ {
82
+ return 0 ;
83
+ }
75
84
}
76
85
77
86
public override long Seek ( long offset , SeekOrigin origin )
@@ -91,7 +100,7 @@ public override long Seek(long offset, SeekOrigin origin)
91
100
default :
92
101
throw new InvalidOperationException ( "unknown SeekOrigin" ) ;
93
102
}
94
- if ( _Position < 0 || _Position > _Length )
103
+ if ( _Position < 0 || _Position > _Length )
95
104
{
96
105
_Position = oldValue ;
97
106
throw new IndexOutOfRangeException ( ) ;
@@ -178,10 +187,16 @@ public byte[] ToArray()
178
187
return ret ;
179
188
}
180
189
/// <summary>Create ArraySegment for current stream data without allocation buffer</summary>
181
- /// <remarks>after disposing stream, manupilating buffer (read or write) may cause undefined behavior</remarks>
190
+ /// <remarks>After disposing stream, manupilating returned value (read or write) may cause undefined behavior</remarks>
182
191
public ArraySegment < byte > ToUnsafeArraySegment ( )
183
192
{
184
193
return new ArraySegment < byte > ( _currentbuffer , 0 , ( int ) _Length ) ;
185
194
}
195
+ ArrayPool < byte > m_Pool ;
196
+ byte [ ] _currentbuffer = null ;
197
+ bool _CanWrite = false ;
198
+ long _Length = 0 ;
199
+ long _Position = 0 ;
200
+ bool _FromPool = false ;
186
201
}
187
202
}
0 commit comments