View Javadoc
1   /*
2    * Copyright 2015 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.netty5.channel.unix;
17  
18  import java.net.Inet6Address;
19  import java.net.InetAddress;
20  import java.net.InetSocketAddress;
21  import java.net.UnknownHostException;
22  
23  /**
24   * Act as special {@link InetSocketAddress} to be able to easily pass all needed data from JNI without the need
25   * to create more objects then needed.
26   * <p>
27   * <strong>Internal usage only!</strong>
28   */
29  public final class DatagramSocketAddress extends InetSocketAddress {
30      private static final long serialVersionUID = 3094819287843178401L;
31  
32      // holds the amount of received bytes
33      private final int receivedAmount;
34      private final DatagramSocketAddress localAddress;
35  
36      DatagramSocketAddress(byte[] addr, int scopeId, int port, int receivedAmount, DatagramSocketAddress local)
37              throws UnknownHostException {
38          super(newAddress(addr, scopeId), port);
39          this.receivedAmount = receivedAmount;
40          localAddress = local;
41      }
42  
43      public DatagramSocketAddress localAddress() {
44          return localAddress;
45      }
46  
47      public int receivedAmount() {
48          return receivedAmount;
49      }
50  
51      private static InetAddress newAddress(byte[] bytes, int scopeId) throws UnknownHostException {
52          if (bytes.length == 4) {
53              return InetAddress.getByAddress(bytes);
54          }
55          return Inet6Address.getByAddress(null, bytes, scopeId);
56      }
57  }