From 7ffeefdb241345c0c27953132e11ea0d1dbac4a4 Mon Sep 17 00:00:00 2001 From: Joshua Shannon Date: Thu, 5 Sep 2019 14:05:14 -0500 Subject: [PATCH] Made IOBundle extend AutoCloseable --- src/main/java/me/retrodaredevil/io/IOBundle.java | 13 ++++++++++++- .../retrodaredevil/io/serial/JSerialIOBundle.java | 5 +---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/retrodaredevil/io/IOBundle.java b/src/main/java/me/retrodaredevil/io/IOBundle.java index ab8cbc7..fef768c 100644 --- a/src/main/java/me/retrodaredevil/io/IOBundle.java +++ b/src/main/java/me/retrodaredevil/io/IOBundle.java @@ -9,7 +9,18 @@ * It is expected that throughout the lifetime of this object that calls to both {@link #getInputStream()} and {@link #getOutputStream()} * will return the same instance each time it is called. */ -public interface IOBundle { +public interface IOBundle extends AutoCloseable { InputStream getInputStream(); OutputStream getOutputStream(); + + /** + * This should be overridden by subclasses to close the input and output streams + * + * @throws Exception If this cannot be closed + */ + @Override + default void close() throws Exception { + getInputStream().close(); + getOutputStream().close(); + } } diff --git a/src/main/java/me/retrodaredevil/io/serial/JSerialIOBundle.java b/src/main/java/me/retrodaredevil/io/serial/JSerialIOBundle.java index 122b95e..ffc0f50 100644 --- a/src/main/java/me/retrodaredevil/io/serial/JSerialIOBundle.java +++ b/src/main/java/me/retrodaredevil/io/serial/JSerialIOBundle.java @@ -7,7 +7,7 @@ import java.io.InputStream; import java.io.OutputStream; -public class JSerialIOBundle implements IOBundle, AutoCloseable { +public class JSerialIOBundle implements IOBundle { private final InputStream inputStream; private final OutputStream outputStream; private final SerialPort serialPort; @@ -15,9 +15,6 @@ public class JSerialIOBundle implements IOBundle, AutoCloseable { public JSerialIOBundle(SerialPort serialPort, SerialConfig serialConfig){ this.serialPort = serialPort; serialPort.openPort(1000); -// serialPort.setComPortParameters(19200, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); -// serialPort.setDTR(); -// serialPort.clearRTS(); final int stopBits; switch(serialConfig.getStopBits()){ case ONE: stopBits = SerialPort.ONE_STOP_BIT; break;