io.netty.channel.socket.nio.NioChannelOption Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
/*
* Copyright 2018 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.socket.nio;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelOption;
import io.netty.util.internal.SuppressJava6Requirement;
import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.channels.ServerSocketChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Provides {@link ChannelOption} over a given {@link java.net.SocketOption} which is then passed through the underlying
* {@link java.nio.channels.NetworkChannel}.
*/
@SuppressJava6Requirement(reason = "Usage explicit by the user")
public final class NioChannelOption extends ChannelOption {
private final java.net.SocketOption option;
@SuppressWarnings("deprecation")
private NioChannelOption(java.net.SocketOption option) {
super(option.name());
this.option = option;
}
/**
* Returns a {@link ChannelOption} for the given {@link java.net.SocketOption}.
*/
public static ChannelOption of(java.net.SocketOption option) {
return new NioChannelOption(option);
}
// It's important to not use java.nio.channels.NetworkChannel as otherwise the classes that sometimes call this
// method may not be used on Java 6, as method linking can happen eagerly even if this method was not actually
// called at runtime.
//
// See https://github.com/netty/netty/issues/8166
// Internal helper methods to remove code duplication between Nio*Channel implementations.
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
static boolean setOption(Channel jdkChannel, NioChannelOption option, T value) {
java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
if (!channel.supportedOptions().contains(option.option)) {
return false;
}
if (channel instanceof ServerSocketChannel && option.option == java.net.StandardSocketOptions.IP_TOS) {
// Skip IP_TOS as a workaround for a JDK bug:
// See https://mail.openjdk.java.net/pipermail/nio-dev/2018-August/005365.html
return false;
}
try {
channel.setOption(option.option, value);
return true;
} catch (IOException e) {
throw new ChannelException(e);
}
}
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
static T getOption(Channel jdkChannel, NioChannelOption option) {
java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
if (!channel.supportedOptions().contains(option.option)) {
return null;
}
if (channel instanceof ServerSocketChannel && option.option == java.net.StandardSocketOptions.IP_TOS) {
// Skip IP_TOS as a workaround for a JDK bug:
// See https://mail.openjdk.java.net/pipermail/nio-dev/2018-August/005365.html
return null;
}
try {
return channel.getOption(option.option);
} catch (IOException e) {
throw new ChannelException(e);
}
}
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
@SuppressWarnings("unchecked")
static ChannelOption[] getOptions(Channel jdkChannel) {
java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
Set> supportedOpts = channel.supportedOptions();
if (channel instanceof ServerSocketChannel) {
List> extraOpts = new ArrayList>(supportedOpts.size());
for (java.net.SocketOption> opt : supportedOpts) {
if (opt == java.net.StandardSocketOptions.IP_TOS) {
// Skip IP_TOS as a workaround for a JDK bug:
// See https://mail.openjdk.java.net/pipermail/nio-dev/2018-August/005365.html
continue;
}
extraOpts.add(new NioChannelOption(opt));
}
return extraOpts.toArray(new ChannelOption[0]);
} else {
ChannelOption>[] extraOpts = new ChannelOption[supportedOpts.size()];
int i = 0;
for (java.net.SocketOption> opt : supportedOpts) {
extraOpts[i++] = new NioChannelOption(opt);
}
return extraOpts;
}
}
}