1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.socket.nio;
17
18 import io.netty.channel.ChannelException;
19 import io.netty.channel.ChannelOption;
20 import io.netty.util.internal.SuppressJava6Requirement;
21
22 import java.io.IOException;
23 import java.nio.channels.Channel;
24 import java.nio.channels.ServerSocketChannel;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Set;
28
29
30
31
32
33 @SuppressJava6Requirement(reason = "Usage explicit by the user")
34 public final class NioChannelOption<T> extends ChannelOption<T> {
35
36 private final java.net.SocketOption<T> option;
37
38 @SuppressWarnings("deprecation")
39 private NioChannelOption(java.net.SocketOption<T> option) {
40 super(option.name());
41 this.option = option;
42 }
43
44
45
46
47 public static <T> ChannelOption<T> of(java.net.SocketOption<T> option) {
48 return new NioChannelOption<T>(option);
49 }
50
51
52
53
54
55
56
57
58 @SuppressJava6Requirement(reason = "Usage guarded by java version check")
59 static <T> boolean setOption(Channel jdkChannel, NioChannelOption<T> option, T value) {
60 java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
61 if (!channel.supportedOptions().contains(option.option)) {
62 return false;
63 }
64 if (channel instanceof ServerSocketChannel && option.option == java.net.StandardSocketOptions.IP_TOS) {
65
66
67 return false;
68 }
69 try {
70 channel.setOption(option.option, value);
71 return true;
72 } catch (IOException e) {
73 throw new ChannelException(e);
74 }
75 }
76
77 @SuppressJava6Requirement(reason = "Usage guarded by java version check")
78 static <T> T getOption(Channel jdkChannel, NioChannelOption<T> option) {
79 java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
80
81 if (!channel.supportedOptions().contains(option.option)) {
82 return null;
83 }
84 if (channel instanceof ServerSocketChannel && option.option == java.net.StandardSocketOptions.IP_TOS) {
85
86
87 return null;
88 }
89 try {
90 return channel.getOption(option.option);
91 } catch (IOException e) {
92 throw new ChannelException(e);
93 }
94 }
95
96 @SuppressJava6Requirement(reason = "Usage guarded by java version check")
97 @SuppressWarnings("unchecked")
98 static ChannelOption[] getOptions(Channel jdkChannel) {
99 java.nio.channels.NetworkChannel channel = (java.nio.channels.NetworkChannel) jdkChannel;
100 Set<java.net.SocketOption<?>> supportedOpts = channel.supportedOptions();
101
102 if (channel instanceof ServerSocketChannel) {
103 List<ChannelOption<?>> extraOpts = new ArrayList<ChannelOption<?>>(supportedOpts.size());
104 for (java.net.SocketOption<?> opt : supportedOpts) {
105 if (opt == java.net.StandardSocketOptions.IP_TOS) {
106
107
108 continue;
109 }
110 extraOpts.add(new NioChannelOption(opt));
111 }
112 return extraOpts.toArray(new ChannelOption[0]);
113 } else {
114 ChannelOption<?>[] extraOpts = new ChannelOption[supportedOpts.size()];
115
116 int i = 0;
117 for (java.net.SocketOption<?> opt : supportedOpts) {
118 extraOpts[i++] = new NioChannelOption(opt);
119 }
120 return extraOpts;
121 }
122 }
123 }