FileStream's read/write method can take only integer
value as length. But FileStream
object returns length in long
. In this case, what if file size is larger than integer
value (approximate more than 2GB). Then how FileStream's read/write method handle long
value.
As the name suggests, a FileStream reads and writes to a file whereas a MemoryStream reads and writes to the memory. So it relates to where the stream is stored.
The size of the buffer used by FileStream for buffering. The default buffer size is 4096.
Use the FileStream class to read from, write to, open, and close files on a file system, and to manipulate other file-related operating system handles, including pipes, standard input, and standard output.
The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.
Then you read and write in multiple chunks. The CLR has a limit on the size of any particular object anyway (also around 2GB IIRC, even on a 64-bit CLR), so you couldn't have a byte array big enough for it to be a problem.
You should always loop when reading anyway, as you can't guarantee that a Read call will read as many bytes as you requested, even if there's more data to come.
EDIT: Reading in chunks:
byte[] buffer = new byte[1024 * 32];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
// Use the data you've read
}
Writing in chunks will depend on what you're writing... it's hard to talk about it in the abstract.
There is no need to directly write more than 2Gb of data in one call
If you would really have that amount buffered contiguously in memory (? maby as an unsafely acquired UnmanagedMemoryStream to implement a core dump?) you could easily batch the writes in multiple calls. It will get written to disk in blocks of 512k to max 4k on current hardware, anyway.
The great value of 'streaming' interfaces is that you can have it anywhich way. In fact, when you look into to it you will find that the CLR Arrays (and anything else) are actually bounded to 2GB
Since you have now confessed that you basically want to copy streams, you might be better served with an instant solution. There is File.Copy
File.Copy("file-a.txt", "file-new.txt");
Or there is the standard answer
Stream input
input.CopyTo(output); // .NET 4.0
// .NET 3.5 and others
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write (buffer, 0, read);
}
}
Don't forget about Flushing
, Closing
and Disposing
your Streams as appropriate if you are handling the streams manually.
Cheers
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