A memory backed stream that stores byte data in blocks, this gives improved performance over System.IO.MemoryStream in some circumstances. MemoryStream is backed by a single byte array, hence if the capacity is reached a new byte array must be instantiated and the existing data copied across. In contrast, MemoryBlockStream grows in blocks and therefore avoids copying and re-instantiating large byte arrays. Also note that by using a sufficiently small block size the blocks will avoid being placed onto the large object heap, with various benefits, e.g. avoidance/mitigation of memory fragmentation.
Inheritance: Stream
コード例 #1
0
 /// <summary>
 /// Read stream into byte array. Reads until the end of stream is reached and returns entire stream contents
 /// as a new byte array.
 /// </summary>
 /// <param name="stream">The stream to read data from.</param>
 /// <returns>Returns a new byte array containing the read data.</returns>
 public static byte[] ReadToByteArray(Stream stream)
 {
     using (MemoryBlockStream ms = new MemoryBlockStream())
     {
         stream.CopyTo(ms);
         return(ms.ToArray());
     }
 }
コード例 #2
0
        private void CompareState(MemoryStream ms, MemoryBlockStream ms2)
        {
            // Compare byte content.
            byte[] buff1 = ms.ToArray();
            byte[] buff2 = ms2.ToArray();

            if(!Utils.AreEqual(buff1, buff2)) {
                throw new Exception("State mismatch");
            }

            // Compare read/write position.
            if(ms.Position != ms2.Position) {
                throw new Exception("Position mismatch");
            }
        }
コード例 #3
0
        public void TestWriteZeroBytes()
        {
            byte[] buf = new byte[0];
            MemoryBlockStream ms = new MemoryBlockStream();
            ms.Write(buf, 0, 0);
            Assert.AreEqual(ms.Length, 0);

            XorShiftRandom rng = new XorShiftRandom(1234567);
            byte[] buf2 = new byte[100];
            rng.NextBytes(buf2);
            ms.Write(buf2, 0, buf2.Length);

            if(!Utils.AreEqual(ms.ToArray(), buf2)) Assert.Fail();

            ms.Write(buf, 0, 0);
            Assert.AreEqual(ms.Length, buf2.Length);
        }
コード例 #4
0
        public void MemoryBlockStream_FuzzerTest()
        {
            MemoryStream ms = new MemoryStream();
            MemoryBlockStream ms2 = new MemoryBlockStream();

            MemoryStreamFuzzer fuzzer = new MemoryStreamFuzzer(ms, ms2, 0);
            for(int i=0; i<1000; i++)
            {
                fuzzer.PerformMultipleOps(100);
                CompareState(ms, ms2);

                if(ms.Length > 3e9)
                {
                    ms = new MemoryStream();
                    ms2 = new MemoryBlockStream();
                }
            }
        }
コード例 #5
0
ファイル: MemoryStreamFuzzer.cs プロジェクト: colgreen/Redzen
 public MemoryStreamFuzzer(MemoryStream strmA, MemoryBlockStream strmB, int seed)
 {
     _strmA = strmA;
     _strmB = strmB;
     _rng = new XorShiftRandom(seed);
 }
コード例 #6
0
ファイル: MemoryStreamFuzzer.cs プロジェクト: colgreen/Redzen
 public MemoryStreamFuzzer(MemoryStream strmA, MemoryBlockStream strmB)
     : this(strmA, strmB, 0)
 {
 }
コード例 #7
0
ファイル: StreamHelper.cs プロジェクト: colgreen/Redzen
 /// <summary>
 /// Read stream into byte array. Reads until the end of stream is reached and returns entire stream contents
 /// as a new byte array.
 /// </summary>
 /// <param name="stream">The stream to read data from.</param>
 /// <returns>Returns a new byte array containing the read data.</returns>
 public static byte[] ReadToByteArray(Stream stream)
 {
     using(MemoryBlockStream ms = new MemoryBlockStream())
     {
         Copy(stream, ms);
         return ms.ToArray();
     }
 }