471,599 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

The size of buffer

ad
I new a buffer for read file:
How to detriminate the BolockSize when new the buffer?
-----------------------------------------------------------------------------------------------------------------------------------------
int BlockSize=????
System.IO.FileStream fsRead = new System.IO.FileStream("c:\Backup.bck",
System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[BlockSize];
System.Int32 size = fsRead.Read(buffer, 0, buffer.Length);
Nov 17 '05 #1
1 2422
Hi ad,
generally when you do not know the size of the file in advance, you can
just assign some arbritary size to your buffer and keep looking in a while
loop calling read until the FileStream.Read function returns a value < 0 at
which point you know there is no more data.

In the code example you gave, you know exactly the size of the file you are
reading because you can use the FileInfo object to get the size of the file
in bytes i.e.

System.IO.FileInfo fi = new FileInfo(@"c:\backup.bck");
long fileSize = fi.Length;

I would not make your buffer equal to the size of your file though, make it
some arbitrary size like 500K, for example, and keep looping until all the
data is read, this way if you are reading a large file or you want to cancel
the operation you still have chance to cancel the read operation by putting
extra code in your while loop to do so i.e.

System.IO.FileInfo fi = new FileInfo(@"c:\backup.bck");
long fileSize = fi.Length;

//have some boolean that can be set i.e. when the user clicks on
//a cancel button this would turn to false
bool keepReading = true;

byte[] arrData = new byte[500 * 1024];

long remainingData = fileSize;
int dataRead;
int offset = 0;

while(remainingData > 0 && keepReading)
{
dataRead = fs.Read(arrData, offset, remainingData);

if(dataRead <= 0)
{
//we are done
break;
}

remainingData -= dataRead;
}

Hope that helps
Mark R Dawson

"ad" wrote:
I new a buffer for read file:
How to detriminate the BolockSize when new the buffer?
-----------------------------------------------------------------------------------------------------------------------------------------
int BlockSize=????
System.IO.FileStream fsRead = new System.IO.FileStream("c:\Backup.bck",
System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[BlockSize];
System.Int32 size = fsRead.Read(buffer, 0, buffer.Length);

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Sharon | last post: by
11 posts views Thread by Stephan Steiner | last post: by
2 posts views Thread by Macca | last post: by
43 posts views Thread by Frodo Baggins | last post: by
4 posts views Thread by Zytan | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.