Provides character based and byte based stream-like read operations over multiple streams and provides methods to add data to the front of the buffer.
        public void CanReadSingleUnicodeCharacter()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("é"));

            Assert.AreEqual(stack.Read(), 'é');
        }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RebufferableBinaryReader"/> class.
 /// </summary>
 /// <param name="input">
 /// The input stream to read from.
 /// </param>
 /// <param name="encoding">
 /// The encoding to use for character based operations.
 /// </param>
 /// <param name="bufferSize">
 /// The buffer size to use for new buffers.
 /// </param>
 public RebufferableBinaryReader(Stream input, Encoding encoding, int bufferSize)
 {
     this.stream      = input;
     this.streamStack = new BinaryStreamStack(encoding);
     this.encoding    = encoding;
     this.bufferSize  = bufferSize;
 }
        public void CanReadLineNearEnd()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("\r\n--endboundary--"));

            Assert.AreEqual(stack.ReadLine(), string.Empty);
            Assert.AreEqual(stack.ReadLine(), "--endboundary--");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RebufferableBinaryReader"/> class.
 /// </summary>
 /// <param name="input">
 /// The input stream to read from.
 /// </param>
 /// <param name="encoding">
 /// The encoding to use for character based operations.
 /// </param>
 /// <param name="bufferSize">
 /// The buffer size to use for new buffers.
 /// </param>
 /// <param name="leaveOpen">
 /// If true, leave the underlying stream open on Dispose.
 /// </param>
 public RebufferableBinaryReader(Stream input, Encoding encoding, int bufferSize, bool leaveOpen = false)
 {
     this.stream      = input;
     this.streamStack = new BinaryStreamStack(encoding);
     this.encoding    = encoding;
     this.bufferSize  = bufferSize;
     this.leaveOpen   = leaveOpen;
 }
        public void CanReadMultipleUnicodeCharacters()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("تست"));

            Assert.AreEqual(stack.Read(), 'ت');
            Assert.AreEqual(stack.Read(), 'س');
            Assert.AreEqual(stack.Read(), 'ت');
        }
        public void CanReadLineSingleBuffer()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("6chars" + Environment.NewLine));

            var buffer = new byte[Encoding.UTF8.GetByteCount("6chars" + Environment.NewLine)];
            string result = stack.ReadLine();
            Assert.Equal(result, "6chars");
        }
        public void CanReadAcrossMultipleBuffers()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("ars"));
            stack.Push(TestUtil.StringToByteNoBom("6ch"));

            var buffer = new byte[6];
            stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "6chars");
        }
        public void CanReadLineMultiplesLineInSingleBuffer()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("al" + Environment.NewLine));
            stack.Push(TestUtil.StringToByteNoBom("6chars" + Environment.NewLine + "5char" + Environment.NewLine + "Parti"));

            Assert.AreEqual(stack.ReadLine(), "6chars");
            Assert.AreEqual(stack.ReadLine(), "5char");
            Assert.AreEqual(stack.ReadLine(), "Partial");
        }
        public void CanReadLineAcrossMultipleBuffers()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("13anotherline" + Environment.NewLine));
            stack.Push(TestUtil.StringToByteNoBom("ars" + Environment.NewLine));
            stack.Push(TestUtil.StringToByteNoBom("6ch"));

            string line = stack.ReadLine();
            Assert.AreEqual(line, "6chars");

            line = stack.ReadLine();
            Assert.AreEqual(line, "13anotherline");
        }
        public void CanReadSingleCharacterOverBuffers()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("def"));
            stack.Push(TestUtil.StringToByteNoBom("abc"));

            Assert.AreEqual(stack.Read(), 'a');
            Assert.AreEqual(stack.Read(), 'b');
            Assert.AreEqual(stack.Read(), 'c');
            Assert.AreEqual(stack.Read(), 'd');
            Assert.AreEqual(stack.Read(), 'e');
            Assert.AreEqual(stack.Read(), 'f');
        }
        public void ReadCanResumeInterruptedStream()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("6chars"));

            var buffer = new byte[4];
            int amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "6cha");
            Assert.AreEqual(amountRead, 4);

            stack.Push(TestUtil.StringToByteNoBom("14intermission"));
            buffer = new byte[14];
            amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "14intermission");
            Assert.AreEqual(amountRead, 14);

            buffer = new byte[2];
            amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "rs");
            Assert.AreEqual(amountRead, 2);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RebufferableBinaryReader"/> class.
 /// </summary>
 /// <param name="input">
 /// The input stream to read from.
 /// </param>
 /// <param name="encoding">
 /// The encoding to use for character based operations.
 /// </param>
 /// <param name="bufferSize">
 /// The buffer size to use for new buffers.
 /// </param>
 /// <param name="leaveOpen">
 /// If true, leave the underlying stream open on Dispose.
 /// </param>
 public RebufferableBinaryReader(Stream input, Encoding encoding, int bufferSize, bool leaveOpen = false)
 {
     this.stream = input;
     this.streamStack = new BinaryStreamStack(encoding);
     this.encoding = encoding;
     this.bufferSize = bufferSize;
     this.leaveOpen = leaveOpen;
 }
        public void ReturnsRemainderOnNoNewline()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("noline"));

            string noline = stack.ReadLine();
            Assert.AreEqual(noline, "noline");
        }
        public void ReturnsNullOnNoStreams()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);

            string noline = stack.ReadLine();
            Assert.IsNull(noline);
        }
        public void ReadReturnsZeroOnNoData()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);

            var buffer = new byte[6];
            int amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "\0\0\0\0\0\0");
            Assert.AreEqual(amountRead, 0);
        }
        public void ReadLineCanResumeInterruptedStream()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("6chars" + Environment.NewLine + "Resume" + Environment.NewLine));

            Assert.AreEqual(stack.ReadLine(), "6chars");

            stack.Push(TestUtil.StringToByteNoBom("Interrupt" + Environment.NewLine));

            Assert.AreEqual(stack.ReadLine(), "Interrupt");
            Assert.AreEqual(stack.ReadLine(), "Resume");
        }
        public void ReadCorrectlyHandlesSmallerBufferThenStream()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("6chars"));

            var buffer = new byte[4];
            stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "6cha");

            buffer = new byte[2];
            stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "rs");
        }
        public void MixReadAndReadLineWithInterrupt()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("6chars" + Environment.NewLine + "Resume" + Environment.NewLine));

            Assert.AreEqual(stack.Read(), '6');

            Assert.AreEqual(stack.ReadLine(), "chars");

            stack.Push(TestUtil.StringToByteNoBom("Interrupt" + Environment.NewLine));

            Assert.AreEqual(stack.ReadLine(), "Interrupt");
            Assert.AreEqual(stack.Read(), 'R');
            Assert.AreEqual(stack.ReadLine(), "esume");
        }
        public void MixReadAndReadBufferWithMultipleStreams()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("7inners"));
            stack.Push(TestUtil.StringToByteNoBom("6chars"));

            var buffer = new byte[2];

            Assert.AreEqual(stack.Read(), '6');

            int amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "ch");
            Assert.AreEqual(amountRead, 2);

            Assert.AreEqual(stack.Read(), 'a');
            Assert.AreEqual(stack.Read(), 'r');

            amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "s7");
            Assert.AreEqual(amountRead, 2);

            Assert.AreEqual(stack.Read(), 'i');
            Assert.AreEqual(stack.Read(), 'n');
            Assert.AreEqual(stack.Read(), 'n');
            Assert.AreEqual(stack.Read(), 'e');

            amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "rs");
            Assert.AreEqual(amountRead, 2);
        }
        public void CanReadSingleBuffer()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("6chars"));

            var buffer = new byte[Encoding.UTF8.GetByteCount("6chars")];
            stack.Read(buffer, 0, buffer.Length);
            string result = Encoding.UTF8.GetString(buffer);
            Assert.AreEqual(result, "6chars");
        }
        public void CanReadMixedUnicodeAndAsciiCharacters()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("تست.jpg"));

            Assert.AreEqual(stack.Read(), 'ت');
            Assert.AreEqual(stack.Read(), 'س');
            Assert.AreEqual(stack.Read(), 'ت');
            Assert.AreEqual(stack.Read(), '.');
            Assert.AreEqual(stack.Read(), 'j');
            Assert.AreEqual(stack.Read(), 'p');
            Assert.AreEqual(stack.Read(), 'g');
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="RebufferableBinaryReader" /> class.
 /// </summary>
 /// <param name="input">
 ///     The input stream to read from.
 /// </param>
 /// <param name="encoding">
 ///     The encoding to use for character based operations.
 /// </param>
 /// <param name="bufferSize">
 ///     The buffer size to use for new buffers.
 /// </param>
 public RebufferableBinaryReader(Stream input, Encoding encoding, int bufferSize)
 {
     stream = input;
     streamStack = new BinaryStreamStack(encoding);
     this.encoding = encoding;
     this.bufferSize = bufferSize;
 }
        public void ReadCorrectlyHandlesLargerBufferThenStream()
        {
            var stack = new BinaryStreamStack(Encoding.UTF8);
            stack.Push(TestUtil.StringToByteNoBom("6chars"));

            var buffer = new byte[10];
            int amountRead = stack.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(Encoding.UTF8.GetString(buffer), "6chars\0\0\0\0");
            Assert.AreEqual(amountRead, 6);
        }