private unsafe static int ReadFileNative(SafeFileHandle hFile, byte[] bytes, int offset, int count, int mustBeZero, out int errorCode)
{
BCLDebug.Assert(offset >= 0, "offset >= 0");
BCLDebug.Assert(count >= 0, "count >= 0");
BCLDebug.Assert(bytes != null, "bytes != null");
// Don't corrupt memory when multiple threads are erroneously writing
// to this stream simultaneously.
if (bytes.Length - offset < count)
throw new IndexOutOfRangeException(Environment.GetResourceString("IndexOutOfRange_IORaceCondition"));
// You can't use the fixed statement on an array of length 0.
if (bytes.Length==0) {
errorCode = 0;
return 0;
}
int r;
int numBytesRead;
fixed(byte* p = bytes) {
r = ReadFile(hFile, p + offset, count, out numBytesRead, Win32Native.NULL);
}
if (r==0) {
errorCode = Marshal.GetLastWin32Error();
if (errorCode == ERROR_BROKEN_PIPE) {
// A pipe into stdin was closed. Not an error, but EOF.
return 0;
}
return -1;
}
else
errorCode = 0;
return numBytesRead;
}