Stream.Read to Bytearray Problem

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
 
J

Joerg Jooss

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top