RFR 9: MulticastSendReceiveTests.java fails with NumberFormatException
Chris Hegarty
chris.hegarty at oracle.com
Wed Mar 25 14:26:37 UTC 2015
On 25/03/15 12:36, Alan Bateman wrote:
> On 25/03/2015 12:16, Chris Hegarty wrote:
>> This is a trivial test only one-liner. The test should attempt to
>> reconstruct the String message with the same charset as is used to
>> convert the original message to bytes, UTF-8.
>>
> The fix looks okay and matches the sender. That said, it's possible that
> this is just interference on the network too, in which case the package
> should be ignored as is does already when it doesn't match the expected id.
Ah right. So, maybe the test should ignore data that cannot be parsed,
so then it can continue to try to read the expected id. The current code
cannot do this.
diff --git
a/test/java/nio/channels/DatagramChannel/MulticastSendReceiveTests.java
b/test/java/nio/channels/DatagramChannel/MulticastSendReceiveTests.java
--- a/test/java/nio/channels/DatagramChannel/MulticastSendReceiveTests.java
+++ b/test/java/nio/channels/DatagramChannel/MulticastSendReceiveTests.java
@@ -109,7 +109,14 @@
buf.flip();
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
- int receivedId = Integer.parseInt(new String(bytes));
+ int receivedId = -1;
+ try {
+ String s = new String(bytes, "UTF-8");
+ receivedId = Integer.parseInt(s);
+ } catch (NumberFormatException x) {
+ System.out.format("Caught %s)", x.getMessage());
+ System.out.format("Received message (s=%s)", s);
+ }
System.out.format("Received message from %s (id=0x%x)\n",
sender, receivedId);
-Chris.
> -Alan
More information about the nio-dev
mailing list