Skip to content

Commit

Permalink
[Fix] #388 Fixed I2C.writeRead() not comparing written != writeSize
Browse files Browse the repository at this point in the history
  • Loading branch information
eitch committed Oct 3, 2024
1 parent db6a10a commit 8e0f46b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pi4j-core/src/main/java/com/pi4j/io/i2c/I2C.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,28 @@ default int writeRead(byte[] writeBuffer, byte[] readBuffer) {
*/
default int writeRead(byte[] writeBuffer, int writeSize, int writeOffset, byte[] readBuffer, int readSize,
int readOffset) {

// Check bounds for writeBuffer
if (writeOffset < 0)
throw new IndexOutOfBoundsException("Write offset cannot be negative!");
if (writeOffset + writeSize > writeBuffer.length)
throw new IndexOutOfBoundsException(
String.format("Write operation out of bounds. Write buffer length is %d. Yet write offset + size is=%d",
writeBuffer.length, writeOffset + writeSize));

// Check bounds for readBuffer
if (readOffset < 0)
throw new IndexOutOfBoundsException("Read offset cannot be negative!");
if (readOffset + readSize > readBuffer.length)
throw new IndexOutOfBoundsException(
String.format("Read operation out of bounds. Read buffer length is %d. Yet read offset + size is=%d",
readBuffer.length, readOffset + readSize));

return execute(() -> {
int written = write(writeBuffer, writeOffset, writeSize);
if (written != writeOffset)
if (written != writeSize)
throw new IllegalStateException(
"Expected to write " + writeOffset + " bytes but only wrote " + written + " bytes");
"Expected to write " + writeSize + " bytes but only wrote " + written + " bytes");
return read(readBuffer, readOffset, readSize);
});
}
Expand Down

0 comments on commit 8e0f46b

Please sign in to comment.