File reading and possible overflow

  • Thread starter Thread starter Jason Barnett
  • Start date Start date
J

Jason Barnett

I'm trying to read an entire file into memory. I've read various articles
suggesting how. Regardless of the approach (File.ReadAllBytes(),
FileStream.Read(), etc.), it appears that the data is read into a byte array.
However, a file's length is of "long" data type and a byte array's capacity
is of "int" data type.

Is there a different approach that will read the entire file into memory
when it exceeds that of 2.1GB? Granted, I'm not likely to need to read a
file larger than 2.1GB at this time, but I'm curious if there is a more
accomodating approach.
 
Jason Barnett said:
I'm trying to read an entire file into memory. I've read various articles
suggesting how. Regardless of the approach (File.ReadAllBytes(),
FileStream.Read(), etc.), it appears that the data is read into a byte array.
However, a file's length is of "long" data type and a byte array's capacity
is of "int" data type.

Is there a different approach that will read the entire file into memory
when it exceeds that of 2.1GB? Granted, I'm not likely to need to read a
file larger than 2.1GB at this time, but I'm curious if there is a more
accomodating approach.

Well, if it really is over 2GB do you definitely still want to read it
into memory? It sounds unlikely to be a "friendly" thing to do...
 
Keep in mind that on a 32-bit system, the default allocatable memory for any
given application is limited to 2GB [1]. That memory would include the space
taken up by the executable, it's code, and it's stack.

While you can address an array outside of the int boundaries with
GetValue(long) you'll likely get an OutOfMemoryException before then.

[1] http://msdn2.microsoft.com/en-us/library/aa366778.aspx
 
Back
Top