public static IAsyncResult BeginReadToEnd(this Stream stream, byte[] buffer, int offset, int count, AsyncCallback callback, Object state) { byte[] tempBuffer = new byte[count]; ByteArrayAsyncResult result = new ByteArrayAsyncResult(callback, state, buffer, offset, tempBuffer); ByteArrayAsyncState asyncState = new ByteArrayAsyncState {Result = result, Stream = stream}; stream.BeginRead(tempBuffer, 0, count, OnRead, asyncState); return result; }
public static IAsyncResult BeginReadToEnd(this Stream stream, byte[] buffer, int offset, int count, AsyncCallback callback, Object state) { if (stream == null) { throw new ArgumentNullException("stream"); } byte[] tempBuffer = new byte[count]; ByteArrayAsyncResult result = new ByteArrayAsyncResult(callback, state, buffer, offset, tempBuffer); ByteArrayAsyncState asyncState = new ByteArrayAsyncState { Result = result, Stream = stream }; stream.BeginRead(tempBuffer, 0, count, OnRead, asyncState); return(result); }
private static void OnRead(IAsyncResult ar) { ByteArrayAsyncState state = ar?.AsyncState as ByteArrayAsyncState; if (state == null) { return; } int bytesRead = state.Stream.EndRead(ar); if (bytesRead != 0) { Array.Copy(state.Result.TempBuffer, 0, state.Result.Result, state.Result.Index, bytesRead); state.Result.Index += bytesRead; state.Result.Length = state.Result.Index; state.Stream.BeginRead(state.Result.TempBuffer, 0, state.Result.Result.Length - state.Result.Length, OnRead, state); return; } state.Result.Complete(false); }