locked
Load large files (>100Mb) from fileStream to MemoryStream in VB .NET RRS feed

  • Question

  • Hello,

    When I try to load large files from fileStream  to MemoryStream I get a "System.IO.MemoryStream" Exception.

    Could you please help me to solve this issue ?

    How could I identify the maximum buffer size that the system can handle before raising a MemoryStream IO exception ?

    Kind regards,

    Kalos

     

    '--------------------------------------------------------------------------------------------
    Sub  UploadToRam(byval  fileStream as  System.IO.FileStream)
                'Create Buffer variables
                  Dim  memStream As  Stream = New  MemoryStream()
                Dim  buffer As  Byte () = New  Byte (1024*1024*128) {} 'Question: How to find the maximum size of the "buffer" that the system can handle ?
                Dim  bytesRead As  Integer  = 0
                
                
                    While  (InlineAssignHelper(bytesRead, fileStream.Read(buffer, 0, buffer.Length))) <> 0
                        'Generate an exception when the size of the file is very large (>100Mb)
                        'Question: how to handle large files ?
                        memStream.Write(buffer, 0, bytesRead)
                        '------------------------------------------------------------------------
                    End  While
    End  Sub

      Private  Shared  Function  InlineAssignHelper(Of  T)(ByRef  target As  T, ByVal  value As  T) As  T
            target = value
            Return  value
      End  Function
    '--------------------------------------------------------------------------------------------
    Monday, August 16, 2010 2:38 PM

Answers

  • Maybe I should try fileStream > ServerStream ?

    Yes, I think that would be better. Also when you code for filestream to serverStream, send few bytes at a time as I had shown you in one of your previous threads.
    Gaurav Khanna
    • Marked as answer by Kalos Kagatos Sunday, August 22, 2010 7:01 AM
    Tuesday, August 17, 2010 5:25 PM

All replies

  • I don't think it's a good idea to store data of about 100MB in memory? Why do you want to store that in memory? I think you should thing of alternative option
    Gaurav Khanna
    Monday, August 16, 2010 6:26 PM
  • I am trying to upload a fileStream to a server. So currently I am doing the following:

    fileStream  > MemoryStream > ServerStream


    Maybe I should try fileStream > ServerStream ?

    Monday, August 16, 2010 7:02 PM
  • Maybe I should try fileStream > ServerStream ?

    Yes, I think that would be better. Also when you code for filestream to serverStream, send few bytes at a time as I had shown you in one of your previous threads.
    Gaurav Khanna
    • Marked as answer by Kalos Kagatos Sunday, August 22, 2010 7:01 AM
    Tuesday, August 17, 2010 5:25 PM