Ejemplo n.º 1
0
        public void MoveForward_shifts_index_for_byte_retrieval()
        {
            int window = 100;
            var buffer = new byte[window * 2];

            for (int i = 0; i < buffer.Length; ++i)
            {
                buffer[i] = (byte)(200 - i);
            }
            var streamMock = new Mock <MemoryStream>(MockBehavior.Strict);

            SetupMockStreamRead(streamMock)
            .Returns((byte[] s, int o, int l) =>
            {
                int actualLength = MathEx.Bounded(0, Math.Min(l, buffer.Length), buffer.Length);
                Array.Copy(buffer, 0, s, o, actualLength);
                return(actualLength);
            });
            var buf = new SlidingStreamBuffer(streamMock.Object, window);

            buf.Warmup();
            Assert.AreEqual(buffer[5], buf[5]);
            buf.MoveForward(1);
            buf.MoveForward(3);
            Assert.AreEqual(buffer[9], buf[5]);
        }
Ejemplo n.º 2
0
        private void SendByteDelta(ByteDelta delta, Stream inputStream, Stream outputStream)
        {
            outputStream.WriteByte(DeltaStreamConstants.NEW_BLOCK_START_MARKER);
            outputStream.WriteInt(delta.Length);
            var buffer = new byte[delta.Length];

            inputStream.Seek(delta.Offset, SeekOrigin.Begin);
            long totalRead = 0;

            while (totalRead < delta.Length)
            {
                var toRead     = (int)MathEx.Bounded(0, StreamChunkSize, delta.Length - totalRead);
                int readLength = inputStream.Read(buffer, 0, toRead);
                if (readLength == 0 && totalRead < delta.Length)
                {
                    throw new IOException("Input stream offset out of bounds, or not enough data available");
                }
                outputStream.Write(buffer, 0, readLength);
                totalRead += readLength;
            }
        }
Ejemplo n.º 3
0
        public void GetBuffer_returns_a_buffer_with_valid_data()
        {
            int window = 100;
            var buffer = new byte[window * 2];

            for (int i = 0; i < buffer.Length; ++i)
            {
                buffer[i] = (byte)(200 - i);
            }
            var streamMock = new Mock <MemoryStream>(MockBehavior.Strict);

            SetupMockStreamRead(streamMock)
            .Returns((byte[] s, int o, int l) =>
            {
                int actualLength = MathEx.Bounded(0, Math.Min(l, buffer.Length), buffer.Length);
                Array.Copy(buffer, 0, s, o, actualLength);
                return(actualLength);
            });
            var buf = new SlidingStreamBuffer(streamMock.Object, window);

            buf.Warmup();
            VerifyBufferPartialEquality(buffer, buf.GetBuffer());
        }
Ejemplo n.º 4
0
        public void GetByteAt_retrieves_correct_byte()
        {
            int window = 100;
            var buffer = new byte[window];

            for (int i = 0; i < buffer.Length; ++i)
            {
                buffer[i] = (byte)(200 - i);
            }
            var streamMock = new Mock <MemoryStream>(MockBehavior.Strict);

            SetupMockStreamRead(streamMock)
            .Returns((byte[] s, int o, int l) =>
            {
                int actualLength = MathEx.Bounded(0, Math.Min(l, buffer.Length), buffer.Length);
                Array.Copy(buffer, 0, s, o, actualLength);
                return(actualLength);
            });
            var buf = new SlidingStreamBuffer(streamMock.Object, window);

            buf.Warmup();
            Assert.AreEqual(buffer[5], buf.GetByteAt(5));
        }
Ejemplo n.º 5
0
 /// <summary>
 ///  Returns value between [0, window]
 /// </summary>
 /// <returns></returns>
 public long GetNumBytesAvailable()
 {
     return(MathEx.Bounded(0, _window, _totalValidBytesInBuffer - _bufferPosition));
 }