title: Most efficient way of reading data from a stream
body: <p>I have an algorithm for encrypting and decrypting data using symmetric encryption. anyways when I am about to decrypt, I have:</p>
  
  <pre><code>CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Read);
  </code></pre>
  
  <p>I have to read data from the cs CryptoStream and place that data into a array of bytes. So one method could be:</p>
  
  <pre><code>  System.Collections.Generic.List&lt;byte&gt; myListOfBytes = new System.Collections.Generic.List&lt;byte&gt;();
  
     while (true)
     {
                  int nextByte = cs.ReadByte();
                  if (nextByte == -1) break;
                  myListOfBytes.Add((Byte)nextByte);
     }
     return myListOfBytes.ToArray();
  </code></pre>
  
  <p>another technique could be:</p>
  
  <pre><code>ArrayList chuncks = new ArrayList();
  
  byte[] tempContainer = new byte[1048576];
  
  int tempBytes = 0;
  while (tempBytes &lt; 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 a for each loop on chunks and add those bytes
  </code></pre>
  
  <p>I cannot know in advance the length of the stream cs:</p>
  
  <p><img src="https://i.stack.imgur.com/5Izza.png" alt="enter image description here"></p>
  
  <p>or perhaps I should implement my stack class. I will be encrypting a lot of information therefore making this code efficient will save a lot of time</p>