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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack

    #1

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

    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 (ConstBufferByt eSize=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(Cons tBufferByteSize ) As Byte
    Dim enc As New System.Text.ASC IIEncoding

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

    responseStream. Read(tempBuffer , 0,
    ConstBufferByte Size) ' 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(t empBuffer) ' Put the response into
    a string (finally!)



    Any kind of help will do,

    thankyou

    Jack.

  • Spam Catcher

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

    Jack <bradnerdhss@ho tmail.comwrote in news:1185337001 .737276.37620
    @m37g2000prh.go oglegroups.com:
    put the text data into into a fixed-size buffer BEFORE I put it into a
    string (ConstBufferByt eSize=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.

    Comment

    • Jack

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

      On Jul 25, 2:32 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
      Jack <bradnerd...@ho tmail.comwrote in news:1185337001 .737276.37620
      @m37g2000prh.go oglegroups.com:
      >
      put the text data into into a fixed-size buffer BEFORE I put it into a
      string (ConstBufferByt eSize=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?

      Comment

      • Chris Dunaway

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

        On Jul 24, 11:16 pm, Jack <bradnerd...@ho tmail.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 (ConstBufferByt eSize=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(Cons tBufferByteSize ) As Byte
        Dim enc As New System.Text.ASC IIEncoding
        >
        ' responseStream. Length.ToString () ' <- Can't seem to get
        the length to signify the buffer size here ???
        >
        responseStream. Read(tempBuffer , 0,
        ConstBufferByte Size) ' 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(t empBuffer) ' 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

        Comment

        • Jack Jackson

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

          On Tue, 24 Jul 2007 21:16:41 -0700, Jack <bradnerdhss@ho tmail.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 (ConstBufferByt eSize=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(Cons tBufferByteSize ) As Byte
          Dim enc As New System.Text.ASC IIEncoding
          >
          ' responseStream. Length.ToString () ' <- Can't seem to get
          >the length to signify the buffer size here ???
          >
          responseStream. Read(tempBuffer , 0,
          >ConstBufferByt eSize) ' 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 ConstBufferByte Size to somthing like 1024

          Dim tempBuffer(Cons tBufferByteSize ) as Byte
          Dim str As StringBuilder = New StringBuilder

          Do While True
          Dim len As Integer

          len = responseStream. Read(tempBuffer , 0, ConstBufferByte Size)
          If len = 0
          Exit Do
          End If
          str.Append(enc. GetString(tempB uffer, 0, len))
          End Do

          strResponse = str.ToString()

          Comment

          Working...