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

Possible to put 'stream' character data into a string without creating a temporary fixed-sized buffer?

P: n/a
Hi,

I do a webrequest and it returns some text data in a stream. I want to
put this text data into a string. I've got it working just fine, but I
have to

put the text data into into a fixed-size buffer BEFORE I put it into a
string (ConstBufferByteSize=1000000). This fixed size buffer wastes
space. Is

it possible to somehow assign it straight to a string, or somehow do
this 'dynamically' ?
'' Put the XML response into a string to display
Dim tempBuffer(ConstBufferByteSize) As Byte
Dim enc As New System.Text.ASCIIEncoding

' responseStream.Length.ToString() ' <- Can't seem to get
the length to signify the buffer size here ???

responseStream.Read(tempBuffer, 0,
ConstBufferByteSize) ' Read from the stream x bytes and put into a
temporary buffer

responseStream.Close() ' Close
the request stream to free up resources

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

Any kind of help will do,

thankyou

Jack.

Jul 25 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jack <br*********@hotmail.comwrote in news:1185337001.737276.37620
@m37g2000prh.googlegroups.com:
put the text data into into a fixed-size buffer BEFORE I put it into a
string (ConstBufferByteSize=1000000). This fixed size buffer wastes
space. Is

it possible to somehow assign it straight to a string, or somehow do
this 'dynamically' ?
The buffer is used by the web request to buffer the incoming request.
Strings are immutable, so a new copy of the string is created each time it
is changed. Thus .NET probably buffers to memory first, then builds a
string afterwards for efficiency sake.
Jul 25 '07 #2

P: n/a
On Jul 25, 2:32 pm, Spam Catcher <spamhoney...@rogers.comwrote:
Jack <bradnerd...@hotmail.comwrote in news:1185337001.737276.37620
@m37g2000prh.googlegroups.com:
put the text data into into a fixed-size buffer BEFORE I put it into a
string (ConstBufferByteSize=1000000). This fixed size buffer wastes
space. Is
it possible to somehow assign it straight to a string, or somehow do
this 'dynamically' ?

The buffer is used by the web request to buffer the incoming request.
Strings are immutable, so a new copy of the string is created each time it
is changed. Thus .NET probably buffers to memory first, then builds a
string afterwards for efficiency sake.
OK. So I need to put the stream into a fixed sized buffer first? Isn't
there another way?

Jul 25 '07 #3

P: n/a
On Jul 24, 11:16 pm, Jack <bradnerd...@hotmail.comwrote:
Hi,

I do a webrequest and it returns some text data in a stream. I want to
put this text data into a string. I've got it working just fine, but I
have to

put the text data into into a fixed-size buffer BEFORE I put it into a
string (ConstBufferByteSize=1000000). This fixed size buffer wastes
space. Is

it possible to somehow assign it straight to a string, or somehow do
this 'dynamically' ?

'' Put the XML response into a string to display
Dim tempBuffer(ConstBufferByteSize) As Byte
Dim enc As New System.Text.ASCIIEncoding

' responseStream.Length.ToString() ' <- Can't seem to get
the length to signify the buffer size here ???

responseStream.Read(tempBuffer, 0,
ConstBufferByteSize) ' Read from the stream x bytes and put into a
temporary buffer

responseStream.Close() ' Close
the request stream to free up resources

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

Any kind of help will do,

thankyou

Jack.
Just set your buffer size to something smaller, like 8K. The Read
method should return how many bytes were actually read. You would
have to read in a loop until Read returns 0 indicating the end of the
stream.

Chris

Jul 25 '07 #4

P: n/a
On Tue, 24 Jul 2007 21:16:41 -0700, Jack <br*********@hotmail.com>
wrote:
>Hi,

I do a webrequest and it returns some text data in a stream. I want to
put this text data into a string. I've got it working just fine, but I
have to

put the text data into into a fixed-size buffer BEFORE I put it into a
string (ConstBufferByteSize=1000000). This fixed size buffer wastes
space. Is

it possible to somehow assign it straight to a string, or somehow do
this 'dynamically' ?
'' Put the XML response into a string to display
Dim tempBuffer(ConstBufferByteSize) As Byte
Dim enc As New System.Text.ASCIIEncoding

' responseStream.Length.ToString() ' <- Can't seem to get
the length to signify the buffer size here ???

responseStream.Read(tempBuffer, 0,
ConstBufferByteSize) ' Read from the stream x bytes and put into a
temporary buffer

responseStream.Close() ' Close
the request stream to free up resources

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

Any kind of help will do,

thankyou

Jack.
I haven't tried this, but what about:

' Define ConstBufferByteSize to somthing like 1024

Dim tempBuffer(ConstBufferByteSize) as Byte
Dim str As StringBuilder = New StringBuilder

Do While True
Dim len As Integer

len = responseStream.Read(tempBuffer, 0, ConstBufferByteSize)
If len = 0
Exit Do
End If
str.Append(enc.GetString(tempBuffer, 0, len))
End Do

strResponse = str.ToString()
Jul 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.