Skip to content

Commit

Permalink
Made IOBundle extend AutoCloseable
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Sep 5, 2019
1 parent 3c59bff commit 7ffeefd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/main/java/me/retrodaredevil/io/IOBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
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;

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;
Expand Down

0 comments on commit 7ffeefd

Please sign in to comment.