diff --git a/src/main/java/jssc/SerialNativeAccess.java b/src/main/java/jssc/SerialNativeAccess.java index f8c163695..533b6a4e3 100644 --- a/src/main/java/jssc/SerialNativeAccess.java +++ b/src/main/java/jssc/SerialNativeAccess.java @@ -37,7 +37,7 @@ public class SerialNativeAccess { private int osType = -1; - private static SerialNativeInterface sni = new SerialNativeInterface(); + private static final SerialNativeInterface sni = new SerialNativeInterface(); private static SerialNativeAccess instance = null; public static SerialNativeAccess getInstance() { @@ -79,36 +79,40 @@ else if(osName.equals("Mac OS X") || osName.equals("Darwin")){//os.name "Darwin" osName = "mac_os_x"; osType = SerialNativeInterface.OS_MAC_OS_X; }//<- since 0.9.0 - - if(architecture.equals("i386") || architecture.equals("i686")){ - architecture = "x86"; - } - else if(architecture.equals("amd64") || architecture.equals("universal")){//os.arch "universal" since 2.6.0 - architecture = "x86_64"; - } - else if(architecture.equals("arm")) {//since 2.1.0 - String floatStr = "sf"; - if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){ - floatStr = "hf"; - } - else { - try { - Process readelfProcess = Runtime.getRuntime().exec("readelf -A /proc/self/exe"); - BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream())); - String buffer = ""; - while((buffer = reader.readLine()) != null && !buffer.isEmpty()){ - if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){ - floatStr = "hf"; - break; + switch (architecture) { + case "i386": + case "i686": + architecture = "x86"; + break; + case "amd64": + case "universal": + //os.arch "universal" since 2.6.0 + architecture = "x86_64"; + break; + case "arm": + //since 2.1.0 + String floatStr = "sf"; + if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){ + floatStr = "hf"; + } + else { + try { + Process readelfProcess = Runtime.getRuntime().exec("readelf -A /proc/self/exe"); + BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream())); + String buffer; + while((buffer = reader.readLine()) != null && !buffer.isEmpty()){ + if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){ + floatStr = "hf"; + break; + } } + reader.close(); } - reader.close(); - } - catch (Exception ex) { - //Do nothing - } - } - architecture = "arm" + floatStr; + catch (Exception ex) { + //Do nothing + } + } architecture = "arm" + floatStr; + break; } libFolderPath = libRootFolder + fileSeparator + ".jssc" + fileSeparator + osName; @@ -232,6 +236,7 @@ private static boolean extractLib(String libFilePath, String osName, String libN /** * Get OS type (OS_LINUX || OS_WINDOWS || OS_SOLARIS) * + * @return * @since 0.8 */ public int getOsType() { diff --git a/src/main/java/org/scream3r/jssc/SerialPort.java b/src/main/java/org/scream3r/jssc/SerialPort.java index 613dce389..ce79ee8c8 100644 --- a/src/main/java/org/scream3r/jssc/SerialPort.java +++ b/src/main/java/org/scream3r/jssc/SerialPort.java @@ -35,10 +35,10 @@ */ public class SerialPort { - private SerialNativeInterface serialInterface; + private final SerialNativeInterface serialInterface; private SerialPortEventListener eventListener; private long portHandle; - private String portName; + private final String portName; private boolean portOpened = false; private boolean maskAssigned = false; private boolean eventListenerAdded = false; @@ -234,6 +234,7 @@ else if(stopBits == 3){ * parameter "PURGE_RXCLEAR | PURGE_TXCLEAR". *
Note: some devices or drivers may not support this function * + * @param flags * @return If the operation is successfully completed, the method returns true, otherwise false. * * @throws SerialPortException @@ -257,6 +258,7 @@ public boolean purgePort(int flags) throws SerialPortException { * For example if messages about data receipt and CTS and DSR status changing * shall be received, it is required to set the mask - "MASK_RXCHAR | MASK_CTS | MASK_DSR" * + * @param mask * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -267,24 +269,14 @@ public boolean setEventsMask(int mask) throws SerialPortException { SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_SOLARIS || SerialNativeAccess.getInstance().getOsType() == SerialNativeInterface.OS_MAC_OS_X){//since 0.9.0 linuxMask = mask; - if(mask > 0){ - maskAssigned = true; - } - else { - maskAssigned = false; - } + maskAssigned = mask > 0; return true; } boolean returnValue = serialInterface.setEventsMask(portHandle, mask); if(!returnValue){ throw new SerialPortException(portName, "setEventsMask()", SerialPortException.TYPE_CANT_SET_MASK); } - if(mask > 0){ - maskAssigned = true; - } - else { - maskAssigned = false; - } + maskAssigned = mask > 0; return returnValue; } @@ -317,6 +309,7 @@ private int getLinuxMask() { /** * Change RTS line state. Set "true" for switching ON and "false" for switching OFF RTS line * + * @param enabled * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -329,6 +322,7 @@ public boolean setRTS(boolean enabled) throws SerialPortException { /** * Change DTR line state. Set "true" for switching ON and "false" for switching OFF DTR line * + * @param enabled * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -341,6 +335,7 @@ public boolean setDTR(boolean enabled) throws SerialPortException { /** * Write byte array to port * + * @param buffer * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -353,6 +348,7 @@ public boolean writeBytes(byte[] buffer) throws SerialPortException { /** * Write single byte to port * + * @param singleByte * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -367,6 +363,7 @@ public boolean writeByte(byte singleByte) throws SerialPortException { /** * Write String to port * + * @param string * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -381,9 +378,12 @@ public boolean writeString(String string) throws SerialPortException { /** * Write String to port * + * @param string + * @param charsetName * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException + * @throws java.io.UnsupportedEncodingException * * @since 2.8.0 */ @@ -395,6 +395,7 @@ public boolean writeString(String string, String charsetName) throws SerialPortE /** * Write int value (in range from 0 to 255 (0x00 - 0xFF)) to port * + * @param singleInt * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -409,6 +410,7 @@ public boolean writeInt(int singleInt) throws SerialPortException { /** * Write int array (in range from 0 to 255 (0x00 - 0xFF)) to port * + * @param buffer * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -474,6 +476,7 @@ public String readHexString(int byteCount) throws SerialPortException { * Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF) * * @param byteCount count of bytes for reading + * @param separator * * @return byte array with "byteCount" length converted to Hexadecimal String * @@ -629,6 +632,7 @@ public String readHexString(int byteCount, int timeout) throws SerialPortExcepti * Read Hex string from port with setted separator (example if separator is "::": FF::0A::FF) * * @param byteCount count of bytes for reading + * @param separator * @param timeout timeout in milliseconds * * @return byte array with "byteCount" length converted to Hexadecimal String @@ -739,6 +743,7 @@ public String readHexString() throws SerialPortException { /** * Read all available bytes from port like a Hex String with setted separator * + * @param separator * @return If input buffer is empty null will be returned, else byte array with all data from port converted to Hex String * * @throws SerialPortException @@ -822,6 +827,7 @@ public int getOutputBufferBytesCount() throws SerialPortException { * Set flow control mode. For required mode use variables with prefix "FLOWCONTROL_". * Example of hardware flow control mode(RTS/CTS): setFlowControlMode(FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT); * + * @param mask * @return If the operation is successfully completed, the method returns true, otherwise false * * @throws SerialPortException @@ -905,12 +911,7 @@ public int[] getLinesStatus() throws SerialPortException { */ public boolean isCTS() throws SerialPortException { checkPortOpened("isCTS()"); - if(serialInterface.getLinesStatus(portHandle)[0] == 1){ - return true; - } - else { - return false; - } + return serialInterface.getLinesStatus(portHandle)[0] == 1; } /** @@ -922,12 +923,7 @@ public boolean isCTS() throws SerialPortException { */ public boolean isDSR() throws SerialPortException { checkPortOpened("isDSR()"); - if(serialInterface.getLinesStatus(portHandle)[1] == 1){ - return true; - } - else { - return false; - } + return serialInterface.getLinesStatus(portHandle)[1] == 1; } /** @@ -939,12 +935,7 @@ public boolean isDSR() throws SerialPortException { */ public boolean isRING() throws SerialPortException { checkPortOpened("isRING()"); - if(serialInterface.getLinesStatus(portHandle)[2] == 1){ - return true; - } - else { - return false; - } + return serialInterface.getLinesStatus(portHandle)[2] == 1; } /** @@ -956,12 +947,7 @@ public boolean isRING() throws SerialPortException { */ public boolean isRLSD() throws SerialPortException { checkPortOpened("isRLSD()"); - if(serialInterface.getLinesStatus(portHandle)[3] == 1){ - return true; - } - else { - return false; - } + return serialInterface.getLinesStatus(portHandle)[3] == 1; } /** @@ -970,6 +956,7 @@ public boolean isRLSD() throws SerialPortException { * be in charge for handling of occurred events. This method will independently * set the mask in "MASK_RXCHAR" state if it was not set beforehand * + * @param listener * @throws SerialPortException */ public void addEventListener(SerialPortEventListener listener) throws SerialPortException { @@ -982,6 +969,8 @@ public void addEventListener(SerialPortEventListener listener) throws SerialPort * charge for handling of occurred events. Also events mask shall be sent to * this method, to do it use variables with prefix "MASK_" for example "MASK_RXCHAR" * + * @param listener + * @param mask * @see #setEventsMask(int) setEventsMask(int mask) * * @throws SerialPortException @@ -1108,17 +1097,17 @@ private class EventThread extends Thread { public void run() { while(!threadTerminated){ int[][] eventArray = waitEvents(); - for(int i = 0; i < eventArray.length; i++){ - if(eventArray[i][0] > 0 && !threadTerminated){ - eventListener.serialEvent(new SerialPortEvent(portName, eventArray[i][0], eventArray[i][1])); + for (int[] event : eventArray) { + if (event[0] > 0 && !threadTerminated) { + eventListener.serialEvent(new SerialPortEvent(portName, event[0], event[1])); //FIXME /*if(methodErrorOccurred != null){ - try { - methodErrorOccurred.invoke(eventListener, new Object[]{new SerialPortException("port", "method", "exception")}); - } - catch (Exception ex) { - System.out.println(ex); - } + try { + methodErrorOccurred.invoke(eventListener, new Object[]{new SerialPortException("port", "method", "exception")}); + } + catch (Exception ex) { + System.out.println(ex); + } }*/ } } @@ -1160,9 +1149,9 @@ private class LinuxEventThread extends EventThread { //Need to get initial states public LinuxEventThread(){ int[][] eventArray = waitEvents(); - for(int i = 0; i < eventArray.length; i++){ - int eventType = eventArray[i][0]; - int eventValue = eventArray[i][1]; + for (int[] event : eventArray) { + int eventType = event[0]; + int eventValue = event[1]; switch(eventType){ case INTERRUPT_BREAK: interruptBreak = eventValue; @@ -1202,10 +1191,10 @@ public void run() { int mask = getLinuxMask(); boolean interruptTxChanged = false; int errorMask = 0; - for(int i = 0; i < eventArray.length; i++){ + for (int[] event : eventArray) { boolean sendEvent = false; - int eventType = eventArray[i][0]; - int eventValue = eventArray[i][1]; + int eventType = event[0]; + int eventValue = event[1]; if(eventType > 0 && !super.threadTerminated){ switch(eventType){ case INTERRUPT_BREAK: @@ -1284,10 +1273,10 @@ public void run() { sendEvent = true; } break; - /*case MASK_RXFLAG: + /*case MASK_RXFLAG: //Do nothing at this moment if(((mask & MASK_RXFLAG) == MASK_RXFLAG) && (eventValue > 0)){ - sendEvent = true; + sendEvent = true; } break;*/ case MASK_TXEMPTY: diff --git a/src/main/java/org/scream3r/jssc/SerialPortEvent.java b/src/main/java/org/scream3r/jssc/SerialPortEvent.java index badb83755..67fb9ddd6 100644 --- a/src/main/java/org/scream3r/jssc/SerialPortEvent.java +++ b/src/main/java/org/scream3r/jssc/SerialPortEvent.java @@ -30,9 +30,9 @@ */ public class SerialPortEvent { - private String portName; - private int eventType; - private int eventValue; + private final String portName; + private final int eventType; + private final int eventValue; public static final int RXCHAR = 1; public static final int RXFLAG = 2; @@ -52,6 +52,8 @@ public SerialPortEvent(String portName, int eventType, int eventValue){ /** * Getting port name which sent the event + * + * @return */ public String getPortName() { return portName; @@ -59,6 +61,8 @@ public String getPortName() { /** * Getting event type + * + * @return */ public int getEventType() { return eventType; @@ -77,116 +81,91 @@ public int getEventType() { *
BREAK - 0
*
RING - state of RING line (0 - OFF, 1 - ON)
*
ERR - mask of errors
+ * + * @return Return the event value. */ public int getEventValue() { return eventValue; } /** - * Method returns true if event of type "RXCHAR" is received and otherwise false + * Test for type RXCHAR + * + * @return Method returns true if event of type "RXCHAR" is received and otherwise false. */ public boolean isRXCHAR() { - if(eventType == RXCHAR){ - return true; - } - else { - return false; - } + return eventType == RXCHAR; } /** - * Method returns true if event of type "RXFLAG" is received and otherwise false + * Test for type RXFLAG + * + * @return Method returns true if event of type "RXFLAG" is received and otherwise false */ public boolean isRXFLAG() { - if(eventType == RXFLAG){ - return true; - } - else { - return false; - } + return eventType == RXFLAG; } /** - * Method returns true if event of type "TXEMPTY" is received and otherwise false + * Test for type TXEMPTY + * + * @return Method returns true if event of type "TXEMPTY" is received and otherwise false */ public boolean isTXEMPTY() { - if(eventType == TXEMPTY){ - return true; - } - else { - return false; - } + return eventType == TXEMPTY; } /** - * Method returns true if event of type "CTS" is received and otherwise false + * Test for type CTS + * + * @return Method returns true if event of type "CTS" is received and otherwise false */ public boolean isCTS() { - if(eventType == CTS){ - return true; - } - else { - return false; - } + return eventType == CTS; } /** - * Method returns true if event of type "DSR" is received and otherwise false + * Test for type DSR + * + * @return Method returns true if event of type "DSR" is received and otherwise false */ public boolean isDSR() { - if(eventType == DSR){ - return true; - } - else { - return false; - } + return eventType == DSR; } /** - * Method returns true if event of type "RLSD" is received and otherwise false + * Test for type RLSD + * + * @return Method returns true if event of type "RLSD" is received and otherwise false */ public boolean isRLSD() { - if(eventType == RLSD){ - return true; - } - else { - return false; - } + return eventType == RLSD; } /** - * Method returns true if event of type "BREAK" is received and otherwise false + * Test for type BREAK + * + * @return Method returns true if event of type "BREAK" is received and otherwise false */ public boolean isBREAK() { - if(eventType == BREAK){ - return true; - } - else { - return false; - } + return eventType == BREAK; } /** - * Method returns true if event of type "ERR" is received and otherwise false + * Test for type ERR + * + * @return Method returns true if event of type "ERR" is received and otherwise false */ public boolean isERR() { - if(eventType == ERR){ - return true; - } - else { - return false; - } + return eventType == ERR; } /** - * Method returns true if event of type "RING" is received and otherwise false + * Test for type RING + * + * @return Method returns true if event of type "RING" is received and otherwise false */ public boolean isRING() { - if(eventType == RING){ - return true; - } - else { - return false; - } + return eventType == RING; } } diff --git a/src/main/java/org/scream3r/jssc/SerialPortException.java b/src/main/java/org/scream3r/jssc/SerialPortException.java index 8b3ad647e..e773219fe 100644 --- a/src/main/java/org/scream3r/jssc/SerialPortException.java +++ b/src/main/java/org/scream3r/jssc/SerialPortException.java @@ -63,9 +63,9 @@ public class SerialPortException extends Exception { */ final public static String TYPE_INCORRECT_SERIAL_PORT = "Incorrect serial port"; - private String portName; - private String methodName; - private String exceptionType; + private final String portName; + private final String methodName; + private final String exceptionType; public SerialPortException(String portName, String methodName, String exceptionType){ super("Port name - " + portName + "; Method name - " + methodName + "; Exception type - " + exceptionType + "."); @@ -76,6 +76,8 @@ public SerialPortException(String portName, String methodName, String exceptionT /** * Getting port name during operation with which the exception was called + * + * @return Return the port name. */ public String getPortName(){ return portName; @@ -83,6 +85,8 @@ public String getPortName(){ /** * Getting method name during execution of which the exception was called + * + * @return Return the method name. */ public String getMethodName(){ return methodName; @@ -90,6 +94,8 @@ public String getMethodName(){ /** * Getting exception type + * + * @return Return the exception type. */ public String getExceptionType(){ return exceptionType; diff --git a/src/main/java/org/scream3r/jssc/SerialPortList.java b/src/main/java/org/scream3r/jssc/SerialPortList.java index 05094a3b4..a7b3d548d 100644 --- a/src/main/java/org/scream3r/jssc/SerialPortList.java +++ b/src/main/java/org/scream3r/jssc/SerialPortList.java @@ -308,7 +308,7 @@ private static String[] getWindowsPortNames(Pattern pattern, Comparator if(portNames == null){ return new String[]{}; } - TreeSet ports = new TreeSet(comparator); + TreeSet ports = new TreeSet<>(comparator); for(String portName : portNames){ if(pattern.matcher(portName).find()){ ports.add(portName); @@ -327,7 +327,7 @@ private static String[] getUnixBasedPortNames(String searchPath, Pattern pattern if(dir.exists() && dir.isDirectory()){ File[] files = dir.listFiles(); if(files.length > 0){ - TreeSet portsTree = new TreeSet(comparator); + TreeSet portsTree = new TreeSet<>(comparator); for(File file : files){ String fileName = file.getName(); if(!file.isDirectory() && !file.isFile() && pattern.matcher(fileName).find()){ diff --git a/src/main/java/org/scream3r/jssc/SerialPortTimeoutException.java b/src/main/java/org/scream3r/jssc/SerialPortTimeoutException.java index 5fb013b3d..87a0fe6a7 100644 --- a/src/main/java/org/scream3r/jssc/SerialPortTimeoutException.java +++ b/src/main/java/org/scream3r/jssc/SerialPortTimeoutException.java @@ -32,9 +32,9 @@ public class SerialPortTimeoutException extends Exception { final static long serialVersionUID = -1584357967321684324l; - private String portName; - private String methodName; - private int timeoutValue; + private final String portName; + private final String methodName; + private final int timeoutValue; public SerialPortTimeoutException(String portName, String methodName, int timeoutValue) { super("Port name - " + portName + "; Method name - " + methodName + "; Serial port operation timeout (" + timeoutValue + " ms)."); @@ -45,6 +45,8 @@ public SerialPortTimeoutException(String portName, String methodName, int timeou /** * Getting port name during operation with which the exception was called + * + * @return Return the port name. */ public String getPortName(){ return portName; @@ -52,6 +54,8 @@ public String getPortName(){ /** * Getting method name during execution of which the exception was called + * + * @return Return the method name. */ public String getMethodName(){ return methodName; @@ -59,6 +63,8 @@ public String getMethodName(){ /** * Getting timeout value in millisecond + * + * @return Return the timeout value in milliseconds. */ public int getTimeoutValue(){ return timeoutValue;