Exemplo n.º 1
0
        /// <summary>
        /// Reserves a specific segment which can be used for various operations.
        /// </summary>
        /// <param name="length">The amount of bytes to reserve.</param>
        /// <returns>A delimited buffer segment.</returns>
        public BufferSegment Reserve(int length)
        {
            if (this.Disposed)
            {
                return(null);
            }

            lock (this.Lock)
            {
                // Acquire a block of memory
                var blockOffset = this.Acquire(length);

                // Acquire a segment
                var segment = BufferSegment.Acquire(this, this.Memory, blockOffset, length, blockOffset);

                // Return the segment
                return(segment);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes the array of bytes to the buffer and returns a segment which can be used for various operations.
        /// </summary>
        /// <param name="buffer">The array of bytes to write to this buffer.</param>
        /// <param name="offset">The starting offset in the byte array.</param>
        /// <param name="length">The amount of bytes to write.</param>
        /// <returns>A delimited buffer segment.</returns>
        public BufferSegment Write(byte[] buffer, int offset, int length)
        {
            if (this.Disposed || length == 0)
            {
                return(null);
            }

            lock (this.Lock)
            {
                // Acquire a block of memory
                var blockOffset = this.Acquire(length);

                // Acquire a segment
                var segment = BufferSegment.Acquire(this, this.Memory, blockOffset, length, blockOffset);

                // Write to the block
                Mem.Copy(buffer, offset, this.Memory, blockOffset, length);

                // Return the segment
                return(segment);
            }
        }