Exemplo n.º 1
0
        public void FixedBufferCanAdjustReadOffsetOnOverwrite()
        {
            int dataLength   = 100;
            int bufferLength = 30;
            int readOffset   = 10;

            int writeInitial = 10;
            int readInitial  = 5;

            FixedSizeBuffer <int> fixedSizeBuffer = new FixedSizeBuffer <int>(bufferLength);
            var numbers  = Enumerable.Range(0, dataLength).ToArray();
            var readData = new int[numbers.Length + readOffset];

            fixedSizeBuffer.Write(Enumerable.Range(5000, writeInitial).ToArray(), 0, writeInitial);
            fixedSizeBuffer.Read(readData, 0, readInitial);
            Array.Clear(readData, 0, readData.Length);

            int written = fixedSizeBuffer.Write(numbers, 0, numbers.Length);

            Assert.AreEqual(numbers.Length, written);
            Assert.AreEqual(fixedSizeBuffer.Buffered, bufferLength);

            int read = fixedSizeBuffer.Read(readData, readOffset, readData.Length);

            Assert.AreEqual(read, bufferLength);
            Assert.AreEqual(fixedSizeBuffer.Buffered, 0);

            Array.Copy(readData, readOffset, readData, 0, dataLength);
            Array.Copy(numbers, dataLength - bufferLength, numbers, 0, bufferLength);
            for (int i = 0; i < bufferLength; i++)
            {
                Assert.AreEqual(readData[i], numbers[i]);
            }
        }
Exemplo n.º 2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int read0 = 0;
            int read1 = 0;

            Array.Clear(buffer, offset, count - offset);

            do
            {
                read1  = _buffer.Read(buffer, offset + read0, count - read0);
                read0 += read1;
            } while (read0 < count /* && (read1 != 0 && Position == Length)*/); //todo: abort if read1 = 0?

            return(read0);
        }
 /// <summary>
 ///     Reads a sequence of bytes from the internal buffer of the <see cref="WriteableBufferingSource" /> and advances the position within the internal buffer by the
 ///     number of bytes read.
 /// </summary>
 /// <param name="buffer">
 ///     An array of bytes. When this method returns, the <paramref name="buffer" /> contains the specified
 ///     byte array with the values between <paramref name="offset" /> and (<paramref name="offset" /> +
 ///     <paramref name="count" /> - 1) replaced by the bytes read from the internal buffer.
 /// </param>
 /// <param name="offset">
 ///     The zero-based byte offset in the <paramref name="buffer" /> at which to begin storing the data
 ///     read from the internal buffer.
 /// </param>
 /// <param name="count">The maximum number of bytes to read from the internal buffer.</param>
 /// <returns>The total number of bytes read into the <paramref name="buffer"/>.</returns>
 public int Read(byte[] buffer, int offset, int count)
 {
     lock (_bufferlock)
     {
         int read = _buffer.Read(buffer, offset, count);
         if (FillWithZeros)
         {
             if (read < count)
             {
                 Array.Clear(buffer, offset + read, count - read);
             }
             return(count);
         }
         return(read);
     }
 }
Exemplo n.º 4
0
        public int Read(byte[] buffer, int offset, int count)
        {
            int read = 0;

            Array.Clear(buffer, offset, count - offset);

            if (_buffer.Buffered < _buffer.Length / 2)
            {
                return(count);
            }
            else
            {
                do
                {
                    read += _buffer.Read(buffer, offset + read, count - read);
                } while (read < count);
                return(read);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Reads a sequence of bytes from internal buffer and advances the position by the
        ///     number of bytes read.
        /// </summary>
        /// <param name="buffer">
        ///     An array of bytes. When this method returns, the <paramref name="buffer" /> contains the specified
        ///     byte array with the values between <paramref name="offset" /> and (<paramref name="offset" /> +
        ///     <paramref name="count" /> - 1) replaced by the bytes read from the <see cref="BufferSource"/>.
        /// </param>
        /// <param name="offset">
        ///     The zero-based byte offset in the <paramref name="buffer" /> at which to begin storing the data
        ///     read from the current stream.
        /// </param>
        /// <param name="count">The maximum number of bytes to read from the <see cref="BufferSource"/>.</param>
        /// <returns>The total number of bytes read into the buffer.</returns>
        /// <exception cref="ObjectDisposedException">BufferSource</exception>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_disposing)
            {
                throw new ObjectDisposedException("BufferSource");
            }

            int read0 = 0, read1;

            Array.Clear(buffer, offset, count - offset);

            do
            {
                read1  = _buffer.Read(buffer, offset + read0, count - read0);
                read0 += read1;
            } while (read0 < count &&
                     !(read1 <= 0 && _eofCounter >= 5));    //if the buffering thread could not read any data for 5 times, we abort here

            return(read0);
        }
Exemplo n.º 6
0
        public void FixedBufferCanReadWrite()
        {
            int length     = 100;
            int readOffset = 10;

            FixedSizeBuffer <int> fixedSizeBuffer = new FixedSizeBuffer <int>(length);
            var numbers  = Enumerable.Range(0, length).ToArray();
            var readData = new int[numbers.Length + readOffset];

            int written = fixedSizeBuffer.Write(numbers, 0, numbers.Length);

            Assert.AreEqual(numbers.Length, written);
            Assert.AreEqual(fixedSizeBuffer.Buffered, numbers.Length);

            fixedSizeBuffer.Read(readData, readOffset, length);
            Assert.AreEqual(fixedSizeBuffer.Buffered, 0);

            Array.Copy(readData, readOffset, readData, 0, length);
            for (int i = 0; i < length; i++)
            {
                Assert.AreEqual(readData[i], numbers[i]);
            }
        }