DatagramPacket - getData 总是 return 传递的相同缓冲区吗?

DatagramPacket - will getData always return the same buffer which is passed?

byte [] r = new byte[4096];
DatagramPacket dpr = new DatagramPacket(r, r.length);
sock.receive(dpr);

receive 之后,dpr.getData()r 会一直一样吗?

例如:我可以直接使用字节数组r还是需要调用getData()重新获取缓冲区?

测试它,表明它是一样的,但这总是有保证吗?

byte [] r = new byte[4096];
DatagramPacket dpr = new DatagramPacket(r, r.length);
sock.receive(r);

应该是sock.receive(dpr);

After the receive, will dpr.getData() & r always be the same?

是的。 r 作为 'the buffer for holding the incoming datagram' 提供给构造函数,getData() 'returns the buffer used to receive or send data'.

i.e. can I directly use the byte array r or do I need to call getData() to retrieve the buffer again?

你可以使用字节数组,但为什么呢?像其他人一样使用 getData(),不要忘记也使用 getOffset()getLength(),而不是假设数据报填充了字节数组:例如,System.out.println(new String(datagram.getData(), datagram.getOffset(), datagram.getLength()));