Beispiel #1
0
        /// <summary>
        /// 数据取出后就从当前列表中移除
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public ByteListStream getBytesTo(int len)
        {
            if (available() < len)
            {
                return(null);
            }
            if (Count < 1)
            {
                return(null);
            }
            ByteListStream bs     = new ByteListStream();
            int            offset = 0;

            while (offset < len)
            {
                ArraySegment <byte> seg = this[0];
                if (offset + seg.Count > len)
                {
                    var    level = ByteListPoolMgr.CaculBufLevel(seg.Count);
                    byte[] buf   = ByteListPoolMgr.AllocBuf(level);
                    Buffer.BlockCopy(seg.Array, seg.Offset, buf, 0, len - offset);
                    updateOffset(seg.Offset + (len - offset));
                    //
                    bs.Add(new ArraySegment <byte>(buf, 0, len - offset));
                    break;
                }
                else
                {
                    //Buffer.BlockCopy(seg.Array, seg.Offset, buf, 0, seg.Count);
                    offset += seg.Count;
                    this.RemoveAt(0);
                    //回收seg 如果不是通过ByteListPoolMgr申请的byte[] 并且长度不是64偶数倍(最大64)的数据将直接丢弃,由GC回收
                    //ByteListPoolMgr.FreeBuf(seg.Array);
                    bs.Add(seg);
                }
            }
            return(bs);
        }
Beispiel #2
0
        /// <summary>
        /// 把大字节数组分割成指定容量等级的ByteListStream
        /// </summary>
        /// <param name="largeBuf"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public static ByteListStream Slice(byte[] largeBuf, ByteListPool.BufferLevel level)
        {
            ByteListStream bs         = new ByteListStream();
            int            countleave = largeBuf.Length;
            int            offset     = 0;

            while (countleave > 0)
            {
                byte[] buf = AllocBuf(level);
                int    len = Math.Min(countleave, buf.Length);
                Buffer.BlockCopy(largeBuf, offset, buf, 0, len);
                bs.Add(new ArraySegment <byte>(buf, 0, len));
                offset     += len;
                countleave -= len;
            }
            return(bs);
        }
Beispiel #3
0
        /// <summary>
        /// ArraySegmen.array最大长度为64K,超过64K报错
        /// </summary>
        /// <returns></returns>
        public ByteListStream Copy()
        {
            ByteListStream bs = new ByteListStream();

            for (int i = 0; i < this.Count; i++)
            {
                var level = ByteListPoolMgr.CaculBufLevel(this[i].Count);
                if (level == ByteListPool.BufferLevel.None)
                {
                    throw new OutOfMemoryException("ArraySegmen.array最大长度超过64K");
                }
                byte[] buf = ByteListPoolMgr.AllocBuf(level);
                Buffer.BlockCopy(this[i].Array, this[i].Offset, buf, 0, this[i].Count);
                bs.Add(new ArraySegment <byte>(buf, 0, this[i].Count));
            }
            return(bs);
        }
Beispiel #4
0
        static void test()
        {
            //从缓存中取buf 用完回收 最大支持64K缓存数据
            Random         rn = new Random();
            ByteListStream bs = new ByteListStream();

            for (int i = 0; i < 10; i++)
            {
                //从缓存池中申请byte数组
                var v128 = ByteListPoolMgr.AllocBuf(ByteListPool.BufferLevel.b128);
                rn.NextBytes(v128);
                //大数据分片放入ByteListStream
                bs.Add(new ArraySegment <byte>(v128));
            }
            //读取数据
            byte[] buf = bs.getBytes(256);
            //覆盖读取数据
            bs.getBytes(ref buf, 0, 256);
            //销毁并回收关联的内存
            bs.Dispose();
        }