diff --git a/src/HidApi.Net/Device.cs b/src/HidApi.Net/Device.cs index 486600b..cbd6312 100644 --- a/src/HidApi.Net/Device.cs +++ b/src/HidApi.Net/Device.cs @@ -68,44 +68,34 @@ public void Write(ReadOnlySpan data) /// /// Returns an input report. /// - /// Max length of the expected data. The value can be greater than the actual report. + /// Buffer to write the data into /// timeout in milliseconds. -1 for blocking mode. - /// The received data of the HID device. If the timeout is exceeded an empty result is returned. - /// Raised if maxlength is smaller than 0 + /// The length of the received data in bytes. If the timeout is exceeded 0 is returned. /// Raised on failure - public ReadOnlySpan ReadTimeout(int maxLength, int milliseconds) + public int ReadTimeout(Span buffer, int milliseconds) { - if (maxLength < 0) - throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, "Please provide a positive value"); - - ReadOnlySpan data = new byte[maxLength]; - var result = NativeMethods.ReadTimeOut(handle, data, milliseconds); + var result = NativeMethods.ReadTimeOut(handle, buffer, milliseconds); if (result == -1) HidException.Throw(handle); - return data[..result]; + return result; } /// /// Returns an input report. /// - /// Max length of the expected data. The value can be greater than the actual report. - /// The received data of the HID device. If non-blocking mode is enabled and no data is available an empty result will be returned. - /// Raised if maxlength is smaller than 0 + /// Buffer to write the data into + /// The length of the received data in bytes. If non-blocking mode is enabled and no data is available 0 will be returned. /// Raised on failure - public ReadOnlySpan Read(int maxLength) + public int Read(Span buffer) { - if (maxLength < 0) - throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, "Please provide a positive value"); - - ReadOnlySpan data = new byte[maxLength]; - var result = NativeMethods.Read(handle, data); + var result = NativeMethods.Read(handle, buffer); if (result == -1) HidException.Throw(handle); - return data[..result]; + return result; } /// diff --git a/src/HidApi.Net/Internal/NativeMethods.cs b/src/HidApi.Net/Internal/NativeMethods.cs index 350400a..35d7991 100644 --- a/src/HidApi.Net/Internal/NativeMethods.cs +++ b/src/HidApi.Net/Internal/NativeMethods.cs @@ -42,12 +42,12 @@ public static int Write(DeviceSafeHandle device, ReadOnlySpan data) return Write(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length); } - public static int ReadTimeOut(DeviceSafeHandle device, ReadOnlySpan data, int milliseconds) + public static int ReadTimeOut(DeviceSafeHandle device, Span data, int milliseconds) { return ReadTimeOut(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length, milliseconds); } - public static int Read(DeviceSafeHandle device, ReadOnlySpan data) + public static int Read(DeviceSafeHandle device, Span data) { return Read(device, ref MemoryMarshal.GetReference(data), (nuint) data.Length); }