changeset 54104:954399cb6959

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
author aeubanks
date Wed, 13 Mar 2019 09:12:38 -0700
parents fab8496eb541
children d58599c7cdef
files src/java.base/share/classes/java/net/MulticastSocket.java
diffstat 1 files changed, 3 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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];