<<.>> wrote:
> I now do the following... to just be safe on the casting limit.
>
> byte[] ba = new byte[br.BaseStream.Length]; // are you saying this
> should be null before and let .ReadBytes allocate it (if it does that?)
I'm saying you don't need to assign a value to it at all, as you assign
the value when you've done the read.
> if (br.BaseStream.Length <= int.MaxValue)
> {
> // we are within the casting limits so we can use the optimized
> method of reading
> ba = br.ReadBytes((int)br.BaseStream.Length);
> }
> else
> {
> // we are outside the limits (rare) so we can use the normal way
> of reading (slower)
> ArrayList b = new ArrayList();
> byte readByte = 0x00;
> while(br.BaseStream.Position < br.BaseStream.Length)
> {
> readByte = br.ReadByte();
> b.Add(readByte);
> }
>
> ba = (byte[])b.ToArray(typeof(byte));
> }
No, that second bit isn't a good idea. If you've got a file of over
2Gb, you most certainly *don't* want to create an ArrayList where each
element is a byte read from the file. It would take at least 12 times
the file size - so you'd end up with a memory usage of *at least* 24Gb.
Not pretty.
Do you really want to create an array that is the size of the whole
file, if it's more than 2Gb? I would expect any sane use of such a file
to be either something which can discard the bytes as it reads and
processes them, or something which seeks around within the file. A
safer bet is probably to throw an exception.
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too