The `receive` method of the datagram channel socket adapter inadvertently
restricts the usable portion of the DatagramPacket's byte array. The most
obvious case is where a DatagramPacket is reused by several receive invocations,
where subsequently received packets contain larger data payloads. The maximum
usable portion of the packet's byte array is restricted to the smallest received
payload.
The following jshell snippet demonstrates the issue. First the snippet operates
with a java.net.DatagramSocket, the second with a socket adapter.
jshell> var ds1 = new DatagramSocket()
ds1 ==> java.net.DatagramSocket@5ce65a89
jshell> var ds2 = new DatagramSocket()
ds2 ==> java.net.DatagramSocket@1a86f2f1
jshell> ds2.getLocalPort()
$6 ==> 65145
jshell> ds1.send(new DatagramPacket("hello".getBytes(), 0, 5, new InetSocketAddress("localhost", 65145)))
jshell> var packet = new DatagramPacket(new byte[100], 0, 100)
packet ==> java.net.DatagramPacket@1de0aca6 <<<< This packet will be reused when receiving
jshell> ds2.receive(packet)
jshell> new String(packet.getData(), packet.getOffset(), packet.getLength())
$10 ==> "hello"
jshell> ds1.send(new DatagramPacket("Bob Cratchit".getBytes(), 0, 12, new InetSocketAddress("localhost", 65145)))
jshell> ds2.receive(packet)
jshell> new String(packet.getData(), packet.getOffset(), packet.getLength())
$13 ==> "Bob Cratchit"
---
jshell> var ds1 = new DatagramSocket()
ds1 ==> java.net.DatagramSocket@5ce65a89
jshell> var ds2 = DatagramChannel.open().socket()
ds2 ==> sun.nio.ch.DatagramSocketAdaptor@443b7951
jshell> ds2.getLocalPort()
$6 ==> 0
jshell> var ds2 = DatagramChannel.open().bind(null).socket()
ds2 ==> sun.nio.ch.DatagramSocketAdaptor@69663380
jshell> ds2.getLocalPort()
$8 ==> 58305
jshell> ds1.send(new DatagramPacket("hello".getBytes(), 0, 5, new InetSocketAddress("localhost", 58305)))
jshell> var packet = new DatagramPacket(new byte[100], 0, 100)
packet ==> java.net.DatagramPacket@736e9adb
jshell> ds2.receive(packet)
jshell> new String(packet.getData(), packet.getOffset(), packet.getLength())
$12 ==> "hello"
jshell> ds1.send(new DatagramPacket("Bob Cratchit".getBytes(), 0, 12, new InetSocketAddress("localhost", 58305)))
jshell> ds2.receive(packet)
jshell> new String(packet.getData(), packet.getOffset(), packet.getLength())
$15 ==> "Bob C"