DatagramPacket.setData has the following definition:
public synchronized void setData(byte[] buf) {
if (buf == null) {
throw new NullPointerException("null packet buffer");
}
this.buf = buf;
if (length > buf.length) {
setLength(buf.length);
}
}
This is strange because it leaves offset unchanged, and only changes
length if buf.length is less than length. This is incosistent with
setData(byte[] buf, int offset, int length)
which sets both offset and length. The intuitive behavior for
setData(buf) would be that it be equivalent to
setData(buf, 0, buf.length), but it is not.
A minor doc error is that
public synchronized void setData(byte[] buf, int offset, int length) {
says it is @since JDK1.1. This is not true, because this overload was
introduced in JDK1.2.
public synchronized void setData(byte[] buf) {
if (buf == null) {
throw new NullPointerException("null packet buffer");
}
this.buf = buf;
if (length > buf.length) {
setLength(buf.length);
}
}
This is strange because it leaves offset unchanged, and only changes
length if buf.length is less than length. This is incosistent with
setData(byte[] buf, int offset, int length)
which sets both offset and length. The intuitive behavior for
setData(buf) would be that it be equivalent to
setData(buf, 0, buf.length), but it is not.
A minor doc error is that
public synchronized void setData(byte[] buf, int offset, int length) {
says it is @since JDK1.1. This is not true, because this overload was
introduced in JDK1.2.
- relates to
-
JDK-4261082 DatagramPacket does not uniformly enforce offset+length constraint
-
- Resolved
-