Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileStream, read data chunk from big file. Filesize bigger than int. How to set the offset?

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?

like image 819
Chris Avatar asked Aug 18 '11 12:08

Chris


2 Answers

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.

like image 200
Marc Gravell Avatar answered Sep 30 '22 05:09

Marc Gravell


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

like image 30
Yahia Avatar answered Sep 30 '22 07:09

Yahia