I have some large data files which I can retrieve in chunks of let's say 32kb using an API specially designed for this. One usage of the API can be the following:
LargeFileAPI lfa = new LargeFileAPI("file1.bin");
bool moredata = true;
List<byte[]> theWholeFile = new List<byte[]>();
while ( moredata )
{
byte[] arrayRead = new byte[32768];
moredata = lfa.Read(arrayRead);
theWholeFile.Add(arrayRead);
}
The problem with the above is that reading from it takes up as much memory as the size of the large file (let's say 100Mb). And since I want to pass this as a return result to a WCF service, I would prefer to use a Stream as the output of the service.
How can I create a Stream object from this and pass it as a return parameter to a WCF service without occupying the full file size in memory?
I was thinking of creating a class LargeFileStream inheriting from
System.IO.Stream
and override the Read method. But I cannot seem to figure out how to work through the fact that Stream.Read takes an offset parameter and a number of bytes to read, because the API I mentioned requires reading a fixed number of bytes for each read. Moreover, what about all the other methods I have to override, such as Flush(), Position and whatever else there is. What should they imeplement? I am asking because I have no idea what other functions than Stream.Read(), WCF would call when I am reading the stream from the client (the caller of the WCF service).
Moreover, I need it to be serializable so that it can be an output parameter to a WCF service.
Thanks Jihad