Created
September 1, 2015 21:44
-
-
Save cmpunches/7922f3613d09a7dc94c6 to your computer and use it in GitHub Desktop.
POC: Stream.Capacity > int32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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