Better exception message to tell the user why it is not supported

This commit is contained in:
Norman Maurer 2014-01-30 07:00:53 +01:00
parent cd5103feb7
commit f7f808c7e0

View File

@ -83,9 +83,7 @@ public final class NioDatagramChannel
return newSocket(); return newSocket();
} }
if (PlatformDependent.javaVersion() < 7) { checkJavaVersion();
throw new UnsupportedOperationException();
}
try { try {
return DatagramChannel.open(ProtocolFamilyConverter.convert(ipFamily)); return DatagramChannel.open(ProtocolFamilyConverter.convert(ipFamily));
@ -94,6 +92,12 @@ public final class NioDatagramChannel
} }
} }
private static void checkJavaVersion() {
if (PlatformDependent.javaVersion() < 7) {
throw new UnsupportedOperationException("Only supported on java 7+.");
}
}
/** /**
* Create a new instance which will use the Operation Systems default {@link InternetProtocolFamily}. * Create a new instance which will use the Operation Systems default {@link InternetProtocolFamily}.
*/ */
@ -350,39 +354,39 @@ public final class NioDatagramChannel
public ChannelFuture joinGroup( public ChannelFuture joinGroup(
InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress multicastAddress, NetworkInterface networkInterface,
InetAddress source, ChannelPromise promise) { InetAddress source, ChannelPromise promise) {
if (PlatformDependent.javaVersion() >= 7) {
if (multicastAddress == null) {
throw new NullPointerException("multicastAddress");
}
if (networkInterface == null) { checkJavaVersion();
throw new NullPointerException("networkInterface");
}
try { if (multicastAddress == null) {
MembershipKey key; throw new NullPointerException("multicastAddress");
if (source == null) {
key = javaChannel().join(multicastAddress, networkInterface);
} else {
key = javaChannel().join(multicastAddress, networkInterface, source);
}
synchronized (this) {
List<MembershipKey> keys = memberships.get(multicastAddress);
if (keys == null) {
keys = new ArrayList<MembershipKey>();
memberships.put(multicastAddress, keys);
}
keys.add(key);
}
promise.setSuccess();
} catch (Throwable e) {
promise.setFailure(e);
}
} else {
throw new UnsupportedOperationException();
} }
if (networkInterface == null) {
throw new NullPointerException("networkInterface");
}
try {
MembershipKey key;
if (source == null) {
key = javaChannel().join(multicastAddress, networkInterface);
} else {
key = javaChannel().join(multicastAddress, networkInterface, source);
}
synchronized (this) {
List<MembershipKey> keys = memberships.get(multicastAddress);
if (keys == null) {
keys = new ArrayList<MembershipKey>();
memberships.put(multicastAddress, keys);
}
keys.add(key);
}
promise.setSuccess();
} catch (Throwable e) {
promise.setFailure(e);
}
return promise; return promise;
} }
@ -425,9 +429,8 @@ public final class NioDatagramChannel
public ChannelFuture leaveGroup( public ChannelFuture leaveGroup(
InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source, InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
ChannelPromise promise) { ChannelPromise promise) {
if (PlatformDependent.javaVersion() < 7) { checkJavaVersion();
throw new UnsupportedOperationException();
}
if (multicastAddress == null) { if (multicastAddress == null) {
throw new NullPointerException("multicastAddress"); throw new NullPointerException("multicastAddress");
} }
@ -479,36 +482,34 @@ public final class NioDatagramChannel
public ChannelFuture block( public ChannelFuture block(
InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress multicastAddress, NetworkInterface networkInterface,
InetAddress sourceToBlock, ChannelPromise promise) { InetAddress sourceToBlock, ChannelPromise promise) {
if (PlatformDependent.javaVersion() < 7) { checkJavaVersion();
throw new UnsupportedOperationException();
} else {
if (multicastAddress == null) {
throw new NullPointerException("multicastAddress");
}
if (sourceToBlock == null) {
throw new NullPointerException("sourceToBlock");
}
if (networkInterface == null) { if (multicastAddress == null) {
throw new NullPointerException("networkInterface"); throw new NullPointerException("multicastAddress");
} }
synchronized (this) { if (sourceToBlock == null) {
if (memberships != null) { throw new NullPointerException("sourceToBlock");
List<MembershipKey> keys = memberships.get(multicastAddress); }
for (MembershipKey key: keys) {
if (networkInterface.equals(key.networkInterface())) { if (networkInterface == null) {
try { throw new NullPointerException("networkInterface");
key.block(sourceToBlock); }
} catch (IOException e) { synchronized (this) {
promise.setFailure(e); if (memberships != null) {
} List<MembershipKey> keys = memberships.get(multicastAddress);
for (MembershipKey key: keys) {
if (networkInterface.equals(key.networkInterface())) {
try {
key.block(sourceToBlock);
} catch (IOException e) {
promise.setFailure(e);
} }
} }
} }
} }
promise.setSuccess();
return promise;
} }
promise.setSuccess();
return promise;
} }
/** /**