Пример #1
0
        public unsafe int Read(IntPtr buffer, int offset, int count)
        {
            if (buffer == IntPtr.Zero)
            {
                throw new ArgumentNullException("buffer");
            }
            int numberOfBytesRead;

            if (!NativeFile.ReadFile(this.handle, (IntPtr)((void *)((IntPtr)(void *)buffer + offset)), count, out numberOfBytesRead, IntPtr.Zero))
            {
                throw new IOException("Unable to read from file", NativeFileStream.MarshalGetLastWin32Error());
            }
            this.position += (long)numberOfBytesRead;
            return(numberOfBytesRead);
        }
Пример #2
0
        /// <summary>
        /// Reads a block of bytes from the stream and writes the data in a given buffer.
        /// </summary>
        /// <param name="buffer">When this method returns, contains the specified buffer with the values between offset and (offset + count - 1) replaced by the bytes read from the current source. </param>
        /// <param name="offset">The byte offset in array at which the read bytes will be placed. </param>
        /// <param name="count">The maximum number of bytes to read. </param>
        /// <exception cref="ArgumentNullException">array is null. </exception>
        /// <returns>The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.</returns>
        public int Read(IntPtr buffer, int offset, int count)
        {
            if (buffer == IntPtr.Zero)
            {
                throw new ArgumentNullException("buffer");
            }

            int numberOfBytesRead;

            unsafe
            {
                void *pbuffer = (byte *)buffer + offset;
                {
                    if (!NativeFile.ReadFile(handle, (IntPtr)pbuffer, count, out numberOfBytesRead, IntPtr.Zero))
                    {
                        throw new IOException("Unable to read from file", MarshalGetLastWin32Error());
                    }
                }
                position += numberOfBytesRead;
            }
            return(numberOfBytesRead);
        }