public void AddToTail(Bytes bytes) { m_Count += bytes.Length; BytesPlusCount holder = new BytesPlusCount(bytes); m_DataQueue.Enqueue(new TRef(holder)); }
// Copy data out into a supplied buffer. If fPeek is true, it means we // would copy out the exact same bytes if called again. public int CopyFromHead(byte[] buffer, int offset, int length, bool fPeek) { int bytesCopied = 0; while ((m_DataQueue.Count > 0) && (bytesCopied < length)) { // Don't dequeue just yet, just peek TRef wrapper = (TRef)m_DataQueue.Peek(); VTable.Assert(wrapper != null); // Because we checked the count BytesPlusCount topChunk = (BytesPlusCount)(wrapper.Acquire()); int copied = topChunk.CopyOut(buffer, offset + bytesCopied, length - bytesCopied, fPeek); bytesCopied += copied; if ((!fPeek) && (topChunk.Empty)) { // We want to throw away the top chunk. To take pressure off // the finalizer, we'll explicitly free the ExHeap data vector. topChunk.Dispose(); m_DataQueue.Dequeue(); } else if ((!fPeek) && (!topChunk.Empty)) { // This should only happen because we filled up the // caller's buffer Debug.Assert(bytesCopied == length); // Put the data back into its wrapper wrapper.Release(topChunk); } else { // Release topChunk back wrapper.Release(topChunk); } } if (!fPeek) { m_Count -= bytesCopied; Debug.Assert(m_Count >= 0); } return(bytesCopied); }