相关疑难解决方法(0)

从流中读取数据的最有效方法

我有一个使用对称加密来加密和解密数据的算法.无论如何,当我要解密时,我有:

CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Read);
Run Code Online (Sandbox Code Playgroud)

我必须从cs CryptoStream中读取数据并将该数据放入一个字节数组中.所以一种方法可能是:

  System.Collections.Generic.List<byte> myListOfBytes = new System.Collections.Generic.List<byte>();

   while (true)
   {
                int nextByte = cs.ReadByte();
                if (nextByte == -1) break;
                myListOfBytes.Add((Byte)nextByte);
   }
   return myListOfBytes.ToArray();
Run Code Online (Sandbox Code Playgroud)

另一种技术可能是:

ArrayList chuncks = new ArrayList();

byte[] tempContainer = new byte[1048576];

int tempBytes = 0;
while (tempBytes < 1048576)
{
    tempBytes = cs.Read(tempContainer, 0, tempContainer.Length);
    //tempBytes is the number of bytes read from cs stream. those bytes are placed
    // on the tempContainer array

    chuncks.Add(tempContainer);

}

// later do …
Run Code Online (Sandbox Code Playgroud)

c# performance stream

25
推荐指数
2
解决办法
6万
查看次数

标签 统计

c# ×1

performance ×1

stream ×1