Stream.Read to Bytearray Problem

  • Thread starter Thread starter Daniel von Fersen
  • Start date Start date
D

Daniel von Fersen

When I want to Read the Bytes 1000-2000 from a Stream into a ByteArray using

Stream.Read(byteArray,1000,2000)

they are written to the positions 1000-2000 in the byteArray.

but my Array is only 1000 items long Array(0-999), and i just want to have
the positions 1000-2000 from the stream!

How can i realize it that the bytes 1000-2000 are written to an Array of
length=1000

PS: The Stream is reading from the Internet

Thanx in advice
 
Daniel said:
When I want to Read the Bytes 1000-2000 from a Stream into a
ByteArray using

Stream.Read(byteArray,1000,2000)

they are written to the positions 1000-2000 in the byteArray.
but my Array is only 1000 items long Array(0-999), and i just want to
have the positions 1000-2000 from the stream!

How can i realize it that the bytes 1000-2000 are written to an Array
of length=1000

Trick answer: Never... counting from 1000 to 2000 gives you 1001 bytes ;-)

You misinterpreted the meaning of the parameters passed to Stream.Read().
The third parameter is not an end index (that would be a rather strange
notion regarding streams), but the numbers of bytes you want to read at
most. Thus, if you want to consume 1000 bytes starting at index 1000, use

someStream.Read(buffer, 1000, 1000);


BTW, there is a framework group that is more appropriate for these kinds of
questions.

Cheers,
 
Back
Top