2

I am having some trouble with reading a memory stream in chunks.

Dim ByteBuffer(4096) As Byte

While ProcessedBytes < FileLength

    BytesRead = MemoryStream.Read(ByteBuffer, 0, 4096)
    'Write the buffer to an output stream
    ProcessedBytes += BytesRead

End While

'MemoryStream.Read(ByteBuffer, 0, 4096)' is always returning zero. I have looked at 'MemoryStream.Length' and the stream definitely has some bytes in it.

2 Answers 2

2

If you're just trying to get the byte[] data out of the MemoryStream, you can call MemoryStream.ToArray():

Dim ByteBuffer() as Byte = MemoryStream.ToArray()

Otherwise, make sure to set your MemoryStream's Position to 0 prior to reading from it.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe it returns 0 because you reached the end of the stream. Make sure you set the position to 0 before starting reading your stream.

MemoryStream.Position = 0

or

MemoryStream.Seek(0, SeekOrigin.Begin)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.