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.
Spam Catcher - 25 Jul 2007 07:32 GMT
Jack <bradnerdhss@hotmail.com> wrote 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.
Jack - 25 Jul 2007 08:43 GMT
> Jack <bradnerd...@hotmail.com> wrote in news:1185337001.737276.37620
> @m37g2000prh.googlegroups.com:
[quoted text clipped - 10 lines]
> 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?
Chris Dunaway - 25 Jul 2007 14:18 GMT
> Hi,
>
[quoted text clipped - 32 lines]
>
> 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
Jack Jackson - 25 Jul 2007 16:59 GMT
>Hi,
>
[quoted text clipped - 32 lines]
>
>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()