473,769 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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

--

WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Nov 15 '05 #1
6 52268
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.Get ResponseStream( );
MemoryStream memStream = new MemoryStream();

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

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

Regards,
Aravind C
"Hardy Wang" <ha********@mar ketrend.com> wrote in message
news:uX******** ******@TK2MSFTN GP09.phx.gbl...
Hi all:
The Stream object from WebRequest.GetR esponseStream() is non-seekable.
How can I convert this stream to a byte array?

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

--

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

Nov 15 '05 #2
Hardy Wang <ha********@mar ketrend.com> wrote:
The Stream object from WebRequest.GetR esponseStream() is non-seekable.
How can I convert this stream to a byte array?

For ordinary Stream (seekable), I can use StreamObject.Re ad(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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
I tried responseStream. Read(respbuffer ,0, respBuffer.Leng th);

At this point an exception was thrown.

"Aravind C" <ar***********@ nospam.hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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.Get ResponseStream( );
MemoryStream memStream = new MemoryStream();

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

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

Regards,
Aravind C
"Hardy Wang" <ha********@mar ketrend.com> wrote in message
news:uX******** ******@TK2MSFTN GP09.phx.gbl...
Hi all:
The Stream object from WebRequest.GetR esponseStream() is non-seekable. How can I convert this stream to a byte array?

For ordinary Stream (seekable), I can use

StreamObject.Re ad(myByteArray,
0, myLength)

--

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


Nov 15 '05 #4
Hardy Wang <ha********@mar ketrend.com> wrote:
I tried responseStream. Read(respbuffer ,0, respBuffer.Leng th);

At this point an exception was thrown.


And what was the exception?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Just like something "the stream is not seakable"
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Hardy Wang <ha********@mar ketrend.com> wrote:
I tried responseStream. Read(respbuffer ,0, respBuffer.Leng th);

At this point an exception was thrown.


And what was the exception?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Hardy Wang <ha********@mar ketrend.com> wrote:
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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
9889
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double (*func)(double,double)) { nfunc = func ; return qgaus(f1,x1,x2);//error there
4
9864
by: James Geurts | last post by:
Hi all, I'm trying to convert a string representing a path to Dos 8.3 notation. The code that I have works fine if the path exists, but it will not work if the path does not exist. I am looking for a way to convert a string representing a long file path (whether or not it exists) to short (dos 8.3) form. This is what I have: private string GetShortPath(string longPath) { if (longPath.Length <= 8) { return longPath; } StringBuilder...
1
6933
by: dotNeter | last post by:
I have a file with non-ISO extended-ASCII format, but my program only reads ISO ascii characters. How to do? thx
10
5912
by: dorkrawk | last post by:
I am having an issue with some VC++ I am writing. I have a struct and I'm trying to call a function from it from a function in another object. here is the struct.... struct JNI_Interface { JavaVM *jvm; /* denotes a Java VM */ JNIEnv *env; /* pointer to native method interface */
0
1583
by: xhy_China | last post by:
Hi,I want to ask three questions(in vs.net and C#): 1. how can I know whether a bitmap is an indexed or non-indexed? 2. how can I convert a indexed bitmap to non-indexed bitmap? 3. how can I convert a non-indexed bitmap to indexed bitmap?
4
10639
by: Tux SC | last post by:
Hello, I have a JavaScript function which receives as input a string which in some cases contain non-UTF8 characters. The output of this function is used by another utility which fails if it receives any non-UTF8 characters. Is there a way for me to filter out or convert the non- UTF8 characters in the JavaScript function? Thanks much!
0
1248
by: mohankumar11 | last post by:
hi..i am having ms sql server 2005...now we are in upgrading process... in that database is unicode but the tables are not unicode... the tables are in non-unicode..tell me how to convert the tables from non-unicode to unicode tables... we are failing in this process.. thanks in advance.
5
2331
by: RyanL | last post by:
I'm a newbie! I have a non-delimited data file that I'd like to convert to delimited. Example... Line in non-delimited file: 0139725635999992000010100534+42050-102800FM-15+1198KAIA Should be: 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
10
2169
by: annalisa | last post by:
I really need some help with langauge conversion to HTML. My translators are translating into Word and I need to convert Word to HTML. It's been awhile since I've worked with Unicode and know that not all the fonts being used are unicode. Is there a way to strip all the junk out of the MS Word filtered (yeah right) HTML. All I want are the basic formatting tags no spans,fonts, divs, css, but I don't want to lose any language identification...
4
2216
by: HxRLxY | last post by:
I am having a compile-time problem with a simple program I am writing. When I attempt to compile, I get the error "non-static variable this cannot be referenced from a static context". The error point to where I try to create a new instance of 'ConvertToOunces' inside the main method. Any help would be greatly appreciated. Here is my source code. /******************************************************************************\ | |Try...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.