Top Description Inners Fields Constructors Methods
org.jruby.ext.socket

public Class SubnetUtils

extends Object
Class Inheritance
Imports
java.util.regex.Matcher, .Pattern

A class that performs some subnet calculations given a network address and a subnet mask.
Author
[email protected]
Since
2.0 Extracted from Apache Commons Net Project.
See Also
http://www.faqs.org/rfcs/rfc1519.html

Nested and Inner Type Summary

Modifier and TypeClass and Description
public class
SubnetUtils.SubnetInfo

Convenience container for subnet summary information.

Field Summary

Modifier and TypeField and Description
private int
private static final Pattern
private int
private static final Pattern
private boolean
inclusiveHostCount

Whether the broadcast/network address are included in host count

private static final String
private static final int
private int
private int
private static final String

Constructor Summary

AccessConstructor and Description
public
SubnetUtils(String
A CIDR-notation string, e.g. "192.168.0.1/16"
cidrNotation
)

Constructor that takes a CIDR-notation string, e.g. "192.168.0.1/16"

public
SubnetUtils(String
An IP address, e.g. "192.168.0.1"
address
,
String
A dotted decimal netmask e.g. "255.255.0.0"
mask
)

Constructor that takes a dotted decimal address and a dotted decimal mask.

Method Summary

Modifier and TypeMethod and Description
private void
private String
format(int[] octets)

public final SubnetUtils.SubnetInfo

Returns:

new instance
getInfo
()

Return a SubnetInfo instance that contains subnet-specific statistics

public boolean
isInclusiveHostCount()

Returns true if the return value of SubnetInfo#getAddressCount() includes the network address and broadcast addresses.

private int
pack-priv int
pop(int x)

private int
rangeCheck(int value, int begin, int end)

public void
setInclusiveHostCount(boolean inclusiveHostCount)

Set to true if you want the return value of SubnetInfo#getAddressCount() to include the network and broadcast addresses.

private int[]
toArray(int val)

private String
private int
toInteger(String address)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

addressback to summary
private int address
addressPatternback to summary
private static final Pattern addressPattern
broadcastback to summary
private int broadcast
cidrPatternback to summary
private static final Pattern cidrPattern
inclusiveHostCountback to summary
private boolean inclusiveHostCount

Whether the broadcast/network address are included in host count

IP_ADDRESSback to summary
private static final String IP_ADDRESS
NBITSback to summary
private static final int NBITS
netmaskback to summary
private int netmask
networkback to summary
private int network
SLASH_FORMATback to summary
private static final String SLASH_FORMAT

Constructor Detail

SubnetUtilsback to summary
public SubnetUtils(String cidrNotation)

Constructor that takes a CIDR-notation string, e.g. "192.168.0.1/16"

Parameters
cidrNotation:String

A CIDR-notation string, e.g. "192.168.0.1/16"

Exceptions
IllegalArgumentException:
if the parameter is invalid, i.e. does not match n.n.n.n/m where n=1-3 decimal digits, m = 1-3 decimal digits in range 1-32
SubnetUtilsback to summary
public SubnetUtils(String address, String mask)

Constructor that takes a dotted decimal address and a dotted decimal mask.

Parameters
address:String

An IP address, e.g. "192.168.0.1"

mask:String

A dotted decimal netmask e.g. "255.255.0.0"

Exceptions
IllegalArgumentException:
if the address or mask is invalid, i.e. does not match n.n.n.n where n=1-3 decimal digits and the mask is not all zeros

Method Detail

calculateback to summary
private void calculate(String mask)
formatback to summary
private String format(int[] octets)
getInfoback to summary
public final SubnetUtils.SubnetInfo getInfo()

Return a SubnetInfo instance that contains subnet-specific statistics

Returns:SubnetUtils.SubnetInfo

new instance

isInclusiveHostCountback to summary
public boolean isInclusiveHostCount()

Returns true if the return value of SubnetInfo#getAddressCount() includes the network address and broadcast addresses.

Since
2.2
matchAddressback to summary
private int matchAddress(Matcher matcher)
popback to summary
pack-priv int pop(int x)
rangeCheckback to summary
private int rangeCheck(int value, int begin, int end)
setInclusiveHostCountback to summary
public void setInclusiveHostCount(boolean inclusiveHostCount)

Set to true if you want the return value of SubnetInfo#getAddressCount() to include the network and broadcast addresses.

Since
2.2
toArrayback to summary
private int[] toArray(int val)
toCidrNotationback to summary
private String toCidrNotation(String addr, String mask)
toIntegerback to summary
private int toInteger(String address)
org.jruby.ext.socket back to summary

public final Class SubnetUtils.SubnetInfo

extends Object
Class Inheritance

Convenience container for subnet summary information.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private int
public int
asInteger(String address)

private int
public String
public int

Returns:

the count of addresses, may be zero.
getAddressCount
()

Get the count of available addresses.

public String[]
public String
public String
public String

Returns:

the IP address in dotted format, may be "0.0.0.0" if there is no valid address
getHighAddress
()

Return the high address as a dotted IP address.

public String

Returns:

the IP address in dotted format, may be "0.0.0.0" if there is no valid address
getLowAddress
()

Return the low address as a dotted IP address.

public String
public String
private int
high()

public boolean

Returns:

True if in range, false otherwise
isInRange
(String
A dot-delimited IPv4 address, e.g. "192.168.0.1"
address
)

Returns true if the parameter address is in the range of usable endpoint addresses for this subnet.

private boolean
isInRange(int address)

private int
low()

private int
private int
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

SubnetInfoback to summary
private SubnetInfo()

Method Detail

addressback to summary
private int address()
asIntegerback to summary
public int asInteger(String address)
broadcastback to summary
private int broadcast()
getAddressback to summary
public String getAddress()
getAddressCountback to summary
public int getAddressCount()

Get the count of available addresses. Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.

Returns:int

the count of addresses, may be zero.

getAllAddressesback to summary
public String[] getAllAddresses()
getBroadcastAddressback to summary
public String getBroadcastAddress()
getCidrSignatureback to summary
public String getCidrSignature()
getHighAddressback to summary
public String getHighAddress()

Return the high address as a dotted IP address. Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.

Returns:String

the IP address in dotted format, may be "0.0.0.0" if there is no valid address

getLowAddressback to summary
public String getLowAddress()

Return the low address as a dotted IP address. Will be zero for CIDR/31 and CIDR/32 if the inclusive flag is false.

Returns:String

the IP address in dotted format, may be "0.0.0.0" if there is no valid address

getNetmaskback to summary
public String getNetmask()
getNetworkAddressback to summary
public String getNetworkAddress()
highback to summary
private int high()
isInRangeback to summary
public boolean isInRange(String address)

Returns true if the parameter address is in the range of usable endpoint addresses for this subnet. This excludes the network and broadcast adresses.

Parameters
address:String

A dot-delimited IPv4 address, e.g. "192.168.0.1"

Returns:boolean

True if in range, false otherwise

isInRangeback to summary
private boolean isInRange(int address)
lowback to summary
private int low()
netmaskback to summary
private int netmask()
networkback to summary
private int network()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
Since
2.2