RingBuffer

///

/// 变长的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 );

}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值