Use NIO methods when using Java7+ in the NIO transport

Motivation:

We use often javachannel().socket().* in NIO as these methods exists in java6. The problem is that these will throw often very general Exceptions (Like SocketException) while it is more expected to throw the Exceptions listed in the nio interfaces. When possible we should use the new methods available in java7+ which throw the correct exceptions.

Modifications:

Check for java version and depending on it using the socket or the javachannel.

Result:

Throw expected Exceptions.
This commit is contained in:
Norman Maurer 2016-08-17 14:23:12 +02:00
parent 8ce7e73e78
commit 43626ac6ea
4 changed files with 50 additions and 11 deletions

View File

@ -703,9 +703,11 @@
<ignore>java.util.zip.Deflater</ignore>
<!-- Used for NIO UDP multicast -->
<!-- Used for NIO -->
<ignore>java.nio.channels.DatagramChannel</ignore>
<ignore>java.nio.channels.MembershipKey</ignore>
<ignore>java.nio.channels.ServerSocketChannel</ignore>
<ignore>java.nio.channels.SocketChannel</ignore>
<ignore>java.net.StandardProtocolFamily</ignore>
<ignore>java.nio.channels.spi.SelectorProvider</ignore>

View File

@ -188,14 +188,22 @@ public final class NioDatagramChannel
@Override
protected void doBind(SocketAddress localAddress) throws Exception {
doBind0(localAddress);
}
private void doBind0(SocketAddress localAddress) throws Exception {
if (PlatformDependent.javaVersion() >= 7) {
javaChannel().bind(localAddress);
} else {
javaChannel().socket().bind(localAddress);
}
}
@Override
protected boolean doConnect(SocketAddress remoteAddress,
SocketAddress localAddress) throws Exception {
if (localAddress != null) {
javaChannel().socket().bind(localAddress);
doBind0(localAddress);
}
boolean success = false;

View File

@ -21,6 +21,7 @@ import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.nio.AbstractNioMessageChannel;
import io.netty.channel.socket.DefaultServerSocketChannelConfig;
import io.netty.channel.socket.ServerSocketChannelConfig;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
@ -122,8 +123,12 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel
@Override
protected void doBind(SocketAddress localAddress) throws Exception {
if (PlatformDependent.javaVersion() >= 7) {
javaChannel().bind(localAddress, config.getBacklog());
} else {
javaChannel().socket().bind(localAddress, config.getBacklog());
}
}
@Override
protected void doClose() throws Exception {

View File

@ -30,6 +30,7 @@ import io.netty.channel.socket.DefaultSocketChannelConfig;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannelConfig;
import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
@ -240,32 +241,47 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
private void shutdownOutput0(final ChannelPromise promise) {
try {
javaChannel().socket().shutdownOutput();
shutdownOutput0();
promise.setSuccess();
} catch (Throwable t) {
promise.setFailure(t);
}
}
private void shutdownOutput0() throws Exception {
if (PlatformDependent.javaVersion() >= 7) {
javaChannel().shutdownOutput();
} else {
javaChannel().socket().shutdownOutput();
}
}
private void shutdownInput0(final ChannelPromise promise) {
try {
javaChannel().socket().shutdownInput();
shutdownInput0();
promise.setSuccess();
} catch (Throwable t) {
promise.setFailure(t);
}
}
private void shutdownInput0() throws Exception {
if (PlatformDependent.javaVersion() >= 7) {
javaChannel().shutdownInput();
} else {
javaChannel().socket().shutdownInput();
}
}
private void shutdown0(final ChannelPromise promise) {
Socket socket = javaChannel().socket();
Throwable cause = null;
try {
socket.shutdownOutput();
shutdownOutput0();
} catch (Throwable t) {
cause = t;
}
try {
socket.shutdownInput();
shutdownInput0();
} catch (Throwable t) {
if (cause == null) {
promise.setFailure(t);
@ -294,13 +310,21 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
@Override
protected void doBind(SocketAddress localAddress) throws Exception {
doBind0(localAddress);
}
private void doBind0(SocketAddress localAddress) throws Exception {
if (PlatformDependent.javaVersion() >= 7) {
javaChannel().bind(localAddress);
} else {
javaChannel().socket().bind(localAddress);
}
}
@Override
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
if (localAddress != null) {
javaChannel().socket().bind(localAddress);
doBind0(localAddress);
}
boolean success = false;