简体   繁体   English

无法扩展MemoryStream…从FileStream到MemoryStream或类似的重构

[英]MemoryStream cannot be expanded…refactoring from FileStream to MemoryStream or similar

I have a problem refactoring a chunk of code. 我在重构代码块时遇到问题。 I vaguely understand the problem but a little more insight and or explanation would be very helpful to my understanding and resolving the problem. 我模糊地理解了问题,但是多一点洞察力和/或解释将对我的理解和解决问题非常有帮助。

First the original chunk of code: 首先是原始代码块:

FileStream fS = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
Int64 fSLength = fS.Length;
Int64 remainingNotFulEncDecBlock = fSLength % 1;
fS.Seek(0, SeekOrigin.End);
Int64 nrBytesToAdd = 1 - remainingNotFulEncDecBlock;

Byte fillByte = (Byte)'$';

  for (Int32 i = 0; i < nrBytesToAdd - 1; i++)
    fS.WriteByte(fillByte);
    fS.WriteByte((Byte)nrBytesToAdd);
    fSLength = fS.Length;
    fS.Close();

The only purpose to the refactor is that we no longer have a physical file to open. 重构的唯一目的是我们不再需要打开物理文件。 Instead we provide a byte[] . 相反,我们提供了byte[]

Here is my refactored code: 这是我的重构代码:

MemoryStream fS = new MemoryStream(_data);
Int64 fSLength = fS.Length;
Int64 remainingNotFulEncDecBlock = fSLength % 1;
fS.Seek(0, SeekOrigin.End);
Int64 nrBytesToAdd = 1 - remainingNotFulEncDecBlock;

Byte fillByte = (Byte)'$';
  for (Int32 i = 0; i < nrBytesToAdd - 1; i++)
    fS.WriteByte(fillByte);
    fS.WriteByte((Byte)nrBytesToAdd);
    fSLength = fS.Length;
    fS.Close();

The code above produces the error MemoryStream cannot be expanded on the line 上面的代码产生错误MemoryStream不能在行上展开

fS.WriteByte((Byte)nrBytesToAdd);

Originally I had to Google the error and read up. 最初,我不得不向Google搜索错误并进行阅读。 This now makes sense as the memory stream has been set to a specific size. 现在这很有意义,因为内存流已设置为特定大小。 What I am unclear on is how should I proceed. 我尚不清楚应该如何进行。 Do I create another MemoryStream object that is NOT set to a specific size thus allowing me to do something like: 我是否创建另一个未设置为特定大小的MemoryStream对象,从而允许我执行以下操作:

MemoryStream ms = new MemoryStream();
ms.WriteByte((Byte)nrBytesToAdd);

My reading of this technique indicates this will be less performant than initializing a MemoryStream to a specified byte[] however I don't know of (can't find) another way to expand MemoryStream dynamically. 我对这项技术的阅读表明,这比将MemoryStream初始化为指定的byte[]性能要差,但是我不知道(找不到)动态扩展MemoryStream另一种方法。 One plus is that now and in the foreseen future the byte[] coming in will be very small so while slower probably won't make a drastic impact on performance. 加号的是,现在和可预见的将来, byte[]会很小,因此虽然速度较慢可能不会对性能产生重大影响。

You can use the MemeoryStream constructor that takes an initial capacity. 您可以使用具有初始容量的MemeoryStream 构造函数

MemoryStream fS = new MemoryStream(_data.Length);

This will set up the internal byte array of that size so the memory stream doesn't have to keep expanding the array each time it hits it's size limit. 这将设置该大小的内部字节数组,因此内存流不必在每次达到其大小限制时都继续扩展该数组。 It will allow you to add bytes to the memory stream. 它将允许您将字节添加到内存流。 While not as effective as just having a static byte[] size it's probably the next best thing. 尽管不如拥有一个静态byte []大小那么有效,但它可能是下一个最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM