本文整理汇总了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();
}
}
}
}
}