Skip to content

Instantly share code, notes, and snippets.

@cmpunches
Created September 1, 2015 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmpunches/7922f3613d09a7dc94c6 to your computer and use it in GitHub Desktop.
Save cmpunches/7922f3613d09a7dc94c6 to your computer and use it in GitHub Desktop.
POC: Stream.Capacity > int32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
namespace StreamExtensionPOC
{
class Program
{
unsafe static void Main(string[] args)
{
Console.Write("\nEnter the size you'd like your test stream to be in Gigabytes: ");
string UserInput = "1";
UserInput = Console.ReadLine().ToString();
long LongInput = Convert.ToInt64(UserInput);
long ProcessedInput = LongInput;
long _StreamCapacityGigabytes = LongInput;
long _StreamCapacityBytes = 1073741824L * _StreamCapacityGigabytes;
const int _bufferLengthBytes = 1024 * 1024 * 1024;
Console.WriteLine("Press Enter to Begin....");
Console.ReadLine();
// allocate unmanaged memory buffer
IntPtr StreamPointer = new IntPtr(_StreamCapacityBytes);
IntPtr AllocationHandle = Marshal.AllocHGlobal(StreamPointer);
Byte* strmBuffer = (Byte*)AllocationHandle;
try
{
long len;
// Create the stream and write to it
using (UnmanagedMemoryStream ms = new UnmanagedMemoryStream(strmBuffer, _bufferLengthBytes, _StreamCapacityBytes, FileAccess.ReadWrite))
{
Console.WriteLine("Filling memory stream with {0} bytes using numbers in sequential order...", _StreamCapacityBytes);
using (StreamWriter writer = new StreamWriter(ms))
{
for (long i = 0; ms.Position <= _StreamCapacityBytes; i++)
{
if ((ms.Position % 1048576) == 0)
{
Console.WriteLine("Processed {0} megabytes...", (ms.Position / (1024 * 1024)));
Console.WriteLine("");
Console.WriteLine("");
}
try
{
writer.WriteLine(i);
}
catch
{
// Why the hell doesn't this catch?
Console.WriteLine(ms.Position.ToString());
}
}
Console.WriteLine(ms.Position.ToString());
// Get the length
len = ms.Position;
}
}
Console.WriteLine("Completed Write. Play back?");
Console.ReadLine();
Console.WriteLine("Reading memory stream of {0} numbers in sequential order...", _StreamCapacityBytes);
// Now read back from the stream
using (UnmanagedMemoryStream ms = new UnmanagedMemoryStream(strmBuffer, len))
{
using (StreamReader reader = new StreamReader(ms))
{
for (long i = 0; ms.Position <= _StreamCapacityBytes; i++)
{
if ((ms.Position % 1048576) == 0)
{
Console.WriteLine("Processed {0} megabytes...", (ms.Position / (1024 * 1024)));
}
int x = int.Parse(reader.ReadLine());
if (x != i)
{
throw new ApplicationException("Oops.");
}
}
Console.WriteLine(ms.Position.ToString());
}
}
}
finally
{
// Free the unmanaged memory
Marshal.FreeHGlobal(AllocationHandle);
}
Console.Write("Press Enter to exit...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment