public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, Object state) {
try {
if (Interlocked.Increment(ref m_ReadNesting) != 1) {
throw new NotSupportedException(SR.GetString(SR.net_io_invalidnestedcall, "BeginRead", "read"));
}
if (m_ReadCallback == null) {
m_ReadCallback = new AsyncCallback(ReadCallback);
}
if (m_HeadEOF) {
return m_TailStream.BeginRead(buffer, offset, count, callback, state);
}
else {
InnerAsyncResult userResult = new InnerAsyncResult(state, callback, buffer, offset, count);
IAsyncResult ar = m_HeadStream.BeginRead(buffer, offset, count, m_ReadCallback, userResult);
if (!ar.CompletedSynchronously)
{
return userResult;
}
int bytes = m_HeadStream.EndRead(ar);
m_HeadLength += bytes;
//check on EOF condition
if (bytes == 0 && userResult.Count != 0) {
//Got a first stream EOF
m_HeadEOF = true;
m_HeadStream.Close();
return m_TailStream.BeginRead(buffer, offset, count, callback, state);
}
else {
// just complete user IO
userResult.Buffer = null;
userResult.InvokeCallback(count);
return userResult;
}
}
}
catch {
Interlocked.Decrement(ref m_ReadNesting);
throw;
}
}