How to convert a non-seekable Stream to Byte Array?

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
H

Hardy Wang

Hi all:
The Stream object from WebRequest.GetResponseStream() is non-seekable.
How can I convert this stream to a byte array?

For ordinary Stream (seekable), I can use StreamObject.Read(myByteArray,
0, myLength)

--



WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
 
Hi Hardy,

Since the response stream from a web request is a network stream
that does not provide random access (non-seekable), you will need to
copy the response stream to a seekable stream such as a MemoryStream
before you can perform seek operations:

Stream responseStream = webResponse.GetResponseStream();
MemoryStream memStream = new MemoryStream();

byte [] respBuffer = new byte[1024];
try
{
int bytesRead = responseStream.Read(respbuffer,0,
respBuffer.Length);
if(bytesRead > 0)
{
memStream.Write(respBuffer,0,bytesRead);
bytesRead = responseStream.Read(respBuffer,0,
respBuffer.Length);
}
}
finally
{
responseStream.Close();
webResponse.Close();
}

To get the array containing the byte data from the MemoryStream, use the
MemoryStream.GetBuffer() method.

Regards,
Aravind C
 
Hardy Wang said:
The Stream object from WebRequest.GetResponseStream() is non-seekable.
How can I convert this stream to a byte array?

For ordinary Stream (seekable), I can use StreamObject.Read(myByteArray,
0, myLength)

You can use that with a non-seekable request too. However, in both
cases you shouldn't ignore the return value - it may well be that you
can't read the whole of the data in one chunk. You should loop until
either you've read everything you need to or you've got to the end of
the stream (where Read will return -1).
 
I tried responseStream.Read(respbuffer,0, respBuffer.Length);

At this point an exception was thrown.

Aravind C said:
Hi Hardy,

Since the response stream from a web request is a network stream
that does not provide random access (non-seekable), you will need to
copy the response stream to a seekable stream such as a MemoryStream
before you can perform seek operations:

Stream responseStream = webResponse.GetResponseStream();
MemoryStream memStream = new MemoryStream();

byte [] respBuffer = new byte[1024];
try
{
int bytesRead = responseStream.Read(respbuffer,0,
respBuffer.Length);
if(bytesRead > 0)
{
memStream.Write(respBuffer,0,bytesRead);
bytesRead = responseStream.Read(respBuffer,0,
respBuffer.Length);
}
}
finally
{
responseStream.Close();
webResponse.Close();
}

To get the array containing the byte data from the MemoryStream, use the
MemoryStream.GetBuffer() method.

Regards,
Aravind C


Hardy Wang said:
Hi all:
The Stream object from WebRequest.GetResponseStream() is non-seekable.
How can I convert this stream to a byte array?

For ordinary Stream (seekable), I can use StreamObject.Read(myByteArray,
0, myLength)

--



WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
 
Hardy Wang said:
I tried responseStream.Read(respbuffer,0, respBuffer.Length);

At this point an exception was thrown.

And what was the exception?
 
Hardy Wang said:
Just like something "the stream is not seakable"

"Something like" doesn't help much. Could you post a short but
*complete* sample which demonstrates the problem? There should be no
problem just reading from the stream, if you're not actually *trying*
to seek.
 
Back
Top