Why is FileStream.Length a long type, but the argument FileStream.Read - offset has a shorter length?

Why is FileStream.Length a long type, but the argument of FileStream.Read is the offset instead has a shorter int length?

Brian

+7
source share
5 answers

The offset parameter indicates where to start writing data to your array, the array parameter. It does not indicate an offset in the data files.

The offset parameter gives the byte offset in the array (buffer index) from which to start reading, and the count parameter gives the maximum number of bytes to be read from this stream. The return value is the actual number of bytes read, or zero if the end of the stream is reached. If the read operation is successful, the current position of the stream is increased by the number of bytes read. If an exception occurs, the current position of the stream does not change.

Source: FileStream.Read

+5
source

An offset is an index into the byte array where the data is placed. You cannot allocate an array larger than 2 GB, so there is no need for more to offset.

+6
source

I assume that you are referring to this method, which is overridden from the Stream base class.

The offset is the location in the array argument to put the bytes not the offset from the start of the FileStream . The array argument is of type byte[] , which cannot contain more than int.MaxValue elements.

+1
source

As an addition to mike z answer, why byte [] cannot be indexed more than int.MaxValue - the Array class implements IList using the object this[int index] { get; set; } object this[int index] { get; set; } object this[int index] { get; set; } , so the index can only be an integer.

+1
source

FileStream.Length gets the length in bytes of the stream and FileStream.Read reads the block of bytes from the stream. Thus, logically you will have more single bytes than blocks of these bytes. I guess for this reason, FileStream.Length requires a long type.

+1
source

All Articles