Skip to content

Commit

Permalink
Throw exception in case we fail to getBuffersBytesCount (#138)
Browse files Browse the repository at this point in the history
Throw exception in case we fail to getBuffersBytesCount

Relates to: #125
  • Loading branch information
hiddenalpha authored Nov 9, 2023
1 parent b60e5ee commit 0d106b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/main/cpp/_nix_based/jssc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <time.h>
Expand Down Expand Up @@ -641,12 +642,22 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
*/
JNIEXPORT jintArray JNICALL Java_jssc_SerialNativeInterface_getBuffersBytesCount
(JNIEnv *env, jobject, jlong portHandle){
int err;
jint returnValues[2];
returnValues[0] = -1; //Input buffer
returnValues[1] = -1; //Output buffer

err = ioctl(portHandle, FIONREAD, &returnValues[0]) == -1
|| ioctl(portHandle, TIOCOUTQ, &returnValues[1]) == -1;
if( err ){
err = errno;
jclass exClz = env->FindClass("java/io/IOException");
if( exClz != NULL ) env->ThrowNew(exClz, strerror(err));
return NULL;
}

jintArray returnArray = env->NewIntArray(2);
ioctl(portHandle, FIONREAD, &returnValues[0]);
ioctl(portHandle, TIOCOUTQ, &returnValues[1]);
if( returnArray == NULL ) return NULL;
env->SetIntArrayRegion(returnArray, 0, 2, returnValues);
return returnArray;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jssc/SerialNativeInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public static String getLibraryVersion() {
*
* @since 0.8
*/
public native int[] getBuffersBytesCount(long handle);
public native int[] getBuffersBytesCount(long handle) throws IOException;

/**
* Set flow control mode
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,11 @@ public int[] readIntArray() throws SerialPortException {
*/
public int getInputBufferBytesCount() throws SerialPortException {
checkPortOpened("getInputBufferBytesCount()");
return serialInterface.getBuffersBytesCount(portHandle)[0];
try{
return serialInterface.getBuffersBytesCount(portHandle)[0];
}catch( IOException ex ){
throw SerialPortException.wrapNativeException(ex, this, "getInputBufferBytesCount");
}
}

/**
Expand All @@ -890,7 +894,11 @@ public int getInputBufferBytesCount() throws SerialPortException {
*/
public int getOutputBufferBytesCount() throws SerialPortException {
checkPortOpened("getOutputBufferBytesCount()");
return serialInterface.getBuffersBytesCount(portHandle)[1];
try{
return serialInterface.getBuffersBytesCount(portHandle)[1];
}catch( IOException ex ){
throw SerialPortException.wrapNativeException(ex, this, "getOutputBufferBytesCount");
}
}

/**
Expand Down

0 comments on commit 0d106b7

Please sign in to comment.