Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jack - 25 Jul 2007 05:16 GMT
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()

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.