///
/// 变长的RingBuffer
///
private class RingBuffer
{
byte[] _buffer;
int _read;
int _write;
int _count;//有效数据长度
///
/// 缓存区总长度
///
///
protected int length
{
get
{
lock ( this . _buffer . SyncRoot )
{
return this . _buffer . Length;
}
}
}
///
/// 存有的数据数量
///
///
public int Count
{
get
{
return this . _count;
}
}
public RingBuffer ( int size )
{
this . _buffer = new byte [ size ];
this . _read = 0;
this . _write = 0;
}
public bool IsEmpty ( )
{
lock ( this . _buffer . SyncRoot )
{
return this . Count == 0;
}
}
public void Push ( byte element )
{
lock ( this . _buffer . SyncRoot )
{
if ( this . willFull ( 1 ) )
{
this . expendBuffer ( 1 );
}
{
this . _buffer [ this . _write ] = element;
this . _write = ( this . _write + 1 ) % this . length;
this . _count++;
}
}
}
public void Push ( byte [ ] elements )
{
lock ( this . _buffer . SyncRoot )
{
if ( this . willFull ( elements . Length ) )
{
this . expendBuffer ( elements . Length );
}
if ( this . _write + elements . Length > this . length )
{
//需要循环写入
//1、把尾部的剩余空间写满
int tailWriteLength = this . length - this . _write;
Buffer . BlockCopy ( elements , 0 , this . _buffer , this . _write , tailWriteLength );
//2、从头开始写完剩下的部分
int headWriteLength = elements . Length - tailWriteLength;
Buffer . BlockCopy ( elements , tailWriteLength , this . _buffer , 0 , headWriteLength );
this . _write = headWriteLength;
}
else
{
Buffer . BlockCopy ( elements , 0 , this . _buffer , this . _write , elements . Length );
this . _write += elements . Length;
}
this . _count += elements . Length;
}
}
public byte Top ( )
{
lock ( this . _buffer . SyncRoot )
{
if ( this.Count < 1 )
throw new ArgumentOutOfRangeException ( );
return this . _buffer [ this . _read ];
}
}
public byte [ ] Top ( int size )
{
lock ( this . _buffer . SyncRoot )
{
if ( size > this . Count )
throw new ArgumentOutOfRangeException ( );
byte [ ] result = new byte [ size ];
if ( this . _read + size > this . length )
{
//越界了,需要分两段读取
//1、把尾部的剩余空间读完
int tailReadLength = this . length - this . _read;
Buffer . BlockCopy ( this . _buffer , this . _read , result , 0 , tailReadLength );
//2、从头开始读完剩下的部分
int headReadLength = size - tailReadLength;
Buffer . BlockCopy ( this . _buffer , 0 , result , tailReadLength , headReadLength );
}
else
{
Buffer . BlockCopy ( this . _buffer , this . _read , result , 0 , size );
}
return result;
}
}
public byte Pop ( )
{
lock ( this . _buffer . SyncRoot )
{
this . _count--;
this . _read = ( this . _read + 1 ) % this . length;
return this . Top();
}
}
public byte [ ] Pop ( int size )
{
byte [ ] result = this . Top ( size );
//Pop和Top的区别只是Pop会移动起点的位置
this . _count -= size;
this . _read = ( this . _read + size ) % this . length;
return result;
}
public bool IsFull ( )
{
lock ( this . _buffer . SyncRoot )
{
return this . Count == this . length;
}
}
protected bool willFull ( int newDataSize )
{
lock ( this . _buffer . SyncRoot )
{
//已有的数据+新的数据长度已经溢出
return newDataSize + this . Count > this . length;
}
}
protected void expendBuffer ( int newDataSize )
{
lock(this._buffer.SyncRoot)
{
byte [ ] oldBuffer = this . _buffer;
//最优的buffer长度应该是2的指数倍
int expendPows =( int ) Math . Log ( oldBuffer . Length + newDataSize , 2 ) + 1;
int expendSize = ( int ) Math . Pow ( 2 , expendPows );
this . _buffer = new byte [ expendSize ];
Buffer . BlockCopy ( oldBuffer , 0 , this . _buffer , 0 , oldBuffer . Length );
}
}
}
RingBuffer
最新推荐文章于 2021-11-19 17:02:03 发布