Background:
I'm using an API that for a certain call returns a System.IO.Stream
. The content is humongous and cannot be contained in RAM, and needs to be persisted. The size ranges from a couple of hundred MiB to a few GiB. The api is not something I can access other than the call I am making, but it has to be used. I cannot increase the amount of memory available to the program or switch to 64 bit.
What I've tried:
Simply doing
var output = api.getStream();
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter("C:\\temp\\myfile.extension");
streamWriter.Write(output);
fails not because I'm writing an arbitrary binary as a string, but rather due to that when the first line is run (where output is assigned) the program crashes with a System.InsufficientMemoryException
.
When trying the approaches outlined in the most upvoted answers to this post: How do I save a stream to a file in C#? I also get an out of memory error once the line where I get the stream from the api is called.
I've also tried approaches such as api.getStream.CopyTo(myFileStream)
as suggested in an answer to this post: Save and load MemoryStream to/from a file , but still get a system out of memory exception.
The resulting question:
Does anyone know how to write an arbitrarily large IO stream to file in C# when the stream is supplied from an api-call to an external library?