Ejemplo n.º 1
0
        /// <summary>
        ///     Creates a new big-endian buffer whose content is a merged copy of of the specified arrays.
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="Array.Length" /> respectively.
        /// </summary>
        /// <param name="arrays"></param>
        /// <returns></returns>
        public static IByteBuffer CopiedBuffer(params byte[][] arrays)
        {
            switch (arrays.Length)
            {
            case 0:
                return(Empty);

            case 1:
                return(0u >= (uint)arrays[0].Length ? Empty : CopiedBuffer(arrays[0]));
            }

            // Merge the specified arrays into one array.
            int length = 0;

            foreach (byte[] a in arrays)
            {
                if (int.MaxValue - length < a.Length)
                {
                    ThrowHelper.ThrowArgumentException_CopyArray();
                }
                length += a.Length;
            }

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

            var mergedArray = new byte[length];

            for (int i = 0, j = 0; i < arrays.Length; i++)
            {
                byte[] a = arrays[i];
                PlatformDependent.CopyMemory(a, 0, mergedArray, j, a.Length);
                j += a.Length;
            }

            return(WrappedBuffer(mergedArray));
        }