Ejemplo n.º 1
0
 /// <summary>
 /// 数据取出后就从当前列表中移除
 /// </summary>
 /// <param name="len"></param>
 /// <returns></returns>
 public BufferListStream getBytesTo(int len) {
     if (available() < len) return null;
     if (Count < 1) return null;
     BufferListStream bs = new BufferListStream();
     int offset = 0;
     while (offset < len) {
         BufferListSegment seg = this.m_List[0];
         if (offset + seg.Count > len) {
             var level = BufferListPoolMgr.CaculBufLevel(seg.Count);
             var buf = BufferListPoolMgr.AllocBuf(level);
             //Buffer.BlockCopy(seg.Array, seg.Offset, buf.Array, buf.Offset, len - offset);
             buf.SetDataFast(seg.Array, seg.Offset, buf.Offset, len - offset);
             updateOffset(seg.Offset + (len - offset));
             //
             bs.Add(buf);
             break;
         }
         else {
             //Buffer.BlockCopy(seg.Array, seg.Offset, buf, 0, seg.Count);
             offset += seg.Count;
             this.m_List.RemoveAt(0);
             //回收seg 如果不是通过BufferListPoolMgr申请的byte[] 并且长度不是64偶数倍(最大64)的数据将直接丢弃,由GC回收
             //BufferListPoolMgr.FreeBuf(seg.Array);
             bs.Add(seg);
         }
     }
     return bs;
 }
Ejemplo n.º 2
0
        static void test() {
            //从缓存中取buf 用完回收 最大支持64K缓存数据
            Random rn = new Random();
            BufferListStream bs = new BufferListStream();
            for (int i = 0; i < 4; i++) {
                //从缓存池中申请byte数组
                var v128 = BufferListPoolMgr.AllocBuf(BufferListPool.BufferLevel.b128);
                int offset = v128.Offset + 2;
                v128.SetLength(offset, 100);
                for (int j = 0; j < 100; j++) {
                    v128.Array[offset + j] = (byte)(j + 1);
                }
                //大数据分片放入BufferListStream
                bs.Add(v128);
                
            }
            //读取数据
            byte[] buf = bs.getBytes(128);
            byte[] buf2 = new byte[64];
            //覆盖读取数据
            bs.getBytes(ref buf2, 0, buf2.Length);
            //销毁并回收关联的内存
            bs.Dispose();

        }
Ejemplo n.º 3
0
 /// <summary>
 /// ArraySegmen.array最大长度为64K,超过64K报错
 /// </summary>
 /// <returns></returns>
 public BufferListStream Copy() {
     BufferListStream bs = new BufferListStream();
     for (int i = 0; i < this.Count; i++) {
         var level = BufferListPoolMgr.CaculBufLevel(this.m_List[i].Count);
         if (level == BufferListPool.BufferLevel.None) throw new OutOfMemoryException("ArraySegmen.array最大长度超过64K");
         var buf = BufferListPoolMgr.AllocBuf(level);
         //Buffer.BlockCopy(this[i].Source.Array, this[i].Source.Offset, buf.Source.Array, buf.Source.Offset, this[i].Source.Count);                
         buf.SetDataFast(this.m_List[i].Array, this.m_List[i].Offset, buf.Offset, this.m_List[i].Count);
         bs.Add(buf);
     }
     return bs;
 }
Ejemplo n.º 4
0
 protected void Dispose(bool Disposing) {
     if (m_IsDispose) {
         return;
     }
     m_IsDispose = true;
     //if (Disposing) {
     //主动释放,则回收
     for (int i = this.Count - 1; i >= 0; i--) {
         BufferListPoolMgr.FreeBuf(this.m_List[i]);
     }
     this.m_List.Clear();
     //}
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 数据取出后就从当前列表中移除
        /// </summary>
        /// <param name="fill2Data">填充数组,外面保证其剩余长度有效性</param>
        /// <param name="fill2BuffOffset"></param>
        /// <param name="len">外面保证取长度的有效性</param>
        public void getBytes(ref byte[] fill2Data, int fill2BuffOffset, int len) {
            if (available() < len) return;
            if (fill2Data.Length - fill2BuffOffset < len) return;
            if (Count < 1) return;
            //填充到数组
            int offset = 0;
            while (offset < len) {
                BufferListSegment seg = this.m_List[0];
                if (offset + seg.Count > len) {
                    Buffer.BlockCopy(seg.Array, seg.Offset, fill2Data, offset + fill2BuffOffset, len - offset);
                    updateOffset(seg.Offset + (len - offset));
                    break;
                }
                else {
                    Buffer.BlockCopy(seg.Array, seg.Offset, fill2Data, offset + fill2BuffOffset, seg.Count);
                    offset += seg.Count;
                    this.m_List.RemoveAt(0);
                    //回收seg 如果不是通过ByteListPoolMgr申请的byte[] 并且长度不是64偶数倍(最大64)的数据将直接丢弃,由GC回收
                    BufferListPoolMgr.FreeBuf(seg);
                }
            }

        }
Ejemplo n.º 6
0
 /// <summary>
 /// 数据取出后就从当前列表中移除
 /// </summary>
 /// <param name="len"></param>
 /// <returns></returns>
 public byte[] getBytes(int len) {
     if (available() < len) return null;
     if (Count < 1) return null;
     byte[] data = new byte[len];
     int offset = 0;
     while (offset < data.Length) {
         BufferListSegment seg = this.m_List[0];
         if (offset + seg.Count > len) {
             Buffer.BlockCopy(seg.Array, seg.Offset, data, offset, len - offset);
             updateOffset(seg.Offset + (len - offset));
             return data;
         }
         else {
             Buffer.BlockCopy(seg.Array, seg.Offset, data, offset, seg.Count);
             offset += seg.Count;
             this.m_List.RemoveAt(0);
             //回收seg( 如果是规范内的buf长度 128b/256b/512b/1024b/...)  
             //如果不是通过
             BufferListPoolMgr.FreeBuf(seg);
         }
     }
     // UnityEngine.Debug.Log("EXIT LOOP????!??!?!?!?? ");
     return data;
 }