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

J

Jack

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.
 
M

Mr. Arnold

Jack said:
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/UploadFile/prvn_131971/outstreamvb11182005013421AM/outstreamvb.aspx
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Jack said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top