Beispiel #1
0
        public override unsafe int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset > buffer.Length || (offset + count) > buffer.Length)
            {
                throw new ArgumentException();
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            VerifyHandle();

            bool success;
            uint read;

            fixed(byte *pb = &buffer[offset])
            {
                NativeOverlapped overlapped = default(NativeOverlapped);

                success = NativeMethods.SFileReadFile(_handle, new IntPtr(pb), unchecked ((uint)count), out read, ref overlapped);
            }

            if (!success)
            {
                int lastError = Win32Methods.GetLastError();
                if (lastError != 38) // EOF
                {
                    throw new Win32Exception(lastError);
                }
            }

            return(unchecked ((int)read));
        }