Skip to content

Commit

Permalink
Hand exception around before throwing it as requested.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenalpha committed Nov 8, 2023
1 parent 7d1c643 commit a499ad4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/main/java/jssc/SerialNativeInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,7 @@ 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)
throws java.io.IOException;
public native boolean writeBytes(long handle, byte[] buffer) throws IOException;

/**
* Get bytes count in buffers of port
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package jssc;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;

Expand Down Expand Up @@ -406,9 +407,13 @@ public boolean setDTR(boolean enabled) throws SerialPortException {
*
* @throws SerialPortException if exception occurred
*/
public boolean writeBytes(byte[] buffer) throws SerialPortException, java.io.IOException {
public boolean writeBytes(byte[] buffer) throws SerialPortException {
checkPortOpened("writeBytes()");
return serialInterface.writeBytes(portHandle, buffer);
try {
return serialInterface.writeBytes(portHandle, buffer);
} catch(IOException ex) {
throw SerialPortException.wrapNativeException(ex, this, "writeBytes");
}
}

/**
Expand All @@ -422,7 +427,7 @@ public boolean writeBytes(byte[] buffer) throws SerialPortException, java.io.IOE
*
* @since 0.8
*/
public boolean writeByte(byte singleByte) throws SerialPortException, java.io.IOException {
public boolean writeByte(byte singleByte) throws SerialPortException {
checkPortOpened("writeByte()");
return writeBytes(new byte[]{singleByte});
}
Expand All @@ -438,7 +443,7 @@ public boolean writeByte(byte singleByte) throws SerialPortException, java.io.IO
*
* @since 0.8
*/
public boolean writeString(String string) throws SerialPortException, java.io.IOException {
public boolean writeString(String string) throws SerialPortException {
checkPortOpened("writeString()");
return writeBytes(string.getBytes());
}
Expand All @@ -455,7 +460,7 @@ public boolean writeString(String string) throws SerialPortException, java.io.IO
*
* @since 2.8.0
*/
public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException, java.io.IOException {
public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException {
checkPortOpened("writeString()");
return writeBytes(string.getBytes(charsetName));
}
Expand All @@ -471,7 +476,7 @@ public boolean writeString(String string, String charsetName) throws SerialPortE
*
* @since 0.8
*/
public boolean writeInt(int singleInt) throws SerialPortException, java.io.IOException {
public boolean writeInt(int singleInt) throws SerialPortException {
checkPortOpened("writeInt()");
return writeBytes(new byte[]{(byte)singleInt});
}
Expand All @@ -487,7 +492,7 @@ public boolean writeInt(int singleInt) throws SerialPortException, java.io.IOExc
*
* @since 0.8
*/
public boolean writeIntArray(int[] buffer) throws SerialPortException, java.io.IOException {
public boolean writeIntArray(int[] buffer) throws SerialPortException {
checkPortOpened("writeIntArray()");
byte[] byteArray = new byte[buffer.length];
for(int i = 0; i < buffer.length; i++){
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/jssc/SerialPortException.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class SerialPortException extends Exception {
*/
final public static String TYPE_INCORRECT_SERIAL_PORT = "Incorrect serial port";

/** Exception occurred in native code */
final public static String TYPE_NATIVE_EXCEPTION = "Native exception occurred: %s";

/** Serial port object **/
private SerialPort port;
/** Method name **/
Expand Down Expand Up @@ -110,6 +113,10 @@ public SerialPortException(String portName, String methodName, String exceptionT
this.exceptionType = exceptionType;
}

public static SerialPortException wrapNativeException(Exception ex, SerialPort port, String methodName) {
return new SerialPortException(port, methodName, String.format(TYPE_NATIVE_EXCEPTION, ex.getLocalizedMessage()));
}

/**
* Getting port name during operation with which the exception was called
* Deprecated: Use <code>getPort().getName()</code> instead.
Expand Down

0 comments on commit a499ad4

Please sign in to comment.