8220585: Incorrect code in MulticastSocket sample code
authoraeubanks
Wed, 13 Mar 2019 09:12:38 -0700
changeset 54104 954399cb6959
parent 54102 fab8496eb541
child 54105 d58599c7cdef
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
src/java.base/share/classes/java/net/MulticastSocket.java
--- 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];