Java library for handling IP addresses and subnets, both IPv4 and IPv6
IP address and network manipulations, CIDR, address and subnet operations, iterations, containment checks, longest prefix match, subnetting, and address and subnet data structures including address tries, with polymorphic code
In the Maven Central Repository, packaged as an OSGI bundle, packaged as a Linux Fedora rpm
- Maven group id: com.github.seancfoley
- Maven artifact id: ipaddress
- Maven versions: 2.0.2, 3.0.0, 4.3.3, 5.5.1
- Latest Maven version:
- OSGI bundle since version 5.3.1: com.github.seancfoley.ipaddress
As a Java library, it is also interoperable with Kotlin, Scala, Groovy and Clojure
Available as a Go library from the ipaddress-go repository
Integrate into Development: Intellij, Android, and Eclipse
Because IPAddress is 100% Java, it is easy to integrate with your build and deploy process
Make your IPv4 App work with IPv6
Version | Notes |
---|---|
1.0.1 | Requires Java 6 or higher |
2.0.2 | Requires Java 8 or higher |
3.0.0 | Requires Java 8 or higher, features MAC address support, EUI-48 and EUI-64 MAC integration with IPv6, new address framework, new IP string formats parsed and produced, and other additions |
4.3.3 | Requires Java 8 or higher. Features new prefix length handling. IPv4-network/IPv6-subnet-router-anycast/zero-host addresses are interpreted as the prefix block subnet, while other prefixed addresses are individual addresses. There exists the option to preserve the version 3 behaviour. Version 4.2.0 has additional methods for managing prefix blocks. Version 4.3 features improved parsing performance and a change to increment(long) behaviour for subnets. |
Latest Version 5.5.1 | Requires Java 8 or higher. Compatible with Android using Android API level 24 or higher. The code is compiled with Java 8, but provides a Java 9 compiled module-info.class file for those using the Java Platform Module System (JPMS) in Java 9 or later versions. You may need (or wish) to delete the module-info, which can be done with gradle, when using Java 8 environments. Version 5 features the addition of IPAddress sequential range classes IP*AddressSeqRange, the reorganization of classes and interfaces in inet.ipaddr.format package to standard, large, and string subpackages, enhanced address block splitting and merging functionality, Java 8 stream and spliterator methods, additional parsing options, address tries, associative address tries, and the prefix block allocator. Other enhancements are listed on the releases page for 5.0.0, 5.1.0, 5.2.0, 5.3.0, 5.4.0 and 5.5.0 |
starting with address or subnet strings
String ipv6Str = "::/64";
String ipv4Str = "1.2.255.4/255.255.0.0";
try {
IPAddress ipv6Address = new IPAddressString(ipv6Str).toAddress();
IPAddress ipv4Address = new IPAddressString(ipv4Str).toAddress();
// use addresses
} catch (AddressStringException e) {
String msg = e.getMessage();//detailed message indicating improper string format
// handle improperly formatted address string
}
starting with host name strings
String hostPortStr = "[a:b:c:d:e:f:a:b]:8080";
String hostServiceStr = "a.b.com:service";
String hostAddressStr = "1.2.3.4";
String dnsStr = "a.b.com";
try {
HostName host = new HostName(hostPortStr);
InetSocketAddress socketAddress = host.asInetSocketAddress();
// use socket address
host = new HostName(hostServiceStr);
socketAddress = host.asInetSocketAddress(
service -> service.equals("service") ? 100 : null);
// use socket address
host = new HostName(hostAddressStr);
IPAddress address = host.asAddress(); // does not resolve
// use address
host = new HostName(dnsStr);
address = host.toAddress(); // resolves if necessary
// use address
} catch (HostNameException | UnknownHostException e) {
String msg = e.getMessage();
// handle improperly formatted host name or address string
}
starting with address or subnet strings, using exceptions for invalid formats
val ipv6Str = "a:b:c:d::a:b/64"
try {
val ipv6AddressStr = IPAddressString(ipv6Str)
val ipv6Addr = ipv6AddressStr.toAddress()
// use address
println(ipv6Addr) // a:b:c:d::a:b/64
} catch(e: AddressStringException) {
// handle improperly formatted address string
println(e.message)
}
starting with address or subnet strings, using nullable types and safe calls to handle invalid or unexpected formats
val ipv6v4Str = "a:b:c:d:e:f:1.2.3.4/112"
val ipv6v4AddressStr = IPAddressString(ipv6v4Str)
val ipAddr: IPAddress? = ipv6v4AddressStr.address
println(ipAddr) // a:b:c:d:e:f:102:304/112
val ipv4Addr = ipAddr?.toIPv6()?.embeddedIPv4Address
println(ipv4Addr) // 1.2.3.4/16
starting with address strings, using exceptions for invalid formats
import scala.util.{Failure, Success, Try}
val addressStr = new IPAddressString("a:b:c:d::/64")
Try(addressStr.toAddress) match {
case Success(userInfo) =>
// use address
case Failure(exception: AddressStringException) =>
// handle improperly formatted address string
}
starting with address or subnet strings, using exceptions for invalid formats
def addressStr = new IPAddressString('a:b:c:d:e:f:1.2.3.4')
try {
def address = addressStr.toAddress()
// use address
} catch (AddressStringException e) {
// handle improperly formatted address string
}
starting with address or subnet strings, checking for null for invalid formats
def subnetStr = new IPAddressString('108.30-31.*.*')
def subnet = subnetStr.getAddress()
if(subnet != null) {
// use address
}