views:

2152

answers:

4

Howdy,

I am reading a binary file into a parsing program. I will need to iterate through the file and look for certain markers so I can split the file up and pass those parts into their respective object’s constructors.

Is there an advantage to holding the file as a stream, either MemoryStream or FileStream, or should it be converted into a byte[] array?

Keith

A: 

A stream is (usually) more efficient because all of the data is not being store in memory, if it is not a memory stream (AFAIK), whereas a byte[] array is stored entirely in memory.

Dan Herbert
A: 

A MemoryStream is basically a byte array with a stream interface, e.g. sequential reading/writing and the concept of a current position.

Timbo
+7  A: 

A byte[] or MemoryStream will both require bringing the entire file into memory. A MemoryStream is really a wrapper around an underlying byte array. The best approach is to have two FileStreams (one for input and one for output). Read from the input stream looking for the pattern used to indicate the file should be separated while writing to the current output file.

You may want to consider wrapping the input and output files in a BinaryReader and BinaryWriter respectively if they add value to your scenario.

dpp
A: 

I think that most of the files I will need to parse will be less than 5MB, and most likely be about 1MB each and the destination after I have parsed it is a several tables in a SQL database.

I will probably copy the file contents into memory. Does a Stream give me any extra’s for looking through the data vs doing a For loop over the byte array, like pattern matching?

Keith

Keith Sirmons