Discussion:
How to convert a non-seekable Stream to Byte Array?
(too old to reply)
Hardy Wang
2003-11-09 03:50:43 UTC
Permalink
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
Aravind C
2003-11-09 07:14:54 UTC
Permalink
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
Post by Hardy Wang
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,
Post by Hardy Wang
0, myLength)
--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Hardy Wang
2003-11-09 17:17:34 UTC
Permalink
I tried responseStream.Read(respbuffer,0, respBuffer.Length);

At this point an exception was thrown.
Post by Aravind C
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
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
Post by Hardy Wang
The Stream object from WebRequest.GetResponseStream() is
non-seekable.
Post by Aravind C
Post by Hardy Wang
How can I convert this stream to a byte array?
For ordinary Stream (seekable), I can use
StreamObject.Read(myByteArray,
Post by Hardy Wang
0, myLength)
--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Jon Skeet [C# MVP]
2003-11-09 17:44:39 UTC
Permalink
Post by Hardy Wang
I tried responseStream.Read(respbuffer,0, respBuffer.Length);
At this point an exception was thrown.
And what was the exception?
--
Jon Skeet - <***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hardy Wang
2003-11-10 01:10:11 UTC
Permalink
Just like something "the stream is not seakable"
Post by Jon Skeet [C# MVP]
Post by Hardy Wang
I tried responseStream.Read(respbuffer,0, respBuffer.Length);
At this point an exception was thrown.
And what was the exception?
--
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
2003-11-10 08:05:04 UTC
Permalink
Post by Hardy Wang
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.
--
Jon Skeet - <***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
2003-11-09 08:35:31 UTC
Permalink
Post by Hardy Wang
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).
--
Jon Skeet - <***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Loading...