Exemple #1
0
        public void Write()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, true);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[20];
            cacheStream.Position = 10;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 10);

            cacheStream.Position = 20;
            cacheStream.Write(new byte[10], 0, 10);
            Assert.Equal(30, cacheStream.Position);

            cacheStream.Position = 10;
            buffer = new byte[30];
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 0, 10, 10);
            AssertSequenced(buffer, 20, 10, 30);
            Assert.Equal(0, buffer[10]);
            Assert.Equal(0, buffer[19]);
        }
Exemple #2
0
        public void CacheBlockRecycle()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 50, LargeReadSize = 100
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[50];
            cacheStream.Position = 10;
            int numRead = cacheStream.Read(buffer, 0, buffer.Length);

            Assert.Equal(50, numRead);
            AssertSequenced(buffer, 10);
            Assert.Equal(0, cacheStream.Statistics.LargeReadsIn);
            Assert.Equal(0, cacheStream.Statistics.ReadCacheHits);
            Assert.Equal(1, cacheStream.Statistics.TotalReadsIn);

            buffer = new byte[40];
            cacheStream.Position = 50;
            numRead = cacheStream.Read(buffer, 0, buffer.Length);

            Assert.Equal(40, numRead);
            AssertSequenced(buffer, 50);
            Assert.Equal(0, cacheStream.Statistics.LargeReadsIn);
            Assert.Equal(1, cacheStream.Statistics.ReadCacheHits);
            Assert.Equal(2, cacheStream.Statistics.TotalReadsIn);
        }
Exemple #3
0
        public void UnalignedCachedRead()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[20];
            cacheStream.Position = 3;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 3);
            Assert.Equal(0, cacheStream.Statistics.LargeReadsIn);
            Assert.Equal(0, cacheStream.Statistics.ReadCacheHits);
            Assert.Equal(1, cacheStream.Statistics.TotalReadsIn);

            buffer = new byte[buffer.Length];
            cacheStream.Position = 3;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 3);
            Assert.Equal(0, cacheStream.Statistics.LargeReadsIn);
            Assert.Equal(1, cacheStream.Statistics.ReadCacheHits);
            Assert.Equal(2, cacheStream.Statistics.TotalReadsIn);
        }
Exemple #4
0
        internal byte GetByte(long index)
        {
            if (index >= _bitmap.Length)
            {
                return(0);
            }

            byte[] buffer = new byte[1];
            _bitmap.Position = index;
            if (_bitmap.Read(buffer, 0, 1) != 0)
            {
                return(buffer[0]);
            }
            return(0);
        }
Exemple #5
0
        public void FailWrite()
        {
            MemoryStream       ms       = CreateSequencedMemStream(100, false);
            BlockCacheSettings settings = new BlockCacheSettings()
            {
                BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30
            };
            BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            byte[] buffer = new byte[25];
            cacheStream.Position = 0;
            cacheStream.Read(buffer, 0, buffer.Length);

            AssertSequenced(buffer, 0);

            int freeBefore = cacheStream.Statistics.FreeReadBlocks;

            cacheStream.Position = 11;
            try
            {
                cacheStream.Write(new byte[10], 0, 10);
            }
            catch (NotSupportedException)
            {
                Assert.Equal(freeBefore + 2, cacheStream.Statistics.FreeReadBlocks);
            }
        }
Exemple #6
0
        public void Bug5203_IncreaseSize()
        {
            MemoryStream       ms       = new MemoryStream();
            BlockCacheSettings settings = new BlockCacheSettings {
                BlockSize = 64, LargeReadSize = 128, OptimumReadSize = 64, ReadCacheSize = 1024
            };
            BlockCacheStream bcs = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings);

            // Pre-load read cache with a 'short' block
            bcs.Write(new byte[11], 0, 11);
            bcs.Position = 0;
            bcs.Read(new byte[11], 0, 11);

            // Extend stream
            for (int i = 0; i < 20; ++i)
            {
                bcs.Write(new byte[11], 0, 11);
            }

            // Try to read from first block beyond length of original cached short length
            // Bug was throwing exception here
            bcs.Position = 60;
            bcs.Read(new byte[20], 0, 20);
        }