FileStream.Read() is defined as:
public override int Read(
byte[] array,
int offset,
int count
)
How can I read some bytes from an offset bigger than int.MaxValue?
Let's say I have a very big file and I want to read 100MB starting from position 3147483648.
How can I do that?
The offset here is the offset in the array at which to start writing. In your case, just set:
stream.Position = 3147483648;
and then use Read(). The offset is most commonly used when you know you need to read [n] bytes:
int toRead = 20, bytesRead;
while(toRead > 0 && (bytesRead = stream.Read(buffer, offset, toRead)) > 0)
{
toRead -= bytesRead;
offset += bytesRead;
}
if(toRead > 0) throw new EndOfStreamException();
this will read exactly 20 bytes into buffer (or throw an exception). Note that Read() is not guaranteed to read all the required data in one go, hence a loop incrementing an offset is usually required.
According to http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx the offset parameter is an offset inside the byte[] array:
array Type: System.Byte[] When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.
offset Type: System.Int32 The byte offset in array at which the read bytes will be placed.
count Type: System.Int32 The maximum number of bytes to read.
Read() just reads from the current positon which happens to be a long and should be set before calling Read() see http://msdn.microsoft.com/en-us/library/system.io.filestream.position.aspx
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