System.Net.Cache.ForwardingReadStream.Read C# (CSharp) Method

Read() public method

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int
        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 = m_OriginalStream.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 = m_OriginalStream.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 swallow 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());
                    }
                    catch {
                        GlobalLog.Print("ShadowReadStream::Read() Got (ignoring) Exception, on shadow stream.Close, stack trace = Non-CLS Compliant Exception");
                    }
                }
                if (!isDoingWrite || m_ThrowOnWriteError)
                    throw;

                return result;
            }
            catch {
                GlobalLog.Print("ShadowReadStream::Read() Got Exception, disabling the shadow stream, stack trace =  Non-CLS Compliant Exception");
                if (!m_ShadowStreamIsDead) {
                    // try to swallow 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 (NclUtilities.IsFatal(ee)) throw;
                        GlobalLog.Print("ShadowReadStream::Read() Got (ignoring) Exception, on shadow stream.Close, stack trace = " + ee.ToString());
                    }
                    catch {
                        GlobalLog.Print("ShadowReadStream::Read() Got (ignoring) Exception, on shadow stream.Close, stack trace =  Non-CLS Compliant Exception");
                    }
                }
                if (!isDoingWrite || m_ThrowOnWriteError)
                    throw;

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