Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/net/InterfaceAddress.java
41152 views
1
/*
2
* Copyright (c) 2005, 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
26
package java.net;
27
28
import java.util.Objects;
29
30
/**
31
* This class represents a Network Interface address. In short it's an
32
* IP address, a subnet mask and a broadcast address when the address is
33
* an IPv4 one. An IP address and a network prefix length in the case
34
* of IPv6 address.
35
*
36
* @see java.net.NetworkInterface
37
* @since 1.6
38
*/
39
public class InterfaceAddress {
40
private InetAddress address = null;
41
private Inet4Address broadcast = null;
42
private short maskLength = 0;
43
44
/*
45
* Package private constructor. Can't be built directly, instances are
46
* obtained through the NetworkInterface class.
47
*/
48
InterfaceAddress() {
49
}
50
51
/**
52
* Returns an {@code InetAddress} for this address.
53
*
54
* @return the {@code InetAddress} for this address.
55
*/
56
public InetAddress getAddress() {
57
return address;
58
}
59
60
/**
61
* Returns an {@code InetAddress} for the broadcast address
62
* for this InterfaceAddress.
63
* <p>
64
* Only IPv4 networks have broadcast address therefore, in the case
65
* of an IPv6 network, {@code null} will be returned.
66
*
67
* @return the {@code InetAddress} representing the broadcast
68
* address or {@code null} if there is no broadcast address.
69
*/
70
public InetAddress getBroadcast() {
71
return broadcast;
72
}
73
74
/**
75
* Returns the network prefix length for this address. This is also known
76
* as the subnet mask in the context of IPv4 addresses.
77
* Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0)
78
* or 24 (255.255.255.0). <p>
79
* Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)
80
*
81
* @return a {@code short} representing the prefix length for the
82
* subnet of that address.
83
*/
84
public short getNetworkPrefixLength() {
85
return maskLength;
86
}
87
88
/**
89
* Compares this object against the specified object.
90
* The result is {@code true} if and only if the argument is
91
* not {@code null} and it represents the same interface address as
92
* this object.
93
* <p>
94
* Two instances of {@code InterfaceAddress} represent the same
95
* address if the InetAddress, the prefix length and the broadcast are
96
* the same for both.
97
*
98
* @param obj the object to compare against.
99
* @return {@code true} if the objects are the same;
100
* {@code false} otherwise.
101
* @see java.net.InterfaceAddress#hashCode()
102
*/
103
public boolean equals(Object obj) {
104
return obj instanceof InterfaceAddress cmp &&
105
Objects.equals(address, cmp.address) &&
106
Objects.equals(broadcast, cmp.broadcast) &&
107
maskLength == cmp.maskLength;
108
}
109
110
/**
111
* Returns a hashcode for this Interface address.
112
*
113
* @return a hash code value for this Interface address.
114
*/
115
public int hashCode() {
116
return address.hashCode() + ((broadcast != null) ? broadcast.hashCode() : 0) + maskLength;
117
}
118
119
/**
120
* Converts this Interface address to a {@code String}. The
121
* string returned is of the form: InetAddress / prefix length [ broadcast address ].
122
*
123
* @return a string representation of this Interface address.
124
*/
125
public String toString() {
126
return address + "/" + maskLength + " [" + broadcast + "]";
127
}
128
129
}
130
131