System.Net.ScatterGatherBuffers.Write C# (CSharp) Method

Write() private method

private Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void
        internal void Write(byte[] buffer, int offset, int count) {
            GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::Add() count:" + count.ToString());
            while (count > 0) {
                //
                // compute available space in current allocated buffer (0 if there's no buffer)
                //
                int available = Empty ? 0 : currentChunk.Buffer.Length - currentChunk.FreeOffset;
                GlobalLog.Assert(available >= 0, "ScatterGatherBuffers::Add()|available < 0");
                //
                // if the current chunk is is full, allocate a new one
                //
                if (available==0) {
                    // ask for at least count bytes so that we need at most one allocation
                    MemoryChunk newChunk = AllocateMemoryChunk(count);
                    if (currentChunk!=null) {
                        currentChunk.Next = newChunk;
                    }
                    //
                    // move ahead in the linked list (or at the beginning if this is the fist buffer)
                    //
                    currentChunk = newChunk;
                }
                int copyCount = count < available ? count : available;

                Buffer.BlockCopy(
                    buffer,                     // src
                    offset,                     // src index
                    currentChunk.Buffer,        // dest
                    currentChunk.FreeOffset,    // dest index
                    copyCount );                // total size to copy

                //
                // update offsets and counts
                //
                offset += copyCount;
                count -= copyCount;
                totalLength += copyCount;
                currentChunk.FreeOffset += copyCount;
            }
            GlobalLog.Print("ScatterGatherBuffers#" + ValidationHelper.HashString(this) + "::Add() totalLength:" + totalLength.ToString());
        }