8220585: Incorrect code in MulticastSocket sample code
Summary: Sample code shows that String.length() can be passed to a DatagramPacket along with String.getBytes(), but is incorrect for non-ASCII Strings.
Reviewed-by: martin, chegar
Contributed-by: aeubanks@google.com
--- a/src/java.base/share/classes/java/net/MulticastSocket.java Wed Mar 13 17:32:17 2019 +0100
+++ b/src/java.base/share/classes/java/net/MulticastSocket.java Wed Mar 13 09:12:38 2019 -0700
@@ -52,8 +52,9 @@
* InetAddress group = InetAddress.getByName("228.5.6.7");
* MulticastSocket s = new MulticastSocket(6789);
* s.joinGroup(group);
- * DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
- * group, 6789);
+ * byte[] msgBytes = msg.getBytes(StandardCharsets.UTF_8);
+ * DatagramPacket hi = new DatagramPacket(msgBytes, msgBytes.length,
+ * group, 6789);
* s.send(hi);
* // get their responses!
* byte[] buf = new byte[1000];