From 98c4e9056ffc3d4e3960995c3747a5f307c137dd Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 4 Jul 2019 10:12:08 +0200 Subject: [PATCH] use helper method to close socket --- .../siacs/conversations/parser/MessageParser.java | 2 +- .../conversations/persistance/FileBackend.java | 4 ++-- .../java/eu/siacs/conversations/xml/XmlReader.java | 10 ++++++++-- .../eu/siacs/conversations/xmpp/XmppConnection.java | 13 +++---------- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index c2347d454..7227f2c5b 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -610,7 +610,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece } else { serverMsgIdUpdated = false; } - Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + Boolean.toString(serverMsgIdUpdated)); + Log.d(Config.LOGTAG, "skipping duplicate message with " + message.getCounterpart() + ". serverMsgIdUpdated=" + serverMsgIdUpdated); return; } } diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index b6f5dc518..8c77bdad4 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -341,7 +341,7 @@ public class FileBackend { } } - public static void close(Closeable stream) { + public static void close(final Closeable stream) { if (stream != null) { try { stream.close(); @@ -350,7 +350,7 @@ public class FileBackend { } } - public static void close(Socket socket) { + public static void close(final Socket socket) { if (socket != null) { try { socket.close(); diff --git a/src/main/java/eu/siacs/conversations/xml/XmlReader.java b/src/main/java/eu/siacs/conversations/xml/XmlReader.java index ef7b73c77..1540e5f61 100644 --- a/src/main/java/eu/siacs/conversations/xml/XmlReader.java +++ b/src/main/java/eu/siacs/conversations/xml/XmlReader.java @@ -6,14 +6,15 @@ import android.util.Xml; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import eu.siacs.conversations.Config; -public class XmlReader { - private XmlPullParser parser; +public class XmlReader implements Closeable { + private final XmlPullParser parser; private InputStream is; public XmlReader() { @@ -48,6 +49,11 @@ public class XmlReader { } } + @Override + public void close() { + this.is = null; + } + public Tag readTag() throws IOException { try { while (this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) { diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index a57f0ae63..159fa4f2b 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1460,15 +1460,8 @@ public class XmppConnection implements Runnable { } private void forceCloseSocket() { - if (socket != null) { - try { - socket.close(); - } catch (IOException e) { - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": io exception " + e.getMessage() + " during force close"); - } - } else { - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": socket was null during force close"); - } + FileBackend.close(this.socket); + FileBackend.close(this.tagReader); } public void interrupt() { @@ -1479,7 +1472,7 @@ public class XmppConnection implements Runnable { public void disconnect(final boolean force) { interrupt(); - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": disconnecting force=" + Boolean.toString(force)); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": disconnecting force=" + force); if (force) { forceCloseSocket(); } else {