diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java index df783618f9..05bcbcd6f3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java @@ -61,6 +61,7 @@ * automatically if one of them is closed. * * @author Henning Staib + * @author Eng Chong Meng */ public class InBandBytestreamSession implements BytestreamSession { @@ -93,6 +94,9 @@ public class InBandBytestreamSession implements BytestreamSession { /* flag to indicate if session is closed */ private boolean isClosed = false; + /* flag to indicate if session is already closed by peer */ + private volatile boolean closedByPeer = false; + /** * Constructor. * @@ -188,6 +192,7 @@ protected void closeByPeer(Close closeRequest) throws NotConnectedException, Int this.inputStream.closeInternal(); this.inputStream.cleanup(); this.outputStream.closeInternal(false); + this.closedByPeer = true; // acknowledge close request IQ confirmClose = IQ.createResultIQ(closeRequest); @@ -224,18 +229,19 @@ protected synchronized void closeByLocal(boolean in) throws IOException { if (this.inputStream.isClosed && this.outputStream.isClosed) { this.isClosed = true; - // send close request - Close close = new Close(this.byteStreamRequest.getSessionID()); - close.setTo(this.remoteJID); - try { - connection.createStanzaCollectorAndSend(close).nextResultOrThrow(); - } - catch (Exception e) { - // Sadly we are unable to use the IOException(Throwable) constructor because this - // constructor is only supported from Android API 9 on. - IOException ioException = new IOException(); - ioException.initCause(e); - throw ioException; + // send close stream request if not already closed by peer + if (!closedByPeer) { + Close close = new Close(this.byteStreamRequest.getSessionID()); + close.setTo(this.remoteJID); + try { + connection.createStanzaCollectorAndSend(close).nextResultOrThrow(); + } catch (Exception e) { + // Sadly we are unable to use the IOException(Throwable) constructor because this + // constructor is only supported from Android API 9 on. + IOException ioException = new IOException(); + ioException.initCause(e); + throw ioException; + } } this.inputStream.cleanup();