Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/net/InetSocketAddress.java
41152 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package java.net;
26
27
import java.io.IOException;
28
import java.io.InvalidObjectException;
29
import java.io.ObjectInputStream;
30
import java.io.ObjectOutputStream;
31
import java.io.ObjectStreamException;
32
import java.io.ObjectStreamField;
33
34
/**
35
*
36
* This class implements an IP Socket Address (IP address + port number)
37
* It can also be a pair (hostname + port number), in which case an attempt
38
* will be made to resolve the hostname. If resolution fails then the address
39
* is said to be <I>unresolved</I> but can still be used on some circumstances
40
* like connecting through a proxy.
41
* <p>
42
* It provides an immutable object used by sockets for binding, connecting, or
43
* as returned values.
44
* <p>
45
* The <i>wildcard</i> is a special local IP address. It usually means "any"
46
* and can only be used for {@code bind} operations.
47
*
48
* @see java.net.Socket
49
* @see java.net.ServerSocket
50
* @since 1.4
51
*/
52
public class InetSocketAddress
53
extends SocketAddress
54
{
55
// Private implementation class pointed to by all public methods.
56
private static class InetSocketAddressHolder {
57
// The hostname of the Socket Address
58
private String hostname;
59
// The IP address of the Socket Address
60
private InetAddress addr;
61
// The port number of the Socket Address
62
private int port;
63
64
private InetSocketAddressHolder(String hostname, InetAddress addr, int port) {
65
this.hostname = hostname;
66
this.addr = addr;
67
this.port = port;
68
}
69
70
private int getPort() {
71
return port;
72
}
73
74
private InetAddress getAddress() {
75
return addr;
76
}
77
78
private String getHostName() {
79
if (hostname != null)
80
return hostname;
81
if (addr != null)
82
return addr.getHostName();
83
return null;
84
}
85
86
private String getHostString() {
87
if (hostname != null)
88
return hostname;
89
if (addr != null) {
90
if (addr.holder().getHostName() != null)
91
return addr.holder().getHostName();
92
else
93
return addr.getHostAddress();
94
}
95
return null;
96
}
97
98
private boolean isUnresolved() {
99
return addr == null;
100
}
101
102
@Override
103
public String toString() {
104
105
String formatted;
106
107
if (isUnresolved()) {
108
formatted = hostname + "/<unresolved>";
109
} else {
110
formatted = addr.toString();
111
if (addr instanceof Inet6Address) {
112
int i = formatted.lastIndexOf("/");
113
formatted = formatted.substring(0, i + 1)
114
+ "[" + formatted.substring(i + 1) + "]";
115
}
116
}
117
return formatted + ":" + port;
118
}
119
120
@Override
121
public final boolean equals(Object obj) {
122
if (!(obj instanceof InetSocketAddressHolder that))
123
return false;
124
boolean sameIP;
125
if (addr != null)
126
sameIP = addr.equals(that.addr);
127
else if (hostname != null)
128
sameIP = (that.addr == null) &&
129
hostname.equalsIgnoreCase(that.hostname);
130
else
131
sameIP = (that.addr == null) && (that.hostname == null);
132
return sameIP && (port == that.port);
133
}
134
135
@Override
136
public final int hashCode() {
137
if (addr != null)
138
return addr.hashCode() + port;
139
if (hostname != null)
140
return hostname.toLowerCase().hashCode() + port;
141
return port;
142
}
143
}
144
145
private final transient InetSocketAddressHolder holder;
146
147
@java.io.Serial
148
private static final long serialVersionUID = 5076001401234631237L;
149
150
private static int checkPort(int port) {
151
if (port < 0 || port > 0xFFFF)
152
throw new IllegalArgumentException("port out of range:" + port);
153
return port;
154
}
155
156
private static String checkHost(String hostname) {
157
if (hostname == null)
158
throw new IllegalArgumentException("hostname can't be null");
159
return hostname;
160
}
161
162
/**
163
* Creates a socket address where the IP address is the wildcard address
164
* and the port number a specified value.
165
* <p>
166
* A valid port value is between 0 and 65535.
167
* A port number of {@code zero} will let the system pick up an
168
* ephemeral port in a {@code bind} operation.
169
*
170
* @param port The port number
171
* @throws IllegalArgumentException if the port parameter is outside the specified
172
* range of valid port values.
173
*/
174
public InetSocketAddress(int port) {
175
this(InetAddress.anyLocalAddress(), port);
176
}
177
178
/**
179
*
180
* Creates a socket address from an IP address and a port number.
181
* <p>
182
* A valid port value is between 0 and 65535.
183
* A port number of {@code zero} will let the system pick up an
184
* ephemeral port in a {@code bind} operation.
185
* <P>
186
* A {@code null} address will assign the <i>wildcard</i> address.
187
*
188
* @param addr The IP address
189
* @param port The port number
190
* @throws IllegalArgumentException if the port parameter is outside the specified
191
* range of valid port values.
192
*/
193
public InetSocketAddress(InetAddress addr, int port) {
194
holder = new InetSocketAddressHolder(
195
null,
196
addr == null ? InetAddress.anyLocalAddress() : addr,
197
checkPort(port));
198
}
199
200
/**
201
*
202
* Creates a socket address from a hostname and a port number.
203
* <p>
204
* An attempt will be made to resolve the hostname into an InetAddress.
205
* If that attempt fails, the address will be flagged as <I>unresolved</I>.
206
* <p>
207
* If there is a security manager, its {@code checkConnect} method
208
* is called with the host name as its argument to check the permission
209
* to resolve it. This could result in a SecurityException.
210
* <P>
211
* A valid port value is between 0 and 65535.
212
* A port number of {@code zero} will let the system pick up an
213
* ephemeral port in a {@code bind} operation.
214
*
215
* @param hostname the Host name
216
* @param port The port number
217
* @throws IllegalArgumentException if the port parameter is outside the range
218
* of valid port values, or if the hostname parameter is {@code null}.
219
* @throws SecurityException if a security manager is present and
220
* permission to resolve the host name is
221
* denied.
222
* @see #isUnresolved()
223
*/
224
public InetSocketAddress(String hostname, int port) {
225
checkHost(hostname);
226
InetAddress addr = null;
227
String host = null;
228
try {
229
addr = InetAddress.getByName(hostname);
230
} catch(UnknownHostException e) {
231
host = hostname;
232
}
233
holder = new InetSocketAddressHolder(host, addr, checkPort(port));
234
}
235
236
// private constructor for creating unresolved instances
237
private InetSocketAddress(int port, String hostname) {
238
holder = new InetSocketAddressHolder(hostname, null, port);
239
}
240
241
/**
242
*
243
* Creates an unresolved socket address from a hostname and a port number.
244
* <p>
245
* No attempt will be made to resolve the hostname into an InetAddress.
246
* The address will be flagged as <I>unresolved</I>.
247
* <p>
248
* A valid port value is between 0 and 65535.
249
* A port number of {@code zero} will let the system pick up an
250
* ephemeral port in a {@code bind} operation.
251
*
252
* @param host the Host name
253
* @param port The port number
254
* @throws IllegalArgumentException if the port parameter is outside
255
* the range of valid port values, or if the hostname
256
* parameter is {@code null}.
257
* @see #isUnresolved()
258
* @return an {@code InetSocketAddress} representing the unresolved
259
* socket address
260
* @since 1.5
261
*/
262
public static InetSocketAddress createUnresolved(String host, int port) {
263
return new InetSocketAddress(checkPort(port), checkHost(host));
264
}
265
266
/**
267
* @serialField hostname String the hostname of the Socket Address
268
* @serialField addr InetAddress the IP address of the Socket Address
269
* @serialField port int the port number of the Socket Address
270
*/
271
@java.io.Serial
272
private static final ObjectStreamField[] serialPersistentFields = {
273
new ObjectStreamField("hostname", String.class),
274
new ObjectStreamField("addr", InetAddress.class),
275
new ObjectStreamField("port", int.class)};
276
277
/**
278
* Writes the state of this object to the stream.
279
*
280
* @param out the {@code ObjectOutputStream} to which data is written
281
* @throws IOException if an I/O error occurs
282
*/
283
@java.io.Serial
284
private void writeObject(ObjectOutputStream out)
285
throws IOException
286
{
287
// Don't call defaultWriteObject()
288
ObjectOutputStream.PutField pfields = out.putFields();
289
pfields.put("hostname", holder.hostname);
290
pfields.put("addr", holder.addr);
291
pfields.put("port", holder.port);
292
out.writeFields();
293
}
294
295
/**
296
* Restores the state of this object from the stream.
297
*
298
* @param in the {@code ObjectInputStream} from which data is read
299
* @throws IOException if an I/O error occurs
300
* @throws ClassNotFoundException if a serialized class cannot be loaded
301
*/
302
@java.io.Serial
303
private void readObject(ObjectInputStream in)
304
throws IOException, ClassNotFoundException
305
{
306
// Don't call defaultReadObject()
307
ObjectInputStream.GetField oisFields = in.readFields();
308
final String oisHostname = (String)oisFields.get("hostname", null);
309
final InetAddress oisAddr = (InetAddress)oisFields.get("addr", null);
310
final int oisPort = oisFields.get("port", -1);
311
312
// Check that our invariants are satisfied
313
checkPort(oisPort);
314
if (oisHostname == null && oisAddr == null)
315
throw new InvalidObjectException("hostname and addr " +
316
"can't both be null");
317
318
InetSocketAddressHolder h = new InetSocketAddressHolder(oisHostname,
319
oisAddr,
320
oisPort);
321
UNSAFE.putReference(this, FIELDS_OFFSET, h);
322
}
323
324
/**
325
* Throws {@code InvalidObjectException}, always.
326
* @throws ObjectStreamException always
327
*/
328
@java.io.Serial
329
private void readObjectNoData()
330
throws ObjectStreamException
331
{
332
throw new InvalidObjectException("Stream data required");
333
}
334
335
private static final jdk.internal.misc.Unsafe UNSAFE
336
= jdk.internal.misc.Unsafe.getUnsafe();
337
private static final long FIELDS_OFFSET
338
= UNSAFE.objectFieldOffset(InetSocketAddress.class, "holder");
339
340
/**
341
* Gets the port number.
342
*
343
* @return the port number.
344
*/
345
public final int getPort() {
346
return holder.getPort();
347
}
348
349
/**
350
* Gets the {@code InetAddress}.
351
*
352
* @return the InetAddress or {@code null} if it is unresolved.
353
*/
354
public final InetAddress getAddress() {
355
return holder.getAddress();
356
}
357
358
/**
359
* Gets the {@code hostname}.
360
* Note: This method may trigger a name service reverse lookup if the
361
* address was created with a literal IP address.
362
*
363
* @return the hostname part of the address.
364
*/
365
public final String getHostName() {
366
return holder.getHostName();
367
}
368
369
/**
370
* Returns the hostname, or the String form of the address if it
371
* doesn't have a hostname (it was created using a literal).
372
* This has the benefit of <b>not</b> attempting a reverse lookup.
373
*
374
* @return the hostname, or String representation of the address.
375
* @since 1.7
376
*/
377
public final String getHostString() {
378
return holder.getHostString();
379
}
380
381
/**
382
* Checks whether the address has been resolved or not.
383
*
384
* @return {@code true} if the hostname couldn't be resolved into
385
* an {@code InetAddress}.
386
*/
387
public final boolean isUnresolved() {
388
return holder.isUnresolved();
389
}
390
391
/**
392
* Constructs a string representation of this InetSocketAddress.
393
* This string is constructed by calling {@link InetAddress#toString()}
394
* on the InetAddress and concatenating the port number (with a colon).
395
* <p>
396
* If the address is an IPv6 address, the IPv6 literal is enclosed in
397
* square brackets, for example: {@code "localhost/[0:0:0:0:0:0:0:1]:80"}.
398
* If the address is {@linkplain #isUnresolved() unresolved},
399
* {@code <unresolved>} is displayed in place of the address literal, for
400
* example {@code "foo/<unresolved>:80"}.
401
* <p>
402
* To retrieve a string representation of the hostname or the address, use
403
* {@link #getHostString()}, rather than parsing the string returned by this
404
* {@link #toString()} method.
405
*
406
* @return a string representation of this object.
407
*/
408
@Override
409
public String toString() {
410
return holder.toString();
411
}
412
413
/**
414
* Compares this object against the specified object.
415
* The result is {@code true} if and only if the argument is
416
* not {@code null} and it represents the same address as
417
* this object.
418
* <p>
419
* Two instances of {@code InetSocketAddress} represent the same
420
* address if both the InetAddresses (or hostnames if it is unresolved) and port
421
* numbers are equal.
422
* If both addresses are unresolved, then the hostname and the port number
423
* are compared.
424
*
425
* Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are
426
* considered equal.
427
*
428
* @param obj the object to compare against.
429
* @return {@code true} if the objects are the same;
430
* {@code false} otherwise.
431
* @see java.net.InetAddress#equals(java.lang.Object)
432
*/
433
@Override
434
public final boolean equals(Object obj) {
435
if (obj instanceof InetSocketAddress addr) {
436
return holder.equals(addr.holder);
437
}
438
return false;
439
}
440
441
/**
442
* Returns a hashcode for this socket address.
443
*
444
* @return a hash code value for this socket address.
445
*/
446
@Override
447
public final int hashCode() {
448
return holder.hashCode();
449
}
450
}
451
452