Beispiel #1
0
        /// <summary>
        ///     Creates a new big-endian buffer whose content  is a merged copy of the specified <see cref="Array" />.
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="IByteBuffer.Capacity" /> respectively.
        /// </summary>
        /// <param name="buffers">Buffers we're going to copy.</param>
        /// <returns>The new buffer that copies the contents of buffers.</returns>
        public static IByteBuffer CopiedBuffer(params IByteBuffer[] buffers)
        {
            switch (buffers.Length)
            {
            case 0:
                return(Empty);

            case 1:
                return(CopiedBuffer(buffers[0]));
            }

            // Merge the specified buffers into one buffer.
            int length = 0;

            foreach (IByteBuffer b in buffers)
            {
                int bLen = b.ReadableBytes;
                if ((uint)(bLen - 1) > SharedConstants.TooBigOrNegative) // bLen <= 0
                {
                    continue;
                }
                if (int.MaxValue - length < bLen)
                {
                    ThrowHelper.ThrowArgumentException_CopyBuffer();
                }

                length += bLen;
            }

            if (0u >= (uint)length)
            {
                return(Empty);
            }

            var mergedArray = new byte[length];

            for (int i = 0, j = 0; i < buffers.Length; i++)
            {
                IByteBuffer b    = buffers[i];
                int         bLen = b.ReadableBytes;
                _  = b.GetBytes(b.ReaderIndex, mergedArray, j, bLen);
                j += bLen;
            }

            return(WrappedBuffer(mergedArray));
        }