473,388 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to read a stream into a buffer of non-fixed size?

Hi,

I want to read a string a chars from a stream, and put it into a
string.

At the moment, I'm creating a buffer of a fixed size, and reading the
stream of text into it. It works, but I have to create a buffer of a
pre-defined length: (ConstBufferByteSize=10000000). How can I read a
stream into a buffer or string without knowing the number of chars the
stream will contain?

Dim response As HttpWebResponse
Dim responseStream As Stream

....

' Do the Request

response = CType(request.GetResponse(), HttpWebResponse) ' Get the
response from the server, and put it into a HttpWebResponse
responseStream = response.GetResponseStream() ' Get the
XML Response stream

' Now put the text received into a buffer

Dim tempBuffer(ConstBufferByteSize) As Byte
Dim enc As New System.Text.ASCIIEncoding

responseStream.Read(tempBuffer, 0, ConstBufferByteSize) ' Read
from the stream x bytes and put into a temporary buffer of fixed size
responseStream.Close() ' Close
the request stream to free up resources

strResponse = enc.GetString(tempBuffer) ' Put the response
into a string (finally!)
MessageBox.Show(strResponse)

Thanks in advance,
Jack.

Jul 18 '07 #1
2 6593

"Jack" <br*********@hotmail.comwrote in message
news:11**********************@g12g2000prg.googlegr oups.com...
Hi,

I want to read a string a chars from a stream, and put it into a
string.

At the moment, I'm creating a buffer of a fixed size, and reading the
stream of text into it. It works, but I have to create a buffer of a
pre-defined length: (ConstBufferByteSize=10000000). How can I read a
stream into a buffer or string without knowing the number of chars the
stream will contain?

This link may help you.

http://www.vbdotnetheaven.com/Upload...tstreamvb.aspx

Jul 18 '07 #2
Jack wrote:
Hi,

I want to read a string a chars from a stream, and put it into a
string.

At the moment, I'm creating a buffer of a fixed size, and reading the
stream of text into it. It works, but I have to create a buffer of a
pre-defined length: (ConstBufferByteSize=10000000). How can I read a
stream into a buffer or string without knowing the number of chars the
stream will contain?
Use a StreamReader:

Dim reader As New StreamReader(response.GetResponseStream(), Encoding.ASCII)
Dim response As String = reader.ReadToEnd()
reader.Close()

The response is most likely not encoded as ASCII, though. You should use
the actual encoding of the response.
Dim response As HttpWebResponse
Dim responseStream As Stream

...

' Do the Request

response = CType(request.GetResponse(), HttpWebResponse) ' Get the
response from the server, and put it into a HttpWebResponse
responseStream = response.GetResponseStream() ' Get the
XML Response stream

' Now put the text received into a buffer

Dim tempBuffer(ConstBufferByteSize) As Byte
Dim enc As New System.Text.ASCIIEncoding

responseStream.Read(tempBuffer, 0, ConstBufferByteSize) ' Read
Oops! The Stream.Read method returns the number of bytes read, which may
be less than the number of bytes requested. If you ignore the return
value, you don't know how much of the data has actually been read. When
using the Stream.Read method you have to loop until you have actually
read all the data.

Just in case you plan to ever use the Stream.Read method...
from the stream x bytes and put into a temporary buffer of fixed size
responseStream.Close() ' Close
the request stream to free up resources

strResponse = enc.GetString(tempBuffer) ' Put the response
into a string (finally!)
MessageBox.Show(strResponse)

Thanks in advance,
Jack.
--
Göran Andersson
_____
http://www.guffa.com
Jul 18 '07 #3

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

Similar topics

15
by: Frank Bormann | last post by:
Hi, probably a stupid question, but I haven't been able to find anything. Is there a istream related function that let me read exactly one keystroke from the keyboard through cin? What I...
6
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
5
by: Xerox | last post by:
Are any of the xml streaming classes able to start reading from an xml file part way through? Say you have the following xml: <root> <elem val = "1"/> <elem val = "2"/> **<>** <elem val =...
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
2
by: GB | last post by:
Hi Everybody! I have 2 different processes/application. One is writing to a file and another is reading from it. For some reason the code doesnt seems to work and gives mscorlib.dll IOException...
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
3
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I'm creating an application that will read emails from GMail, using the System.Net.Sockets.TcpClient and POP protocol. However, I am having a problem with my SslStream. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.