diff --git a/src/main/cpp/_nix_based/jssc.cpp b/src/main/cpp/_nix_based/jssc.cpp index c98403a6c..c650e8d50 100644 --- a/src/main/cpp/_nix_based/jssc.cpp +++ b/src/main/cpp/_nix_based/jssc.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -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; } diff --git a/src/main/java/jssc/SerialNativeInterface.java b/src/main/java/jssc/SerialNativeInterface.java index 40022472e..2f2e0b9f2 100644 --- a/src/main/java/jssc/SerialNativeInterface.java +++ b/src/main/java/jssc/SerialNativeInterface.java @@ -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 diff --git a/src/main/java/jssc/SerialPort.java b/src/main/java/jssc/SerialPort.java index 713fa769c..8fef42cd1 100644 --- a/src/main/java/jssc/SerialPort.java +++ b/src/main/java/jssc/SerialPort.java @@ -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"); + } } /** @@ -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"); + } } /**