diff --git a/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java b/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java index d402f6db0..15b6ab73a 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java @@ -19,6 +19,8 @@ package org.jivesoftware.smack; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; @@ -42,6 +44,8 @@ import org.jivesoftware.smack.packet.XMPPError; */ public class PacketCollector { + private static final Logger LOGGER = Logger.getLogger(PacketCollector.class.getName()); + private PacketFilter packetFilter; private ArrayBlockingQueue resultQueue; private XMPPConnection connection; @@ -114,12 +118,16 @@ public class PacketCollector { * @return the next available packet. */ public Packet nextResultBlockForever() { - try { - return resultQueue.take(); - } - catch (InterruptedException e) { - throw new RuntimeException(e); + Packet res = null; + while (res == null) { + try { + res = resultQueue.take(); + } catch (InterruptedException e) { + LOGGER.log(Level.FINE, + "nextResultBlockForever was interrupted", e); + } } + return res; } /** @@ -139,13 +147,19 @@ public class PacketCollector { * * @return the next available packet. */ - public Packet nextResult(long timeout) { - try { - return resultQueue.poll(timeout, TimeUnit.MILLISECONDS); - } - catch (InterruptedException e) { - throw new RuntimeException(e); - } + public Packet nextResult(final long timeout) { + Packet res = null; + long remainingWait = timeout; + final long waitStart = System.currentTimeMillis(); + while (res == null && remainingWait > 0) { + try { + res = resultQueue.poll(remainingWait, TimeUnit.MILLISECONDS); + remainingWait = timeout - (System.currentTimeMillis() - waitStart); + } catch (InterruptedException e) { + LOGGER.log(Level.FINE, "nextResult was interrupted", e); + } + } + return res; } /**