From 610ad369a16dfaf622c6f31adad5324bb24fadd8 Mon Sep 17 00:00:00 2001 From: hedgecrw85 Date: Wed, 8 Aug 2018 12:54:37 -0500 Subject: [PATCH] Create SerialPort exceptions to differentiate between IO exceptions --- .../com/fazecast/jSerialComm/SerialPort.java | 64 +++++++++++-------- .../jSerialComm/SerialPortIOException.java | 2 +- .../SerialPortTimeoutException.java | 2 +- .../fazecast/jSerialComm/SerialPortTest.java | 4 +- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 12405eb..5c9c986 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -2,7 +2,7 @@ * SerialPort.java * * Created on: Feb 25, 2012 - * Last Updated on: Jul 20, 2018 + * Last Updated on: Aug 08, 2018 * Author: Will Hedgecock * * Copyright (C) 2012-2018 Fazecast, Inc. @@ -42,7 +42,7 @@ import java.util.Date; * This class provides native access to serial ports and devices without requiring external libraries or tools. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 2.1.2 + * @version 2.2.0 * @see java.io.InputStream * @see java.io.OutputStream */ @@ -522,7 +522,7 @@ public final class SerialPort private final native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1 /** - * Returns the number of bytes available without blocking if {@link #readBytes} were to be called immediately + * Returns the number of bytes available without blocking if {@link #readBytes(byte[], long)} were to be called immediately * after this method returns. * * @return The number of bytes currently available to be read, or -1 if the port is not open. @@ -1189,27 +1189,41 @@ public final class SerialPort public SerialPortInputStream() {} @Override - public final int available() throws IOException + public final int available() throws SerialPortIOException { if (!isOpened) - throw new IOException("This port appears to have been shutdown or disconnected."); + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); return bytesAvailable(portHandle); } @Override - public final int read() throws IOException + public final int read() throws SerialPortIOException { + if (!isOpened) + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); return (readBytes(portHandle, byteBuffer, 1L, 0) < 0) ? -1 : ((int)byteBuffer[0] & 0xFF); } @Override - public final int read(byte[] b) throws NullPointerException, IOException + public final int read(byte[] b) throws NullPointerException, SerialPortIOException, SerialPortTimeoutException { - return readBytes(portHandle, b, b.length, 0); + // Perform error checking + if (b == null) + throw new NullPointerException("A null pointer was passed in for the read buffer."); + if (!isOpened) + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); + if (b.length == 0) + return 0; + + // Read from the serial port + int numRead = readBytes(portHandle, b, b.length, 0); + if (numRead == 0) + throw new SerialPortTimeoutException("The read operation timed out before any data was returned."); + return numRead; } @Override - public final int read(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, IOException + public final int read(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, SerialPortIOException, SerialPortTimeoutException { // Perform error checking if (b == null) @@ -1217,22 +1231,22 @@ public final class SerialPort if ((len < 0) || (off < 0) || (len > (b.length - off))) throw new IndexOutOfBoundsException("The specified read offset plus length extends past the end of the specified buffer."); if (!isOpened) - throw new IOException("This port appears to have been shutdown or disconnected."); - if (len == 0) + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); + if ((b.length == 0) || (len == 0)) return 0; - + // Read from the serial port int numRead = readBytes(portHandle, b, len, off); if (numRead == 0) - throw new IOException("The read operation timed out before any data was returned."); + throw new SerialPortTimeoutException("The read operation timed out before any data was returned."); return numRead; } @Override - public final long skip(long n) throws IOException + public final long skip(long n) throws SerialPortIOException { if (!isOpened) - throw new IOException("This port appears to have been shutdown or disconnected."); + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); byte[] buffer = new byte[(int)n]; return readBytes(portHandle, buffer, n, 0); } @@ -1246,26 +1260,26 @@ public final class SerialPort public SerialPortOutputStream() {} @Override - public final void write(int b) throws IOException + public final void write(int b) throws SerialPortIOException, SerialPortTimeoutException { if (!isOpened) - throw new IOException("This port appears to have been shutdown or disconnected."); + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); byteBuffer[0] = (byte)(b & 0xFF); int bytesWritten = writeBytes(portHandle, byteBuffer, 1L, 0); if (bytesWritten < 0) - throw new IOException("This port appears to have been shutdown or disconnected."); + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); else if (bytesWritten == 0) - throw new IOException("The write operation timed out before all data was written."); + throw new SerialPortTimeoutException("The write operation timed out before all data was written."); } @Override - public final void write(byte[] b) throws NullPointerException, IOException + public final void write(byte[] b) throws NullPointerException, SerialPortIOException, SerialPortTimeoutException { write(b, 0, b.length); } @Override - public final void write(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, IOException + public final void write(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, SerialPortIOException, SerialPortTimeoutException { // Perform error checking if (b == null) @@ -1273,16 +1287,16 @@ public final class SerialPort if ((len < 0) || (off < 0) || ((off + len) > b.length)) throw new IndexOutOfBoundsException("The specified write offset plus length extends past the end of the specified buffer."); if (!isOpened) - throw new IOException("This port appears to have been shutdown or disconnected."); + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); if (len == 0) return; - + // Write to the serial port int numWritten = writeBytes(portHandle, b, len, off); if (numWritten < 0) - throw new IOException("This port appears to have been shutdown or disconnected."); + throw new SerialPortIOException("This port appears to have been shutdown or disconnected."); else if (numWritten == 0) - throw new IOException("The write operation timed out before all data was written."); + throw new SerialPortTimeoutException("The write operation timed out before all data was written."); } } } diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortIOException.java b/src/main/java/com/fazecast/jSerialComm/SerialPortIOException.java index c1d8714..4c28eec 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortIOException.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortIOException.java @@ -32,7 +32,7 @@ import java.io.IOException; * * @author Will Hedgecock <will.hedgecock@fazecast.com> * @version 2.2.0 - * @see java.util.EventObject + * @see java.io.IOException */ public final class SerialPortIOException extends IOException { diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortTimeoutException.java b/src/main/java/com/fazecast/jSerialComm/SerialPortTimeoutException.java index 25d2505..1e15cee 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortTimeoutException.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortTimeoutException.java @@ -32,7 +32,7 @@ import java.io.IOException; * * @author Will Hedgecock <will.hedgecock@fazecast.com> * @version 2.2.0 - * @see java.util.EventObject + * @see java.io.IOException */ public final class SerialPortTimeoutException extends IOException { diff --git a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java index 25ed3dc..10384ce 100644 --- a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java +++ b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java @@ -212,7 +212,9 @@ public class SerialPortTest System.out.print((char)in.read()); in.close(); in2.close(); - } catch (Exception e) { e.printStackTrace(); } + } + catch (SerialPortIOException e) { e.printStackTrace(); } + catch (Exception e) { e.printStackTrace(); } } System.out.println("\n\nEntering Java-based InputStream in Scanner mode and reading 200 lines\n"); ubxPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);