Part of bug 11618: allow timeout when fetching messages from Nokia TCF.
--- a/connectivity/com.nokia.tcf/src/com/nokia/tcf/api/ITCMessageInputStream.java Thu Sep 16 10:15:59 2010 -0500
+++ b/connectivity/com.nokia.tcf/src/com/nokia/tcf/api/ITCMessageInputStream.java Fri Sep 17 11:13:41 2010 -0500
@@ -20,11 +20,14 @@
package com.nokia.tcf.api;
import java.io.IOException;
+import java.util.concurrent.TimeoutException;
/**
* This interface is used to access various features of the Message Input Stream. The implementation is created
* after a successful connection is established. To get a reference to this stream, use the
* {@link #ITCAPIConnection.getInputStream} method.
+ * @noimplement
+ * @noextend
*/
public interface ITCMessageInputStream extends java.io.Closeable {
@@ -105,4 +108,21 @@
*/
public byte[] readBytes(int inNumberMessages, int[] outNumberMessages) throws IOException;
+ /**
+ * Return messages from input stream. This call is blocking until at least 1 message exists. Number of
+ * messages returned may be less than requested. It is up to the caller to parse the byte array
+ * for individual messages. Only whole messages are returned in the byte stream.
+ *
+ * @param inNumberMessages - maximum number of messages to return. If this is zero, then all messages
+ * currently in the buffer are returned that will fit into a 2MB byte[] array.
+ * @param timeoutMs - timeout in milliseconds (values <= 0 mean wait forever)
+ *
+ * @return byte[] - byte array
+ *
+ * @throws IOException - if an IO error occurs
+ * @throws TimeoutException - if no messages are received in the given time frame
+ */
+ public byte[] readBytes(int inNumberMessages, long timeoutMs) throws IOException, TimeoutException;
+
+
}
--- a/connectivity/com.nokia.tcf/src/com/nokia/tcf/impl/TCMessageInputStream.java Thu Sep 16 10:15:59 2010 -0500
+++ b/connectivity/com.nokia.tcf/src/com/nokia/tcf/impl/TCMessageInputStream.java Fri Sep 17 11:13:41 2010 -0500
@@ -282,7 +282,7 @@
}
}
}
- public byte[] readBytes(int inNumberMessages) throws IOException {
+ public byte[] readBytes(int inNumberMessages, long timeoutMs) throws IOException {
// if not open throw exception
if (!this.isOpen) {
// stream not open
@@ -296,6 +296,7 @@
long[] outNumberMessagesRead = new long[1];
outNumberMessagesRead[0] = 0;
long[] outNumberBytesRead = new long[1];
+ long deadline = timeoutMs > 0 ? System.currentTimeMillis() + timeoutMs : Long.MAX_VALUE;
do {
long ret = api.nativeReadInputStream(this.clientId, inNumberMessages, outNumberMessagesRead, outNumberBytesRead, MAX_BYTES, messageData);
if (ret != TCErrorConstants.TCAPI_ERR_NONE) {
@@ -312,7 +313,9 @@
break;
}
}
- } while (outNumberMessagesRead[0] == 0);
+ if (timeoutMs != 0)
+ timeoutMs -= blockingTime;
+ } while (outNumberMessagesRead[0] == 0 && System.currentTimeMillis() < deadline);
// return how many messages actually processed
byte[] newMessageData = new byte[(int)outNumberBytesRead[0]];
@@ -321,6 +324,11 @@
}
return newMessageData;
}
+
+ public byte[] readBytes(int inNumberMessages) throws IOException {
+ return readBytes(inNumberMessages, 0);
+ }
+
public byte[] readBytes(int inNumberMessages, int[] outNumberMessages) throws IOException {
outNumberMessages[0] = 0;