Example:
A stream does not store data and, in that sense, is not a data structure.
Abstract. Memory streaming operations (i.e., memory-to-memory data transfer with or without simple arithmetic/logical operations) are one of the most important tasks in general embedded/mobile computer systems. In this paper, we propose a technique to accelerate memory streaming operations.
No storage. Streams don't have storage for values; they carry values from a source (which could be a data structure, a generating function, an I/O channel, etc) through a pipeline of computational steps.
Streams involve three fundamental operations: Reading - transferring data from a stream into a data structure, such as an array of bytes. Writing - transferring data to a stream from a data source. Seeking - querying and modifying the current position within a stream.
If you stream your copying, i.e. read a buffer, write a buffer, read a buffer, write a buffer etc until you've run out of data, it will only take as much memory as the buffer size. I expect File.Copy to do this (in the native Windows code, admittedly).
If you want to do it yourself, use something like this:
public void CopyData(Stream input, Stream output)
{
byte[] buffer = new byte[32 * 1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
This will only take 32K however big the stream is.
EDIT: As noted in the comments, the streams may well have their own buffers as well, but the point is that you could still transfer a very large file without running out of memory.
If you call something like ReadToEnd()
then yes, the contents of the file will be loaded into memory. You're correct in guessing that using a buffer is the appropriate approach to ensure that only a subset of the file's data is loaded into memory at any given time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With