字节数组和MemoryStream之间的区别 [英] Difference between a byte array and MemoryStream

查看:372
本文介绍了字节数组和MemoryStream之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一个二进制文件到解析程序。我需要遍历该文件并查找某些标记,以便我可以拆分该文件,并将这些部分传递到各自的对象的构造函数。

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.

有一个优点将文件作为流保存为MemoryStream或FileStream,还是应将其转换为byte []数组?

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 byte [] MemoryStream 将整个文件带入内存。 A MemoryStream 实际上是一个底层字节数组的包装。最好的办法是有两个 FileStream (一个用于输入,一个用于输出)。从输入流读取,寻找用于指示文件在写入当前输出文件时应该分开的模式。

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 FileStream (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.

您可能需要考虑包装输入和输出分别在 BinaryReader BinaryWriter 中添加文件。

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

这篇关于字节数组和MemoryStream之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆