By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.