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

J

Jack

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

Guest

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

Jack

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?
 
C

Chris Dunaway

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
 
J

Jack Jackson

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()
 

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