/// <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; }
/// <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; }