65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
|
|
// <copyright file="MemoryStreamVisualizerObjectSource.cs" company="PlaceholderCompany">
|
|||
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|||
|
|
// </copyright>
|
|||
|
|
|
|||
|
|
namespace MemoryStreamObjectSource;
|
|||
|
|
|
|||
|
|
using Microsoft.VisualStudio.DebuggerVisualizers;
|
|||
|
|
using System.IO;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Object source class for the MemoryStreamVisualizer.
|
|||
|
|
/// </summary>
|
|||
|
|
public class MemoryStreamVisualizerObjectSource : VisualizerObjectSource
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// How many rows will be transfered, at most, responding to a single request.
|
|||
|
|
/// </summary>
|
|||
|
|
public const int RowsCountPerRequest = 1024;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// How many bytes will be transfered for each row.
|
|||
|
|
/// </summary>
|
|||
|
|
public const int RowLength = 16;
|
|||
|
|
|
|||
|
|
/// <inheritdoc/>
|
|||
|
|
public override void TransferData(object target, Stream incomingData, Stream outgoingData)
|
|||
|
|
{
|
|||
|
|
if (target is not MemoryStream memoryStream)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
using BinaryReader binaryReader = new(incomingData);
|
|||
|
|
var index = binaryReader.ReadInt32(); // The extension will send the offset (Int32) to start reading from
|
|||
|
|
|
|||
|
|
using BinaryWriter binaryWriter = new(outgoingData);
|
|||
|
|
var backupPosition = memoryStream.Position;
|
|||
|
|
|
|||
|
|
// Will reply with the current MemoryStream.Position (Int64),
|
|||
|
|
// followed by MemoryStream.Length (Int64),
|
|||
|
|
// followed by up to 16KB of data retrieved from the MemoryStream
|
|||
|
|
binaryWriter.Write(backupPosition);
|
|||
|
|
binaryWriter.Write(memoryStream.Length);
|
|||
|
|
if (index < memoryStream.Length)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var data = new byte[RowsCountPerRequest * RowLength];
|
|||
|
|
memoryStream.Seek(index, SeekOrigin.Begin);
|
|||
|
|
var count = memoryStream.Read(data, 0, data.Length);
|
|||
|
|
binaryWriter.Write(data, 0, count);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
// Make sure to restore the MemoryStream to its original position
|
|||
|
|
memoryStream.Seek(backupPosition, SeekOrigin.Begin);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
binaryWriter.Flush();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|