Reuse MemoryStream?

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

    #1

    Reuse MemoryStream?

    I've created a new MemoryStream object that takes a byte array but
    I thought it would be a good idea to reuse it instead
    of creating a bunch of new ones.

    How do I sorta clear it out and reuse it with a different byte array?

    Should I call flush() and then write()?

    Drew





  • woodBeeProgrammer

    #2
    Re: Reuse MemoryStream?

    Did you try MemoryStream.Se ek to just set back to origin?

    "Drew" <someone@hotmai l.com> wrote in message
    news:#iw10z#wDH A.1804@TK2MSFTN GP09.phx.gbl...[color=blue]
    > I've created a new MemoryStream object that takes a byte array but
    > I thought it would be a good idea to reuse it instead
    > of creating a bunch of new ones.
    >
    > How do I sorta clear it out and reuse it with a different byte array?
    >
    > Should I call flush() and then write()?
    >
    > Drew
    >
    >
    >
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Reuse MemoryStream?

      Drew <someone@hotmai l.com> wrote:[color=blue]
      > I've created a new MemoryStream object that takes a byte array but
      > I thought it would be a good idea to reuse it instead
      > of creating a bunch of new ones.
      >
      > How do I sorta clear it out and reuse it with a different byte array?
      >
      > Should I call flush() and then write()?[/color]

      No - that wouldn't reset it. Personally I'd just create a new one each
      time.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Carlos Guzmán Álvarez

        #4
        Re: Reuse MemoryStream?

        Hello:
        [color=blue]
        > I've created a new MemoryStream object that takes a byte array but
        > I thought it would be a good idea to reuse it instead
        > of creating a bunch of new ones.
        >
        > How do I sorta clear it out and reuse it with a different byte array?
        >
        > Should I call flush() and then write()?[/color]

        Try this:

        MemoryStream ms = new MemoryStream();

        ...

        ms.SetLength(0) ;
        ms.Position = 0;



        Hope this helps.



        --
        Best regards

        Carlos Guzmán Álvarez
        Vigo-Spain

        Comment

        Working...