private unsafe void WriteCore(byte[] buffer, int offset, int count)
{
Debug.Assert(!_fileHandle.IsClosed, "!_handle.IsClosed");
Debug.Assert(CanWrite, "_parent.CanWrite");
Debug.Assert(buffer != null, "buffer != null");
Debug.Assert(_readPos == _readLength, "_readPos == _readLen");
Debug.Assert(offset >= 0, "offset is negative");
Debug.Assert(count >= 0, "count is negative");
if (_useAsyncIO)
{
WriteInternalCoreAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
return;
}
// Make sure we are writing to the position that we think we are
VerifyOSHandlePosition();
int errorCode = 0;
int r = WriteFileNative(_fileHandle, buffer, offset, count, null, out errorCode);
if (r == -1)
{
// For pipes, ERROR_NO_DATA is not an error, but the pipe is closing.
if (errorCode == ERROR_NO_DATA)
{
r = 0;
}
else
{
// ERROR_INVALID_PARAMETER may be returned for writes
// where the position is too large (i.e. writing at Int64.MaxValue
// on Win9x) OR for synchronous writes to a handle opened
// asynchronously.
if (errorCode == ERROR_INVALID_PARAMETER)
throw new IOException(SR.IO_FileTooLongOrHandleNotSync);
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
}
Debug.Assert(r >= 0, "FileStream's WriteCore is likely broken.");
_filePosition += r;
return;
}