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

EndRead() public method

public EndRead ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult
return int
        public override int EndRead(IAsyncResult asyncResult) {

            if (Interlocked.Decrement(ref m_ReadNesting) != 0) {
                Interlocked.Increment(ref m_ReadNesting);
                throw new InvalidOperationException(SR.GetString(SR.net_io_invalidendcall, "EndRead"));
            }

            if (asyncResult == null) {
                throw new ArgumentNullException("asyncResult");
            }

            InnerAsyncResult myResult = asyncResult as InnerAsyncResult;

            if (myResult == null) {
                // We are just passing IO down, although the shadow stream should be dead for now.
                GlobalLog.Assert(m_ShadowStreamIsDead, "ForwardingReadStream::EndRead|m_ShadowStreamIsDead is false and asyncResult is not of InnerAsyncResult type {0}.", asyncResult.GetType().FullName);
                int bytes = m_OriginalStream.EndRead(asyncResult);
                if (bytes == 0)
                    m_SeenReadEOF = true;
            }

            // this is our wrapped AsyncResult
            bool suceess = false;
            try {
                myResult.InternalWaitForCompletion();
                // Exception?
                if (myResult.Result is Exception)
                    throw (Exception)(myResult.Result);
                suceess = true;
            }
            finally {
                if (!suceess && !m_ShadowStreamIsDead) {
                    m_ShadowStreamIsDead = true;
                    if (m_ShadowStream is ICloseEx)
                        ((ICloseEx)m_ShadowStream).CloseEx(CloseExState.Abort | CloseExState.Silent);
                    else
                        m_ShadowStream.Close();
                }
            }

            // Report the read count
            return (int)myResult.Result;
        }