public bool Write(byte[] buffer, int count)
        {
            try
            {
                lock (lockVAR)
                {
                    if (count > FreeBytes)
                    {
                        // Need a smart grow options
                        GrowBuffer(totalSize + (count - FreeBytes));
                    }

                    // the data will always fit. Just need to write it to the right blocks
                    int todoCount    = count;
                    int bufferOffset = 0;
                    // Step 1 fill current block
                    int block      = Position2BlockNumber(writePtr);
                    int offset     = Position2BlockNumberOffset(writePtr);
                    int writeCount = BLOCKSIZE - offset;
                    if (writeCount > todoCount)
                    {
                        writeCount = todoCount;
                    }
                    Buffer.BlockCopy(buffer, bufferOffset, blockList[block], offset, writeCount);
                    todoCount    -= writeCount;
                    bufferOffset += writeCount;

                    while (todoCount > 0)
                    {
                        block      = NextBlockNumber(block);
                        writeCount = BLOCKSIZE;
                        if (writeCount > todoCount)
                        {
                            writeCount = todoCount;
                        }

                        Buffer.BlockCopy(buffer, bufferOffset, blockList[block], 0, writeCount);
                        todoCount    -= writeCount;
                        bufferOffset += writeCount;
                    } // while

                    // Adjust writePtr
                    writePtr += count;
                    if (writePtr >= totalSize)
                    {
                        writePtr = writePtr - totalSize;
                    }
                    usedBytes += count;
                } //lock
            }
            catch (Exception e)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Error, string.Format("[CDR.LibRTMP.Media.CircularBlockBuffer.Write]: {0}", e.ToString()));
            }

            return(true);
        }
        /// <summary>
        /// Writes counts bytes from internal circular buffer to buffer
        /// return number of bytes write to the buffer in case
        /// requested count could not be fulfilled
        /// </summary>
        public int Read(byte[] buffer, int count)
        {
            int doneCount = 0;

            try
            {
                lock (lockVAR)
                {
                    if (count > usedBytes)
                    {
                        count = Convert.ToInt32(usedBytes);
                    }

                    // the data will always fit. Just need to write it to the right blocks
                    int todoCount    = count;
                    int bufferOffset = 0;
                    // Step 1 fill current block
                    int block     = Position2BlockNumber(readPtr);
                    int offset    = Position2BlockNumberOffset(readPtr);
                    int readCount = BLOCKSIZE - offset;
                    if (readCount > todoCount)
                    {
                        readCount = todoCount;
                    }
                    Buffer.BlockCopy(blockList[block], offset, buffer, bufferOffset, readCount);
                    todoCount    -= readCount;
                    bufferOffset += readCount;
                    doneCount    += readCount;

                    while (todoCount > 0)
                    {
                        block     = NextBlockNumber(block);
                        readCount = BLOCKSIZE;
                        if (readCount > todoCount)
                        {
                            readCount = todoCount;
                        }

                        Buffer.BlockCopy(blockList[block], 0, buffer, bufferOffset, readCount);
                        todoCount    -= readCount;
                        bufferOffset += readCount;
                        doneCount    += readCount;
                    } // while

                    // Adjust readPtr
                    readPtr += count;
                    if (readPtr >= this.totalSize)
                    {
                        readPtr = readPtr - this.totalSize;
                    }
                    // adjust size
                    this.usedBytes -= count;
                } //lock
            }
            catch (Exception e)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Error, string.Format("[CDR.LibRTMP.Media.CircularBlockBuffer.Read]: {0}", e.ToString()));
            }

            return(doneCount);
        }