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


C# SparseStream.Read方法代码示例

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


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

示例1: Generate

        /// <summary>
        /// Creates a hex dump from a stream.
        /// </summary>
        /// <param name="stream">The stream to generate the hex dump from.</param>
        /// <param name="output">The destination for the hex dump.</param>
        public static void Generate(SparseStream stream, TextWriter output)
        {
            stream.Position = 0;
            byte[] buffer = new byte[1024 * 1024];

            foreach(var block in StreamExtent.Blocks(stream.Extents, buffer.Length))
            {
                long startPos = block.Offset * (long)buffer.Length;
                long endPos = Math.Min((block.Offset + block.Count) * (long)buffer.Length, stream.Length);
                stream.Position = startPos;

                while (stream.Position < endPos)
                {
                    int numLoaded = 0;
                    long readStart = stream.Position;
                    while (numLoaded < buffer.Length)
                    {
                        int bytesRead = stream.Read(buffer, numLoaded, buffer.Length - numLoaded);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        numLoaded += bytesRead;
                    }

                    for (int i = 0; i < numLoaded; i += 16)
                    {
                        bool foundVal = false;
                        if (i > 0)
                        {
                            for (int j = 0; j < 16; j++)
                            {
                                if (buffer[i + j] != buffer[i + j - 16])
                                {
                                    foundVal = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            foundVal = true;
                        }

                        if (foundVal)
                        {
                            output.Write("{0:x8}", i + readStart);

                            for (int j = 0; j < 16; j++)
                            {
                                if (j % 8 == 0)
                                {
                                    output.Write(" ");
                                }
                                output.Write(" {0:x2}", buffer[i + j]);
                            }

                            output.Write("  |");
                            for (int j = 0; j < 16; j++)
                            {
                                if (j % 8 == 0 && j != 0)
                                {
                                    output.Write(" ");
                                }
                                output.Write("{0}", (buffer[i + j] >= 32 && buffer[i + j] < 127) ? (char)buffer[i + j] : '.');
                            }
                            output.Write("|");

                            output.WriteLine();
                        }
                    }
                }
            }
        }
开发者ID:alexcmd,项目名称:DiscUtils,代码行数:79,代码来源:HexDump.cs


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