internal static FileStreamAsyncResult CreateBufferedReadResult(int numBufferedBytes, AsyncCallback userCallback, Object userStateObject, bool isWrite) { FileStreamAsyncResult asyncResult = new FileStreamAsyncResult(numBufferedBytes, userCallback, userStateObject, isWrite); asyncResult.CallUserCallback(); return(asyncResult); }
public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { if (array == null) { throw new ArgumentNullException("array"); } if (offset < 0) { throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if (numBytes < 0) { throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); } if ((array.Length - offset) < numBytes) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); } if (this._handle.IsClosed) { __Error.FileNotOpen(); } if (!this._isAsync) { return base.BeginWrite(array, offset, numBytes, userCallback, stateObject); } if (!this.CanWrite) { __Error.WriteNotSupported(); } if (this._isPipe) { if (this._writePos > 0) { this.FlushWrite(false); } return this.BeginWriteCore(array, offset, numBytes, userCallback, stateObject); } if (this._writePos == 0) { if (this._readPos < this._readLen) { this.FlushRead(); } this._readPos = 0; this._readLen = 0; } int num = this._bufferSize - this._writePos; if (numBytes <= num) { if (this._writePos == 0) { this._buffer = new byte[this._bufferSize]; } Buffer.InternalBlockCopy(array, offset, this._buffer, this._writePos, numBytes); this._writePos += numBytes; FileStreamAsyncResult result = new FileStreamAsyncResult { _userCallback = userCallback, _userStateObject = stateObject, _waitHandle = null, _isWrite = true, _numBufferedBytes = numBytes }; result.CallUserCallback(); return result; } if (this._writePos > 0) { this.FlushWrite(false); } return this.BeginWriteCore(array, offset, numBytes, userCallback, stateObject); }
private unsafe FileStreamAsyncResult BeginWriteCore(byte[] bytes, int offset, int numBytes, AsyncCallback userCallback, object stateObject) { NativeOverlapped* overlappedPtr; FileStreamAsyncResult ar = new FileStreamAsyncResult { _handle = this._handle, _userCallback = userCallback, _userStateObject = stateObject, _isWrite = true }; ManualResetEvent event2 = new ManualResetEvent(false); ar._waitHandle = event2; Overlapped overlapped = new Overlapped(0, 0, IntPtr.Zero, ar); if (userCallback != null) { overlappedPtr = overlapped.Pack(IOCallback, bytes); } else { overlappedPtr = overlapped.UnsafePack(null, bytes); } ar._overlapped = overlappedPtr; if (this.CanSeek) { long length = this.Length; if (this._exposedHandle) { this.VerifyOSHandlePosition(); } if ((this._pos + numBytes) > length) { this.SetLengthCore(this._pos + numBytes); } overlappedPtr->OffsetLow = (int) this._pos; overlappedPtr->OffsetHigh = (int) (this._pos >> 0x20); this.SeekCore((long) numBytes, SeekOrigin.Current); } int hr = 0; if ((this.WriteFileNative(this._handle, bytes, offset, numBytes, overlappedPtr, out hr) == -1) && (numBytes != -1)) { if (hr == 0xe8) { ar.CallUserCallback(); return ar; } if (hr == 0x3e5) { return ar; } if (!this._handle.IsClosed && this.CanSeek) { this.SeekCore(0L, SeekOrigin.Current); } if (hr == 0x26) { __Error.EndOfFile(); return ar; } __Error.WinIOError(hr, string.Empty); } return ar; }
internal static FileStreamAsyncResult CreateBufferedReadResult(int numBufferedBytes, AsyncCallback userCallback, Object userStateObject, bool isWrite) { FileStreamAsyncResult asyncResult = new FileStreamAsyncResult(numBufferedBytes, userCallback, userStateObject, isWrite); asyncResult.CallUserCallback(); return asyncResult; }