diff --git a/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/DanfossAirUnitCommunicationController.java b/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/DanfossAirUnitCommunicationController.java index f607e7f83b014..6022ebb35e64d 100644 --- a/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/DanfossAirUnitCommunicationController.java +++ b/bundles/org.openhab.binding.danfossairunit/src/main/java/org/openhab/binding/danfossairunit/internal/DanfossAirUnitCommunicationController.java @@ -53,9 +53,10 @@ public synchronized void connect() throws IOException { if (connected) { return; } - socket = new Socket(inetAddr, port); - oStream = socket.getOutputStream(); - iStream = socket.getInputStream(); + Socket newSocket = new Socket(inetAddr, port); + oStream = newSocket.getOutputStream(); + iStream = newSocket.getInputStream(); + socket = newSocket; connected = true; } @@ -64,8 +65,9 @@ public synchronized void disconnect() { return; } try { - if (socket != null) { - socket.close(); + Socket connectedSocket = socket; + if (connectedSocket != null) { + connectedSocket.close(); } } catch (IOException ioe) { logger.debug("Connection to air unit could not be closed gracefully. {}", ioe.getMessage()); @@ -98,21 +100,23 @@ public synchronized byte[] sendRobustRequest(byte[] operation, byte[] register, } private synchronized byte[] sendRequestInternal(byte[] request) throws IOException { + OutputStream localOutputStream = oStream; - if (oStream == null) { + if (localOutputStream == null) { throw new IOException( String.format("Output stream is null while sending request: %s", Arrays.toString(request))); } - oStream.write(request); - oStream.flush(); + localOutputStream.write(request); + localOutputStream.flush(); byte[] result = new byte[63]; - if (iStream == null) { + InputStream localInputStream = iStream; + if (localInputStream == null) { throw new IOException( String.format("Input stream is null while sending request: %s", Arrays.toString(request))); } // noinspection ResultOfMethodCallIgnored - iStream.read(result, 0, 63); + localInputStream.read(result, 0, 63); return result; }