Correct fail write with NotYetConnectedException when OioDatagramChannel is not connected yet.

Motivation:

NioDatagramChannel fails a write with NotYetConnectedException when the DatagramChannel was not yet connected and a ByteBuf is written. The same should be done for OioDatagramChannel as well.

Modifications:

Make OioDatagramChannel consistent with NioDatagramChannel

Result:

Correct and consistent implementations of DatagramChannel
This commit is contained in:
Norman Maurer 2017-01-09 22:04:18 +01:00
parent 66b9be3a46
commit 8a5e42ad2e

View File

@ -44,6 +44,7 @@ import java.net.NetworkInterface;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.nio.channels.NotYetConnectedException;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -253,17 +254,23 @@ public class OioDatagramChannel extends AbstractOioMessageChannel
} }
final int length = data.readableBytes(); final int length = data.readableBytes();
if (remoteAddress != null) {
tmpPacket.setSocketAddress(remoteAddress);
}
if (data.hasArray()) {
tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
} else {
byte[] tmp = new byte[length];
data.getBytes(data.readerIndex(), tmp);
tmpPacket.setData(tmp);
}
try { try {
if (remoteAddress != null) {
tmpPacket.setSocketAddress(remoteAddress);
} else {
if (!isConnected()) {
// If not connected we should throw a NotYetConnectedException() to be consistent with
// NioDatagramChannel
throw new NotYetConnectedException();
}
}
if (data.hasArray()) {
tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
} else {
byte[] tmp = new byte[length];
data.getBytes(data.readerIndex(), tmp);
tmpPacket.setData(tmp);
}
socket.send(tmpPacket); socket.send(tmpPacket);
in.remove(); in.remove();
} catch (IOException e) { } catch (IOException e) {