I struggled with the following piece of code:
Dim req = WebRequest.Create(Uri)
Dim resp = req.GetResponse()
Dim stream As Stream = resp.GetResponseStream()
Dim Buffer As Byte() = New Byte(1023) {}
Dim MemStream As New MemoryStream()
Dim BytesRead As Integer = 0
Dim totalBytesRead As Long = 0
Dim reader As New BinaryReader(stream)
While ((BytesRead = reader.Read(Buffer, 0, Buffer.Length)) > 0)
BytesRead = reader.Read(Buffer, 0, Buffer.Length)
MemStream.Write(Buffer, 0, BytesRead)
totalBytesRead += BytesRead
End While
The while loop was never entered, despite data was available in the reader. The variable BytesRead was never set, making me think that it would treat the "BytesRead = reader.Read(...)" as an equality validater. However with no luck, as i in debug mode attempted to change the BytesRead variable to 1024 (the length of the buffer (max read value)) but with the same negative outcome.
I solved the issue, changing the while loop to the following "do-while" loop:
Do
BytesRead = reader.Read(Buffer, 0, Buffer.Length)
MemStream.Write(Buffer, 0, BytesRead)
totalBytesRead += BytesRead
Loop While BytesRead > 0
My question goes; why is the while loop not working as i intend?:
((BytesRead = reader.Read(Buffer, 0, Buffer.Length)) > 0) => ((output) > 0)