1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.SocketAddress;
25 import java.net.SocketException;
26 import java.net.URI;
27 import java.nio.ByteBuffer;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.Objects;
31 import java.util.Set;
32
33 import org.newsclub.net.unix.pool.ObjectPool.Lease;
34
35
36
37
38
39
40 public final class AFGenericSocketAddress extends AFSocketAddress {
41 private static final long serialVersionUID = 1L;
42
43 private static AFAddressFamily<AFGenericSocketAddress> family;
44 private static final String SELECTOR_PROVIDER_CLASS =
45 "org.newsclub.net.unix.generic.AFGenericSelectorProvider";
46
47 private AFGenericSocketAddress(int port, final byte[] socketAddress,
48 Lease<ByteBuffer> nativeAddress) throws SocketException {
49 super(port, socketAddress, nativeAddress, addressFamily());
50 }
51
52 private static AFGenericSocketAddress newAFSocketAddress(int port, final byte[] socketAddress,
53 Lease<ByteBuffer> nativeAddress) throws SocketException {
54 return newDeserializedAFSocketAddress(port, socketAddress, nativeAddress, addressFamily(),
55 AFGenericSocketAddress::new);
56 }
57
58
59
60
61
62
63
64
65
66
67
68 public static AFGenericSocketAddress unwrap(InetAddress address, int port)
69 throws SocketException {
70 return AFSocketAddress.unwrap(address, port, addressFamily());
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84 public static AFGenericSocketAddress unwrap(String hostname, int port) throws SocketException {
85 return AFSocketAddress.unwrap(hostname, port, addressFamily());
86 }
87
88
89
90
91
92
93
94
95
96 public static AFGenericSocketAddress unwrap(SocketAddress address) throws SocketException {
97 Objects.requireNonNull(address);
98 if (!isSupportedAddress(address)) {
99 throw new SocketException("Unsupported address");
100 }
101 return (AFGenericSocketAddress) address;
102 }
103
104 @Override
105 public String toString() {
106 int port = getPort();
107
108 return getClass().getName() + "[" + (port == 0 ? "" : "port=" + port + ";") + "bytes=" + Arrays
109 .toString(getBytes()) + "]";
110 }
111
112
113
114
115
116
117
118
119
120 public byte[] toBytes() {
121 byte[] bytes = getBytes();
122 return Arrays.copyOf(bytes, bytes.length);
123 }
124
125 @Override
126 public boolean hasFilename() {
127 return false;
128 }
129
130 @Override
131 public File getFile() throws FileNotFoundException {
132 throw new FileNotFoundException("no file");
133 }
134
135
136
137
138
139
140
141
142
143 public static boolean isSupportedAddress(InetAddress addr) {
144 return AFSocketAddress.isSupportedAddress(addr, addressFamily());
145 }
146
147
148
149
150
151
152
153
154 public static boolean isSupportedAddress(SocketAddress addr) {
155 return (addr instanceof AFGenericSocketAddress);
156 }
157
158
159
160
161
162
163 @SuppressWarnings("null")
164 public static synchronized AFAddressFamily<AFGenericSocketAddress> addressFamily() {
165 if (family == null) {
166 family = AFAddressFamily.registerAddressFamily("generic",
167 AFGenericSocketAddress.class, new AFSocketAddressConfig<AFGenericSocketAddress>() {
168
169 private final AFSocketAddressConstructor<AFGenericSocketAddress> addrConstr =
170 isUseDeserializationForInit() ? AFGenericSocketAddress::newAFSocketAddress
171 : AFGenericSocketAddress::new;
172
173 @Override
174 protected AFGenericSocketAddress parseURI(URI u, int port) throws SocketException {
175 return AFGenericSocketAddress.of(u, port);
176 }
177
178 @Override
179 protected AFSocketAddressConstructor<AFGenericSocketAddress> addressConstructor() {
180 return addrConstr;
181 }
182
183 @Override
184 protected String selectorProviderClassname() {
185 return SELECTOR_PROVIDER_CLASS;
186 }
187
188 @Override
189 protected Set<String> uriSchemes() {
190 return Collections.emptySet();
191 }
192 });
193 try {
194 Class.forName(SELECTOR_PROVIDER_CLASS);
195 } catch (ClassNotFoundException e) {
196
197 }
198 }
199 return family;
200 }
201
202
203
204
205
206
207
208
209 @SuppressWarnings("PMD.ShortMethodName")
210 public static AFGenericSocketAddress of(URI uri) throws SocketException {
211 return of(uri, -1);
212 }
213
214
215
216
217
218
219
220
221
222 @SuppressWarnings({
223 "PMD.CognitiveComplexity", "PMD.CyclomaticComplexity", "PMD.ExcessiveMethodLength",
224 "PMD.NcssCount", "PMD.NPathComplexity", "PMD.ShortMethodName"})
225 public static AFGenericSocketAddress of(URI uri, int overridePort) throws SocketException {
226 throw new SocketException("Unsupported");
227 }
228
229 @Override
230 @SuppressWarnings({"PMD.CognitiveComplexity", "PMD.CompareObjectsWithEquals"})
231 public URI toURI(String scheme, URI template) throws IOException {
232 return super.toURI(scheme, template);
233 }
234 }