Read() публичный Метод

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
Результат int
Пример #1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int startOffset = (int)(_position % _blockSize);

            if (startOffset == 0 && (count % _blockSize == 0 || _position + count == Length))
            {
                // Aligned read - pass through to underlying stream.
                WrappedStream.Position = _position;
                int numRead = WrappedStream.Read(buffer, offset, count);
                _position += numRead;
                return(numRead);
            }

            long startPos = MathUtilities.RoundDown(_position, _blockSize);
            long endPos   = MathUtilities.RoundUp(_position + count, _blockSize);

            if (endPos - startPos > int.MaxValue)
            {
                throw new IOException("Oversized read, after alignment");
            }

            byte[] tempBuffer = new byte[endPos - startPos];

            WrappedStream.Position = startPos;
            int read      = WrappedStream.Read(tempBuffer, 0, tempBuffer.Length);
            int available = Math.Min(count, read - startOffset);

            Array.Copy(tempBuffer, startOffset, buffer, offset, available);

            _position += available;
            return(available);
        }
Пример #2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            try {
                if (Interlocked.Increment(ref m_ReadNesting) != 1)
                {
                    throw new NotSupportedException(SR.GetString(SR.net_io_invalidnestedcall, "Read", "read"));
                }

                if (m_HeadEOF)
                {
                    return(WrappedStream.Read(buffer, offset, count));
                }
                else
                {
                    int result = m_HeadStream.Read(buffer, offset, count);
                    m_HeadLength += result;
                    if (result == 0 && count != 0)
                    {
                        m_HeadEOF = true;
                        m_HeadStream.Close();
                        result = WrappedStream.Read(buffer, offset, count);
                    }
                    return(result);
                }
            }
            finally {
                Interlocked.Decrement(ref m_ReadNesting);
            }
        }
Пример #3
0
        private void DoOperation(ModifyStream modifyStream, ModifyBuffer modifyBuffer, int count)
        {
            int startOffset = (int)(_position % _blockSize);

            if (startOffset == 0 && (count % _blockSize == 0 || _position + count == Length))
            {
                WrappedStream.Position = _position;
                modifyStream(WrappedStream, 0, count);
                _position += count;
                return;
            }

            long unalignedEnd = _position + count;
            long alignedPos   = MathUtilities.RoundDown(_position, _blockSize);

            if (startOffset != 0)
            {
                WrappedStream.Position = alignedPos;
                WrappedStream.Read(_alignmentBuffer, 0, _blockSize);

                modifyBuffer(_alignmentBuffer, startOffset, 0, Math.Min(count, _blockSize - startOffset));

                WrappedStream.Position = alignedPos;
                WrappedStream.Write(_alignmentBuffer, 0, _blockSize);
            }

            alignedPos = MathUtilities.RoundUp(_position, _blockSize);
            if (alignedPos >= unalignedEnd)
            {
                _position = unalignedEnd;
                return;
            }

            int passthroughLength = (int)MathUtilities.RoundDown(_position + count - alignedPos, _blockSize);

            if (passthroughLength > 0)
            {
                WrappedStream.Position = alignedPos;
                modifyStream(WrappedStream, (int)(alignedPos - _position), passthroughLength);
            }

            alignedPos += passthroughLength;
            if (alignedPos >= unalignedEnd)
            {
                _position = unalignedEnd;
                return;
            }

            WrappedStream.Position = alignedPos;
            WrappedStream.Read(_alignmentBuffer, 0, _blockSize);

            modifyBuffer(_alignmentBuffer, 0, (int)(alignedPos - _position), (int)Math.Min(count - (alignedPos - _position), unalignedEnd - alignedPos));

            WrappedStream.Position = alignedPos;
            WrappedStream.Write(_alignmentBuffer, 0, _blockSize);

            _position = unalignedEnd;
        }
Пример #4
0
        public async Task Parameterized_Methods_Are_Properly_Wrapped()
        {
            var strm = Substitute.For <Stream>();

            strm.CanRead.Returns(true);
            strm.CanWrite.Returns(true);
            using (var wrprstrm = new WrappedStream(strm, false))
            {
                wrprstrm.Seek(0, SeekOrigin.Current);
                strm.Received(1).Seek(0, SeekOrigin.Current);

                wrprstrm.SetLength(10);
                strm.Received(1).SetLength(10);

                var arrByte = new byte[5];
                wrprstrm.Read(arrByte, 0, arrByte.Length);
                strm.Received(1).Read(arrByte, 0, arrByte.Length);

                wrprstrm.Write(arrByte, 0, arrByte.Length);
                strm.Received(1).Write(arrByte, 0, arrByte.Length);

                var anotherStrm = new MemoryStream();
                await wrprstrm.CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                await wrprstrm.ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                wrprstrm.WriteByte(20);
                strm.Received(1).WriteByte(20);

                var _  = wrprstrm.Equals(null);
                var __ = strm.Received(1).Equals(null);
            }
        }
Пример #5
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (m_Position >= m_Offset + m_Size)
            {
                return(0);
            }
            if (m_Position + count > m_Offset + m_Size)
            {
                count = (int)(m_Offset + m_Size - m_Position);
            }
            int result = WrappedStream.Read(buffer, offset, count);

            m_Position += result;
            return(result);
        }
        public void WrappedStream_Tests()
        {
            var memoryStream = new MemoryStream(Enumerable.Range(0, 128).Select(i => (byte)i).ToArray(), false);
            var buffer       = new byte[128];

            memoryStream.Position = 20;
            var wrapper = new WrappedStream(memoryStream, 100);
            var read    = wrapper.Read(buffer, 0, 3);

            CollectionAssert.AreEqual(new byte[] { 20, 21, 22 }, buffer.Take(read));
            //do not pass the length of the wrapper
            memoryStream.Position = 10;
            wrapper = new WrappedStream(memoryStream, 2);
            read    = wrapper.Read(buffer, 0, 100);
            CollectionAssert.AreEqual(new byte[] { 10, 11 }, buffer.Take(read));
            //do not pass the length of the internal stream
            memoryStream.Position = 126;
            wrapper = new WrappedStream(memoryStream, 2);
            read    = wrapper.Read(buffer, 0, 100);
            CollectionAssert.AreEqual(new byte[] { 126, 127 }, buffer.Take(read));
        }
Пример #7
0
 /// <summary>
 /// Reads a sequence of bytes from the current stream and advances the position
 /// within the stream by the number of bytes read.
 /// </summary>
 public override int Read(byte[] buffer, int offset, int count) => WrappedStream.Read(buffer, offset, count);
Пример #8
0
 public override int Read(byte[] buffer, int offset, int count) => WrappedStream.Read(buffer, offset, Math.Min(count, 2));
Пример #9
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(WrappedStream.Read(buffer, offset, count));
 }
Пример #10
0
 /// <summary>
 /// Reads a sequence of bytes from the current stream and advances the position
 /// within the stream by the number of bytes read.
 /// </summary>
 public override int Read(byte[] buffer, int offset, int count)
 {
     ThrowIfDisposed();
     return(WrappedStream.Read(buffer, offset, count));
 }
Пример #11
0
        public async Task Parameterized_Methods_Are_Properly_Wrapped()
        {
            var strm = Substitute.For <Stream>();

            using (var wrprstrm = new WrappedStream(strm, false))
            {
                wrprstrm.Seek(0, SeekOrigin.Current);
                strm.Received(1).Seek(0, SeekOrigin.Current);

                wrprstrm.SetLength(10);
                strm.Received(1).SetLength(10);

                var arrByte = new byte[5];
                wrprstrm.Read(arrByte, 0, arrByte.Length);
                strm.Received(1).Read(arrByte, 0, arrByte.Length);

                wrprstrm.Write(arrByte, 0, arrByte.Length);
                strm.Received(1).Write(arrByte, 0, arrByte.Length);

                var anotherStrm = new MemoryStream();
                await wrprstrm.CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).CopyToAsync(anotherStrm, 1, CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1).FlushAsync(CancellationToken.None).ConfigureAwait(false);

                await wrprstrm.WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .WriteAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                await wrprstrm.ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None).ConfigureAwait(false);

                await strm.Received(1)
                .ReadAsync(arrByte, 1, arrByte.Length, CancellationToken.None)
                .ConfigureAwait(false);

                var cb     = new AsyncCallback(ar => { });
                var result = wrprstrm.BeginRead(arrByte, 0, arrByte.Length, cb, null);
                strm.Received(1).BeginRead(arrByte, 0, arrByte.Length, cb, null);

                wrprstrm.BeginWrite(arrByte, 0, arrByte.Length, cb, null);
                strm.Received(1).BeginWrite(arrByte, 0, arrByte.Length, cb, null);

#if FEATURE_REMOTING
                wrprstrm.CreateObjRef(null);
                strm.Received(1).CreateObjRef(null);

                wrprstrm.InitializeLifetimeService();
                strm.Received(1).InitializeLifetimeService();
#else
                Assert.True(Assert.Throws <RemotingException>(() => wrprstrm.CreateObjRef(null))
                            .Message.Equals(WrappedStream.RemotingErrorTxt));
                Assert.True(Assert.Throws <RemotingException>(() => wrprstrm.InitializeLifetimeService())
                            .Message.Equals(WrappedStream.RemotingErrorTxt));
#endif
                wrprstrm.EndRead(result);
                strm.Received(1).EndRead(result);

                wrprstrm.EndWrite(result);
                strm.Received(1).EndWrite(result);

                wrprstrm.WriteByte(20);
                strm.Received(1).WriteByte(20);

                wrprstrm.Equals(null);
                strm.Received(1).Equals(null);
            }
        }
Пример #12
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            bool isDoingWrite = false;
            int  result       = -1;

            if (Interlocked.Increment(ref m_ReadNesting) != 1)
            {
                throw new NotSupportedException(SR.GetString(SR.net_io_invalidnestedcall, "Read", "read"));
            }

            try {
                if (m_BytesToSkip != 0L)
                {
                    // Sometime we want to combine cached + live stream AND the user requested explicit range starts from not 0
                    byte[] tempBuffer = new byte[4096];
                    while (m_BytesToSkip != 0L)
                    {
                        int bytes = WrappedStream.Read(tempBuffer, 0, (m_BytesToSkip < (long)tempBuffer.Length? (int)m_BytesToSkip: tempBuffer.Length));
                        if (bytes == 0)
                        {
                            m_SeenReadEOF = true;
                        }

                        m_BytesToSkip -= bytes;
                        if (!m_ShadowStreamIsDead)
                        {
                            m_ShadowStream.Write(tempBuffer, 0, bytes);
                        }
                    }
                }

                result = WrappedStream.Read(buffer, offset, count);
                if (result == 0)
                {
                    m_SeenReadEOF = true;
                }

                if (m_ShadowStreamIsDead)
                {
                    return(result);
                }
                isDoingWrite = true;
                m_ShadowStream.Write(buffer, offset, result);
                return(result);
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }

                GlobalLog.Print("ShadowReadStream::Read() Got Exception, disabling the shadow stream, stack trace = " + e.ToString());
                if (!m_ShadowStreamIsDead)
                {
                    // try to ignore even serious exception, since got nothing to loose?
                    m_ShadowStreamIsDead = true;
                    try {
                        if (m_ShadowStream is ICloseEx)
                        {
                            ((ICloseEx)m_ShadowStream).CloseEx(CloseExState.Abort | CloseExState.Silent);
                        }
                        else
                        {
                            m_ShadowStream.Close();
                        }
                    }
                    catch (Exception ee) {
                        if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                        {
                            throw;
                        }
                        GlobalLog.Print("ShadowReadStream::Read() Got (ignoring) Exception, on shadow stream.Close, stack trace = " + ee.ToString());
                    }
                }
                if (!isDoingWrite || m_ThrowOnWriteError)
                {
                    throw;
                }

                return(result);
            }
            finally {
                Interlocked.Decrement(ref m_ReadNesting);
            }
        }