Package org.refcodes.net
Enum IpAddress
- java.lang.Object
-
- java.lang.Enum<IpAddress>
-
- org.refcodes.net.IpAddress
-
- All Implemented Interfaces:
Serializable
,Comparable<IpAddress>
public enum IpAddress extends Enum<IpAddress>
Metrics of an IP-Address as of IPv4 or IPv6.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static int[]
fromAnyCidrNotation(String aCidrNotation)
Creates an IP-Address as of an integer array for the first of the enumeration elements being able to successfully parse the providedString
in CIDR notation.int[]
fromCidrNotation(String aCidrNotation)
Creates an IP-Address as of an integer array for the current enumeration from the providedString
in CIDR notation.int[]
getBroadcast()
The byte representation of the "broadcast" address (if the protocol supports a broadcast).char
getCidrDelimiter()
The delimiter used by the CIDR notation.String
getCidrLocalhost()
Returns the CIDR notation of the localhost IP-Address.String
getCidrUnspecified()
Returns the CIDR notation of the unspecified IP-Address.int
getLength()
Returns the length in bytes of the IP-Address.int[]
getLocalhost()
The byte representation of the "localhost".String
getLocalhostAlias()
The commonly used alias for the "localhost".int[]
getUnspecified()
The byte representation of the "unspecified".static boolean
isAnyCidrNotation(String aCidrNotation)
Determines whether the givenString
represents a valid IP-Address.static boolean
isBroadcast(int[] aIpAddress)
Determines whether the given IP-Address is the broadcast address.boolean
isCidrNotation(String aCidrNotation)
Determines whether the givenString
represents a valid CIDR notation.static boolean
isLocalhost(int[] aIpAddress)
Determines whether the given IP-Address represents the IP-Address of the "localhost" as ofgetLocalhost()
.static boolean
isLoopback(int[] aIpAddress)
Determines whether the given IP-Address is in the scope of the the loopback interfaces.String
toCidrNotation(int[] theIpAddress)
static int[]
toHostIpAddress()
Tries to determine a no-localhost IP-Address for this machine.static String
toString(int[] aIpAddress)
Creates aString
as of the CIDR notation from the provided IP-Address bytes.static Set<String>
toVisibleIpAddresses()
Tries to determine the known IP-Addresses from the accessible network segment.static IpAddress
valueOf(String name)
Returns the enum constant of this type with the specified name.static IpAddress[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Method Detail
-
values
public static IpAddress[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (IpAddress c : IpAddress.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static IpAddress valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
getLength
public int getLength()
Returns the length in bytes of the IP-Address.- Returns:
- The IP-Address' length in bytes.
-
getUnspecified
public int[] getUnspecified()
The byte representation of the "unspecified".- Returns:
- The byte representation of the "unspecified".
-
getCidrUnspecified
public String getCidrUnspecified()
Returns the CIDR notation of the unspecified IP-Address. See also "https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation".- Returns:
- The CIDR notation for "unspecified".
-
getLocalhost
public int[] getLocalhost()
The byte representation of the "localhost".- Returns:
- The byte representation of the "localhost".
-
getBroadcast
public int[] getBroadcast()
The byte representation of the "broadcast" address (if the protocol supports a broadcast).- Returns:
- The byte representation of the "broadcast" address or null if the protocol does not support a broadcast address.
-
getCidrLocalhost
public String getCidrLocalhost()
Returns the CIDR notation of the localhost IP-Address. See also "https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation".- Returns:
- The CIDR notation for "localhost".
-
getLocalhostAlias
public String getLocalhostAlias()
The commonly used alias for the "localhost".- Returns:
- The "localhost" alias.
-
getCidrDelimiter
public char getCidrDelimiter()
The delimiter used by the CIDR notation. See also "https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation".- Returns:
- The CIDR delimiter.
-
toCidrNotation
public String toCidrNotation(int[] theIpAddress)
-
fromCidrNotation
public int[] fromCidrNotation(String aCidrNotation)
Creates an IP-Address as of an integer array for the current enumeration from the providedString
in CIDR notation.- Parameters:
aCidrNotation
- The String in CIDR notation to be parsed.- Returns:
- The IP-Address as an array of integer values.
- Throws:
IllegalArgumentException
- Thrown in case the providedString
is not of any known CIDR notation.
-
toString
public static String toString(int[] aIpAddress)
Creates aString
as of the CIDR notation from the provided IP-Address bytes. See also "https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation".
-
isLocalhost
public static boolean isLocalhost(int[] aIpAddress)
Determines whether the given IP-Address represents the IP-Address of the "localhost" as ofgetLocalhost()
.- Parameters:
aIpAddress
- The IP-Address to be tested.- Returns:
- True in case we have an IP-Address representing the localhost, else false.
-
isLoopback
public static boolean isLoopback(int[] aIpAddress)
Determines whether the given IP-Address is in the scope of the the loopback interfaces.- Parameters:
aIpAddress
- The IP-Address to be tested.- Returns:
- True in case we have an IP-Address belonging to the loopback IP-Address block, else false.
-
isBroadcast
public static boolean isBroadcast(int[] aIpAddress)
Determines whether the given IP-Address is the broadcast address.- Parameters:
aIpAddress
- The IP-Address to be tested.- Returns:
- True in case we have an IP-Address being the broadcast address, else false.
-
fromAnyCidrNotation
public static int[] fromAnyCidrNotation(String aCidrNotation)
Creates an IP-Address as of an integer array for the first of the enumeration elements being able to successfully parse the providedString
in CIDR notation.- Parameters:
aCidrNotation
- The String in CIDR notation to be parsed.- Returns:
- The IP-Address as an array of integer values.
- Throws:
IllegalArgumentException
- Thrown in case the providedString
is not of any known CIDR notation.
-
toHostIpAddress
public static int[] toHostIpAddress() throws IOException
Tries to determine a no-localhost IP-Address for this machine. The best guess is returned. If none no-localhost address is found, then the localhost's IP-Address may be returned (as ofgetLocalhost()
).- Returns:
- The best guest for a no-localhost IP-Address or as a fall back
the localhost's IP-Address (as of
getLocalhost()
) may be returned. - Throws:
IOException
- Thrown to indicate that the IP address of a host could not be determined.
-
isAnyCidrNotation
public static boolean isAnyCidrNotation(String aCidrNotation)
Determines whether the givenString
represents a valid IP-Address.- Parameters:
aCidrNotation
- The Text in CIDR notation to be tested.- Returns:
- True in case of being an IP-Address, else false.
-
isCidrNotation
public boolean isCidrNotation(String aCidrNotation)
Determines whether the givenString
represents a valid CIDR notation.- Parameters:
aCidrNotation
- The Text in CIDR notation to be tested.- Returns:
- True in case of being an IP-Address, else false.
-
toVisibleIpAddresses
public static Set<String> toVisibleIpAddresses()
Tries to determine the known IP-Addresses from the accessible network segment. It tries to invoke "arp -a" and also tries to manually detect the visible IP-Addresses.- Returns:
- A set containing the visible IP-Addresses in CIDR notation.
-
-