@@ -42,9 +42,9 @@ public class PipelineStream : Stream
4242 private readonly RingBuffer ringBuffer ;
4343
4444 /// <summary>
45- /// 当写入完成后触发
45+ /// 当完成读取后触发
4646 /// </summary>
47- public event Action < Stream > OnWrote ;
47+ public event Action < Stream > OnRead ;
4848
4949 /// <summary>
5050 /// 是否已经被释放了
@@ -56,11 +56,6 @@ public class PipelineStream : Stream
5656 /// </summary>
5757 private volatile bool closed ;
5858
59- /// <summary>
60- /// 总流量
61- /// </summary>
62- public long TotalFlow { get ; private set ; }
63-
6459 /// <summary>
6560 /// 是否可以被读取
6661 /// </summary>
@@ -78,20 +73,30 @@ public override bool CanWrite
7873 }
7974
8075 /// <summary>
81- /// 偏移位置(不支持)
76+ /// 当前流的位置
77+ /// </summary>
78+ private long position ;
79+
80+ /// <summary>
81+ /// 流位置
8282 /// </summary>
8383 public override long Position
8484 {
85- get { throw new NotSupportedException ( ) ; }
85+ get { return position ; }
8686 set { throw new NotSupportedException ( ) ; }
8787 }
8888
89+ /// <summary>
90+ /// 流的长度
91+ /// </summary>
92+ private long length ;
93+
8994 /// <summary>
9095 /// 流的长度
9196 /// </summary>
9297 public override long Length
9398 {
94- get { throw new NotSupportedException ( ) ; }
99+ get { return length ; }
95100 }
96101
97102 /// <summary>
@@ -119,7 +124,6 @@ public PipelineStream(int capacity = 4096, int sleep = 1)
119124 {
120125 this . capacity = capacity . ToPrime ( ) ;
121126 this . sleep = Math . Max ( 0 , sleep ) ;
122- TotalFlow = 0 ;
123127 ringBuffer = new RingBuffer ( this . capacity , false ) ;
124128 }
125129
@@ -132,7 +136,7 @@ public PipelineStream(int capacity = 4096, int sleep = 1)
132136 }
133137
134138 /// <summary>
135- /// 偏移位置 (不支持)
139+ /// 设定流位置 (不支持)
136140 /// </summary>
137141 /// <param name="offset">偏移量</param>
138142 /// <param name="origin">偏移方向</param>
@@ -143,12 +147,12 @@ public override long Seek(long offset, SeekOrigin origin)
143147 }
144148
145149 /// <summary>
146- /// 设定流的长度(不支持)
150+ /// 设定流的长度
147151 /// </summary>
148152 /// <param name="value">长度</param>
149153 public override void SetLength ( long value )
150154 {
151- throw new NotSupportedException ( ) ;
155+ length = Math . Max ( 0 , value ) ;
152156 }
153157
154158 /// <summary>
@@ -198,7 +202,13 @@ public override int Read(byte[] buffer, int offset, int count)
198202 {
199203 var read = ringBuffer . Read ( buffer , offset , count ) ;
200204 this . count -= read ;
201- TotalFlow += read ;
205+ position += read ;
206+
207+ if ( OnRead != null )
208+ {
209+ OnRead ( this ) ;
210+ }
211+
202212 return read ;
203213 }
204214 finally
@@ -242,12 +252,6 @@ public override void Write(byte[] buffer, int offset, int count)
242252
243253 Guard . Requires < AssertException > ( ringBuffer . Write ( buffer , offset , count ) == count ) ;
244254 this . count += count ;
245-
246- if ( OnWrote != null )
247- {
248- OnWrote ( this ) ;
249- }
250-
251255 return ;
252256 }
253257 }
0 commit comments