public void AfterDispose_UnusableMethodsThrow()
        {
            var subject = new SmallBlockMemoryStream();

            subject.Dispose();

            long dummy;
            var  buffer      = new byte[1];
            var  dummyTarget = new MemoryStream();
            var  actions     = new Action[]
            {
                () => dummy = subject.Length,
                () => subject.SetLength(0),
                () => dummy            = subject.Capacity,
                () => subject.Capacity = 100,
                () => dummy            = subject.Position,
                () => subject.Position = 0,
                () => subject.Seek(0, SeekOrigin.Begin),
                () => subject.Write(buffer, 0, 1),
                () => subject.WriteByte(1),
                () => CallWriteTo(subject, dummyTarget),
                () => subject.Read(buffer, 0, 1),
                () => subject.ReadByte()
            };

            foreach (var action in actions)
            {
                action.ShouldThrow <ObjectDisposedException>();
            }
        }
        public void Position_withBadParameters_fails()
        {
            var    subject = new SmallBlockMemoryStream();
            Action action  = () => subject.Position = -10;

            action.ShouldThrow <ArgumentOutOfRangeException>();
        }
        private static void AssertEquivalent(MemoryStream standard, SmallBlockMemoryStream subject)
        {
            // length and position should be identical to standard
            subject.Length.Should().Be(standard.Length);
            subject.Position.Should().Be(standard.Position);

            // allocations should never exceed LOH limit
            var allocationSizes = subject.GetAllocationSizes();

            allocationSizes.Any(x => x > SmallBlockMemoryStream.MaxBlockSize)
            .Should().BeFalse();

            // capacity should match allocations
            var calculatedCapacity = allocationSizes.Sum(x => (long)Math.Max(0, x));

            subject.Capacity.Should().Be(calculatedCapacity);

            // total allocation should be identical to the standard until the LOH limit
            //  is exceeded...
            if (standard.Capacity < SmallBlockMemoryStream.MaxBlockSize)
            {
                calculatedCapacity.Should().Be(standard.Capacity);
            }

            // contents of the stream should be identical to the standard
            Assert.AreEqual(standard, subject);
        }
        public void Seek_withBadParameters_fails()
        {
            var    subject = new SmallBlockMemoryStream();
            Action action  = () => subject.Seek(0, (SeekOrigin)123);

            action.ShouldThrow <ArgumentException>();
        }
 private static void VerifyThrows <T>(Action <Stream> action) where T : Exception
 {
     using (var standard = new MemoryStream())
         using (var subject = new SmallBlockMemoryStream())
         {
             ((Action)(() => action(standard))).ShouldThrow <T>();
             ((Action)(() => action(subject))).ShouldThrow <T>();
         }
 }
        private static void VerifyResultsAreEqual <T>(Func <Stream, T> func)
        {
            using (var standard = new MemoryStream())
                using (var subject = new SmallBlockMemoryStream())
                {
                    Assert.AreEqual(func(standard), func(subject));

                    AssertEquivalent(standard, subject);
                }
        }
Example #7
0
 public void SmallBlockMemoryStream()
 {
     using (var stream = new SmallBlockMemoryStream())
     {
         for (int position = 0; position < this.Length; position += blockSize)
         {
             stream.Write(this.data, position, Math.Min(blockSize, this.Length - position));
         }
     }
 }
        private static void VerifyAction(Action <Stream> action)
        {
            using (var standard = new MemoryStream())
                using (var subject = new SmallBlockMemoryStream())
                {
                    action(standard);
                    action(subject);

                    AssertEquivalent(standard, subject);
                }
        }
        public void Flush_AfterDispose_DoesNotThrow()
        {
            // Just to mimic the MemoryStream implementation
            var standard = new MemoryStream();

            standard.Dispose();
            standard.Flush();
            var subject = new SmallBlockMemoryStream();

            subject.Dispose();
            subject.Flush();
        }
        public void Dispose_LeavesSafePropertiesInTheCorrectState()
        {
            var subject = new SmallBlockMemoryStream();

            subject.Dispose();

            subject.ShouldBeEquivalentTo(new
            {
                CanRead  = false,
                CanSeek  = false,
                CanWrite = false,
            }, EqOpts);
            subject.GetAllocationSizes().ShouldBeEquivalentTo(NoAllocations);
        }
        public void ReadByte_PastEndOfStream_succeeds()
        {
            using (var standard = new MemoryStream())
                using (var subject = new SmallBlockMemoryStream())
                {
                    standard.Write(TestData, 0, TestData.Length);
                    subject.Write(TestData, 0, TestData.Length);

                    for (var i = 0; i <= TestData.Length; i++)
                    {
                        subject.ReadByte().Should().Be(standard.ReadByte());
                    }
                }
        }
        public void ReadPastEnd_succeedsWithCorrectReadLength()
        {
            const int dataLength = 100;
            const int readLength = 110;
            var       writeData  = MakeTestData(dataLength);
            var       subject    = new SmallBlockMemoryStream();

            subject.Write(writeData, 0, dataLength);
            subject.Position = 0;
            var readData = new byte[readLength];
            var read     = subject.Read(readData, 0, readLength);

            Assert.AreEqual(writeData.Length, read);
        }
        public void Write_withBadParameters_fails()
        {
            var subject = new SmallBlockMemoryStream();
            var data    = MakeTestData(10);

            Action action = () => subject.Write(null, 0, 0);

            action.ShouldThrow <ArgumentException>();

            action = () => subject.Write(data, -10, 0);
            action.ShouldThrow <ArgumentException>();

            action = () => subject.Write(data, 0, -10);
            action.ShouldThrow <ArgumentException>();

            action = () => subject.Write(data, 0, 20);
            action.ShouldThrow <ArgumentException>();
        }
        public void Read_withBadParameters_fails()
        {
            var subject = new SmallBlockMemoryStream();
            var data    = new byte[10];

            Action action = () => subject.Read(null, 0, 0);

            action.ShouldThrow <ArgumentException>();

            action = () => subject.Read(data, -10, 0);
            action.ShouldThrow <ArgumentException>();

            action = () => subject.Read(data, 0, -10);
            action.ShouldThrow <ArgumentException>();

            action = () => subject.Read(data, 0, 20);
            action.ShouldThrow <ArgumentException>();
        }
Example #15
0
        internal static TaskSequence ReadToString(this Stream stream, System.Text.Encoding encoding, Action <string> result)
        {
            using (var ms = new SmallBlockMemoryStream())
            {
                int read = -1;
                while (read != 0)
                {
                    byte[]     buffer = new byte[1024];
                    Task <int> count  = stream.ReadAsync(buffer, 0, 1024);
                    yield return(count);

                    ms.Write(buffer, 0, count.Result);
                    read = count.Result;
                }

                ms.Seek(0, SeekOrigin.Begin);
                using (var strm = new StreamReader(ms, encoding))
                {
                    result(strm.ReadToEnd());
                }
            }
        }
Example #16
0
        internal static TaskSequence ReadToString(this Stream stream, System.Text.Encoding encoding, Action<string> result)
        {
            using (var ms = new SmallBlockMemoryStream())
            {
                int read = -1;
                while (read != 0)
                {
                    byte[] buffer = new byte[1024];
					StorageTask<int> count = stream.ReadAsyncEx(buffer, 0, 1024);
                    yield return count;

                    ms.Write(buffer, 0, count.Result);
                    read = count.Result;
                }

                ms.Seek(0, SeekOrigin.Begin);
                using (var strm = new StreamReader(ms, encoding))
                {
                    result(strm.ReadToEnd());
                }
            }
        }
Example #17
0
        public static Stream Compress(Stream inputStream, CompressionMethod level = CompressionMethod.Default)
        {
            switch (level)
            {
            // bypass compression
            case CompressionMethod.None:
                return(inputStream);

            // average compression using DeflateStream
            case CompressionMethod.DeflateStream:
            {
                var stream = new SmallBlockMemoryStream();
                using (var output = new DeflateStream(stream, CompressionMode.Compress, true))
                {
                    int read;
                    var buffer = new byte[BufferSize];

                    while ((read = inputStream.Read(buffer, 0, BufferSize)) > 0)
                    {
                        output.Write(buffer, 0, read);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                return(stream);
            }

            // fast compression using LZF
            case CompressionMethod.LZF:
            {
                var buffer    = new byte[BufferSize];
                var output    = new byte[BufferSize * 2];                      // safe value for uncompressible data
                var outStream = new SmallBlockMemoryStream();
                var lzf       = new LZF();

                while (true)
                {
                    var readCount = (short)inputStream.Read(buffer, 0, buffer.Length);
                    if (readCount == 0)
                    {
                        break;
                    }

                    var writeCount = (short)lzf.Compress(buffer, readCount, output, output.Length);
                    if (writeCount == 0)
                    {
                        throw new InvalidOperationException("Cannot compress input stream.");
                    }

                    // write source size
                    var temp = BitConverter.GetBytes(readCount);
                    outStream.Write(temp, 0, ShortSize);

                    // write destination size
                    temp = BitConverter.GetBytes(writeCount);
                    outStream.Write(temp, 0, ShortSize);

                    // write data chunk
                    outStream.Write(output, 0, writeCount);
                }

                // rewind the output stream
                outStream.Seek(0, SeekOrigin.Begin);
                return(outStream);
            }
            }

            // unknown compression method
            throw new InvalidOperationException();
        }
Example #18
0
        public static Stream Decompress(Stream inputStream, CompressionMethod level = CompressionMethod.Default)
        {
            switch (level)
            {
            // bypass decompression
            case CompressionMethod.None:
                return(inputStream);

            // decompress using DeflateStream
            case CompressionMethod.DeflateStream:
            {
                var stream = new SmallBlockMemoryStream();
                using (var output = new DeflateStream(inputStream, CompressionMode.Decompress, true))
                {
                    int read;
                    var buffer = new byte[BufferSize];

                    while ((read = output.Read(buffer, 0, BufferSize)) > 0)
                    {
                        stream.Write(buffer, 0, read);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                return(stream);
            }

            // decompress using LZF
            case CompressionMethod.LZF:
            {
                var buffer    = new byte[BufferSize * 2];
                var output    = new byte[BufferSize];
                var temp      = new byte[ShortSize * 2];
                var outStream = new SmallBlockMemoryStream();
                var lzf       = new LZF();

                while (true)
                {
                    // read chunk sizes
                    if (inputStream.Read(temp, 0, ShortSize * 2) == 0)
                    {
                        break;
                    }

                    var sourceSize = BitConverter.ToInt16(temp, 0);
                    var destSize   = BitConverter.ToInt16(temp, ShortSize);

                    var readCount = inputStream.Read(buffer, 0, destSize);
                    if (readCount != destSize)
                    {
                        throw new InvalidOperationException("Cannot read input stream.");
                    }

                    var writeCount = lzf.Decompress(buffer, readCount, output, output.Length);
                    if (writeCount != sourceSize)
                    {
                        throw new InvalidOperationException("Cannot decompress input stream.");
                    }

                    outStream.Write(output, 0, writeCount);
                }

                // rewind the output stream
                outStream.Seek(0, SeekOrigin.Begin);
                return(outStream);
            }
            }

            // unknown compression method
            throw new InvalidOperationException();
        }
 public void CtorWithCapacity_succeeds(int capacity)
 {
     using (var standard = new MemoryStream(capacity))
         using (var subject = new SmallBlockMemoryStream(capacity))
             AssertEquivalent(standard, subject);
 }
 public void DefaultCtor_succeeds()
 {
     using (var standard = new MemoryStream())
         using (var subject = new SmallBlockMemoryStream())
             AssertEquivalent(standard, subject);
 }