当前位置: 首页>>代码示例>>C#>>正文


C# IStream.ReadBlock方法代码示例

本文整理汇总了C#中IStream.ReadBlock方法的典型用法代码示例。如果您正苦于以下问题:C# IStream.ReadBlock方法的具体用法?C# IStream.ReadBlock怎么用?C# IStream.ReadBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IStream的用法示例。


在下文中一共展示了IStream.ReadBlock方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Insert

        /// <summary>
        /// Inserts space into a stream by copying everything back by a certain number of bytes.
        /// </summary>
        /// <param name="stream">The stream to insert space into.</param>
        /// <param name="size">The size of the space to insert.</param>
        /// <param name="fill">The byte to fill the inserted space with. See <see cref="Fill"/>.</param>
        public static void Insert(IStream stream, int size, byte fill)
        {
            if (size == 0)
                return;
            if (size < 0)
                throw new ArgumentException("The size of the data to insert must be >= 0");
            if (stream.Position == stream.Length)
                return; // Seeking past the end automatically increases the file size

            const int BufferSize = 0x1000;
            byte[] buffer = new byte[BufferSize];
            int endPos = (int)stream.Position;
            int oldLength = (int)stream.Length;
            int pos = Math.Max(endPos, oldLength - BufferSize);
            while (pos >= endPos)
            {
                int read = Math.Min(BufferSize, oldLength - pos);
                stream.SeekTo(pos);
                stream.ReadBlock(buffer, 0, read);

                stream.SeekTo(pos + size);
                stream.WriteBlock(buffer, 0, read);
                pos -= read;
            }

            stream.SeekTo(endPos);
            Fill(stream, fill, size);
        }
开发者ID:YxCREATURExY,项目名称:Assembly,代码行数:34,代码来源:StreamUtil.cs

示例2: Copy

        /// <summary>
        ///     Copies data between two locations in the same stream.
        ///     The source and destination areas may overlap.
        /// </summary>
        /// <param name="stream">The stream to copy data in.</param>
        /// <param name="originalPos">The position of the block of data to copy.</param>
        /// <param name="targetPos">The position to copy the block to.</param>
        /// <param name="size">The number of bytes to copy.</param>
        public static void Copy(IStream stream, long originalPos, long targetPos, long size)
        {
            if (size == 0)
                return;
            if (size < 0)
                throw new ArgumentException("The size of the data to copy must be >= 0");

            const int BufferSize = 0x1000;
            var buffer = new byte[BufferSize];
            long remaining = size;
            while (remaining > 0)
            {
                var read = (int) Math.Min(BufferSize, remaining);

                if (targetPos > originalPos)
                    stream.SeekTo(originalPos + remaining - read);
                else
                    stream.SeekTo(originalPos + size - remaining);

                stream.ReadBlock(buffer, 0, read);

                if (targetPos > originalPos)
                    stream.SeekTo(targetPos + remaining - read);
                else
                    stream.SeekTo(targetPos + size - remaining);

                stream.WriteBlock(buffer, 0, read);
                remaining -= read;
            }
        }
开发者ID:ChadSki,项目名称:Assembly,代码行数:38,代码来源:StreamUtil.cs


注:本文中的IStream.ReadBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。