RingBuffer
来源:互联网 发布:it名片设计 编辑:程序博客网 时间:2024/05/12 19:50
////// 变长的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 );}}}
0 0
- RingBuffer
- ringbuffer
- RingBuffer 数据结构
- Ringbuffer 范例
- ringbuffer剖析
- RingBuffer 笔记
- RingBuffer的操作與應用
- RingBuffer队列多线程应用
- Lmax.distruptor RingBuffer
- Ringbuffer为什么这么快?
- Disruptor 之 Ringbuffer
- Disruptor 之 Ringbuffer读取
- Disruptor 之 写入Ringbuffer
- RingBuffer源代码分析
- Disruptor 写入 Ringbuffer
- 环形缓冲区的应用ringbuffer
- 环形缓冲区的应用ringbuffer
- golang实现ringbuffer(不定长)
- iOS APP开发整理--UITabBarController,UINavigationController
- 【数据库】:关于语言连接数据库的一个认识
- Spring AOP进行日志记录,管理
- String类的深拷贝
- 修改用户名 及 可能出现的问题
- RingBuffer
- iOS中Cookie的使用
- 影响黄金价格的因素
- Android5.x 新控件之RecyclerView使用总结
- 随机数生成工具random
- Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant)
- 单线程模型中Message,Handler,MessageQueue,Looper之间的关系
- SYS_REFCURSOR & REF CURSOR
- hdu5646(数学)