Filestream to Memorystream

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

    Filestream to Memorystream

    I am looking at how to move data from a file to a memory stream. This
    data will be needed many times over and over so i don't want to have
    to rely on the system to keep the information cashed for access. Kind
    of an easy question for pros but thats why I figured I would ask here.

    Thanks,
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Filestream to Memorystream

    On Sep 17, 4:40 pm, paradigmshift <chrismsm...@gm ail.comwrote:
    I am looking at how to move data from a file to a memory stream. This
    data will be needed many times over and over so i don't want to have
    to rely on the system to keep the information cashed for access. Kind
    of an easy question for pros but thats why I figured I would ask here.
    >
    Thanks,
    Just cpy the FileStream to the MemoryStream.
    Unfortunately FileStream does not implement a WriteTo a la
    MemoryStream , so you need a temp buffer

    Comment

    • Peter Duniho

      #3
      Re: Filestream to Memorystream

      On Wed, 17 Sep 2008 13:40:55 -0700, paradigmshift <chrismsmith@gm ail.com>
      wrote:
      I am looking at how to move data from a file to a memory stream. This
      data will be needed many times over and over so i don't want to have
      to rely on the system to keep the information cashed for access. Kind
      of an easy question for pros but thats why I figured I would ask here.
      Just read it from the FileStream and write it to the MemoryStream. As an
      optimization, start by setting the Capacity property of the MemoryStream
      to the length of the original FileStream. That will allow you to avoid
      intermediate reallocations as the MemoryStream is written to.

      That said, it's not really clear what advantage you intend to have by
      copying all of the data to a MemoryStream. The system will still wind up
      caching the data, but through a different mechanism (virtual memory rather
      than via the file system). Copying all of the data from a file to a
      memory block simply increases the memory pressure for the overall system,
      which may in fact result in counter-productive behavior (e.g. data being
      flushed from memory and needing to be reloaded later).

      I encourage you to do some real-world performance comparisons if you
      really want to try this. I suspect that you will find that the
      performance improvement is minimal, assuming you don't actually make
      things worse.

      Pete

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Filestream to Memorystream

        paradigmshift <chrismsmith@gm ail.comwrote:
        I am looking at how to move data from a file to a memory stream. This
        data will be needed many times over and over so i don't want to have
        to rely on the system to keep the information cashed for access. Kind
        of an easy question for pros but thats why I figured I would ask here.
        Create a buffer (byte array) and the memory stream. Repeatedly read
        from the file stream into the buffer, and then write the contents (but
        only as much as you read) into the memory stream. Keep going until
        reading from the file stream yields no data.

        See http://pobox.com/~skeet/csharp/readbinary.html for the reading loop
        - the writing to the memory stream is the easy bit!

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        Working...