View Javadoc
1   /*
2    * Copyright 2022 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.socket;
17  
18  import java.net.ProtocolFamily;
19  import java.net.StandardProtocolFamily;
20  
21  /**
22   * {@link ProtocolFamily} implementation that is used by the different transport implementations.
23   */
24  public enum SocketProtocolFamily implements ProtocolFamily {
25      /**
26       * IPv4
27       */
28      INET,
29      /**
30       * IPv6
31       */
32      INET6,
33      /**
34       * Unix Domain Socket
35       */
36      UNIX;
37  
38      /**
39       * Return a {@link ProtocolFamily} that is "known" by the JDK itself and represent the same as
40       * {@link SocketProtocolFamily}.
41       *
42       * @return the JDK {@link ProtocolFamily}.
43       * @throws UnsupportedOperationException if it can't be converted.
44       */
45      public ProtocolFamily toJdkFamily() {
46          switch (this) {
47              case INET:
48                  return StandardProtocolFamily.INET;
49              case INET6:
50                  return StandardProtocolFamily.INET6;
51              case UNIX:
52                  // Just use valueOf as we compile with Java11. If the JDK does not support unix domain sockets, this
53                  // will throw.
54                  return StandardProtocolFamily.valueOf("UNIX");
55              default:
56                  throw new UnsupportedOperationException(
57                          "ProtocolFamily cant be converted to something that is known by the JDKi: " + this);
58          }
59      }
60  
61      /**
62       * Return the {@link SocketProtocolFamily} for the given {@link ProtocolFamily} if possible.
63       *
64       * @param family    the JDK {@link ProtocolFamily} to convert.
65       * @return          the {@link SocketProtocolFamily}.
66       * @throws          UnsupportedOperationException if it can't be converted.
67       */
68      public static SocketProtocolFamily of(ProtocolFamily family) {
69          if (family instanceof StandardProtocolFamily) {
70              switch ((StandardProtocolFamily) family) {
71                  case INET:
72                      return INET;
73                  case INET6:
74                      return INET6;
75                  default:
76                      // Just compare the name as we compile with Java11
77                      if (UNIX.name().equals(family.name())) {
78                          return UNIX;
79                      }
80                      // Fall-through
81              }
82          } else if (family instanceof SocketProtocolFamily) {
83              return (SocketProtocolFamily) family;
84          }
85          throw new UnsupportedOperationException("ProtocolFamily is not supported: " + family);
86      }
87  }