Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report writeBytes errors via java exceptions #146

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/cpp/_nix_based/jssc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
* e-mail: scream3r.org@gmail.com
* web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/
*/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
Expand Down Expand Up @@ -527,11 +529,21 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR
*/
JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes
(JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){
jboolean ret = JNI_FALSE;
jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE);
jint bufferSize = env->GetArrayLength(buffer);
jint result = write(portHandle, jBuffer, (size_t)bufferSize);
if( result == -1 ){
int err = errno; /*bakup errno*/
jclass exClz = env->FindClass("java/io/IOException");
assert(exClz != NULL);
env->ThrowNew(exClz, strerror(err));
goto Finally;
}
ret = (result == bufferSize) ? JNI_TRUE : JNI_FALSE;
Finally:
env->ReleaseByteArrayElements(buffer, jBuffer, 0);
return result == bufferSize ? JNI_TRUE : JNI_FALSE;
return ret;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/jssc/SerialNativeInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ public static String getLibraryVersion() {
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean writeBytes(long handle, byte[] buffer);
public native boolean writeBytes(long handle, byte[] buffer)
throws java.io.IOException;

/**
* Get bytes count in buffers of port
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public boolean setDTR(boolean enabled) throws SerialPortException {
*
* @throws SerialPortException if exception occurred
*/
public boolean writeBytes(byte[] buffer) throws SerialPortException {
public boolean writeBytes(byte[] buffer) throws SerialPortException, java.io.IOException {
checkPortOpened("writeBytes()");
return serialInterface.writeBytes(portHandle, buffer);
}
Expand All @@ -422,7 +422,7 @@ public boolean writeBytes(byte[] buffer) throws SerialPortException {
*
* @since 0.8
*/
public boolean writeByte(byte singleByte) throws SerialPortException {
public boolean writeByte(byte singleByte) throws SerialPortException, java.io.IOException {
checkPortOpened("writeByte()");
return writeBytes(new byte[]{singleByte});
}
Expand All @@ -438,7 +438,7 @@ public boolean writeByte(byte singleByte) throws SerialPortException {
*
* @since 0.8
*/
public boolean writeString(String string) throws SerialPortException {
public boolean writeString(String string) throws SerialPortException, java.io.IOException {
checkPortOpened("writeString()");
return writeBytes(string.getBytes());
}
Expand All @@ -455,7 +455,7 @@ public boolean writeString(String string) throws SerialPortException {
*
* @since 2.8.0
*/
public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException {
public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException, java.io.IOException {
checkPortOpened("writeString()");
return writeBytes(string.getBytes(charsetName));
}
Expand All @@ -471,7 +471,7 @@ public boolean writeString(String string, String charsetName) throws SerialPortE
*
* @since 0.8
*/
public boolean writeInt(int singleInt) throws SerialPortException {
public boolean writeInt(int singleInt) throws SerialPortException, java.io.IOException {
checkPortOpened("writeInt()");
return writeBytes(new byte[]{(byte)singleInt});
}
Expand All @@ -487,7 +487,7 @@ public boolean writeInt(int singleInt) throws SerialPortException {
*
* @since 0.8
*/
public boolean writeIntArray(int[] buffer) throws SerialPortException {
public boolean writeIntArray(int[] buffer) throws SerialPortException, java.io.IOException {
checkPortOpened("writeIntArray()");
byte[] byteArray = new byte[buffer.length];
for(int i = 0; i < buffer.length; i++){
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/jssc/SerialNativeInterfaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public void testPrintVersion() {

}

@Test(expected = java.io.IOException.class)
public void reportsWriteErrorsAsIOException() throws Exception {
long fd = -1; /*bad file by intent*/
byte[] buf = new byte[]{ 0x6A, 0x73, 0x73, 0x63, 0x0A };
SerialNativeInterface testTarget = new SerialNativeInterface();
testTarget.writeBytes(fd, buf);
}

}