View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix;
19  
20  import java.io.IOException;
21  import java.net.DatagramSocketImpl;
22  import java.net.SocketOption;
23  import java.util.Set;
24  
25  /**
26   * A shim that is filled with Java version-specific overrides. This variant is for Java 9 and above.
27   *
28   * @author Christian Kohlschütter
29   */
30  abstract class DatagramSocketImplShim extends DatagramSocketImpl {
31    protected DatagramSocketImplShim() {
32      super();
33    }
34  
35    @Override
36    protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
37      if (name instanceof AFSocketOption<?>) {
38        ((AFDatagramSocketImpl<?>) this).getCore().setOption((AFSocketOption<T>) name, value);
39        return;
40      }
41      Integer optionId = SocketOptionsMapper.resolve(name);
42      if (optionId == null) {
43        super.setOption(name, value);
44      } else {
45        setOption(optionId, value);
46      }
47    }
48  
49    @SuppressWarnings("unchecked")
50    @Override
51    protected <T> T getOption(SocketOption<T> name) throws IOException {
52      if (name instanceof AFSocketOption<?>) {
53        return ((AFDatagramSocketImpl<?>) this).getCore().getOption((AFSocketOption<T>) name);
54      }
55      Integer optionId = SocketOptionsMapper.resolve(name);
56      if (optionId == null) {
57        return super.getOption(name);
58      } else {
59        return (T) getOption(optionId);
60      }
61    }
62  
63    @Override
64    protected Set<SocketOption<?>> supportedOptions() {
65      return SocketOptionsMapper.SUPPORTED_SOCKET_OPTIONS;
66    }
67  }