/// <summary>
        /// Removes a front portion of the memory stream.
        /// </summary>
        public static void TrimStart(this ChunkedMemoryStream stream, int length)
        {
            // TODO: change RecyclableMemoryStream to allow every block to have
            // an offset, so we don't need to shift data

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            stream.Position = 0;

            if (length == 0)
            {
                return;
            }

            var helper = stream.GetBlockOffset(length);

            stream.RemoveBlockRange(0, helper.Block);

            // Now we need to shift data in trailing blocks.
            for (int i = 0; i < stream.BlockCount; i++)
            {
                var currentBlock = stream.GetBlock(i);

                int previous = i - 1;
                if (previous >= 0)
                {
                    var previousBlock = stream.GetBlock(previous);

                    var bytesToMoveBack = currentBlock.Slice(0, helper.Offset);
                    var backDst         = previousBlock[^ helper.Offset..];
示例#2
0
        /// <summary>
        /// Removes a front portion of the memory stream.
        /// </summary>
        public static void TrimStart(this ChunkedMemoryStream stream, int length)
        {
            // TODO: change ChunkedMemoryStream to allow every block to have
            //  an offset, so we don't need to shift data at least as much?

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            stream.Position = 0;

            if (length == 0)
            {
                return;
            }

            (int block, int offset) = stream.GetBlockOffset(length);
            stream.RemoveBlockRange(0, block);

            // Now we need to shift data in trailing blocks.
            for (int i = 0; i < stream.BlockCount; i++)
            {
                Memory <byte> currentBlock = stream.GetBlock(i);

                int previous = i - 1;
                if (previous >= 0)
                {
                    Memory <byte> bytesToMoveBack = currentBlock.Slice(0, offset);
                    Memory <byte> previousBlock   = stream.GetBlock(previous);
                    Memory <byte> backDestination = previousBlock[^ offset..];