A good Java library for network math [closed]

I'm looking for a Java library that is geared towards network math and already tested. Nothing particularly fancy, just something to hold ips and subnets, and do things like print a subnet mask or calculate whether an IP is within a given subnet.

Should I roll my own, or is there already a robust library for this?

Asked by: Caroline649 | Posted: 23-01-2022

Answer 1

We developed a Java IPv4 arithmetic library ourselves. See it here: http://tufar.com/ipcalculator/ It is under BSD license.

Answered by: Chester410 | Posted: 24-02-2022

Answer 2

org.apache.lenya.ac.IPRange appears to have these features.

The Apache Lenya project is an open-source content management system. It uses the Apache License, so you may be able to reuse just the code you need. (But as always, read the license yourself; don't trust legal advice from some guy on the internet! :-)

Answered by: Julian193 | Posted: 24-02-2022

Answer 3

The open-source IPAddress Java library can do ip address manipulation such as conversion to/from ipv4/ipv6 and subnet checking. Disclaimer: I am the project manager.

It handles various network math operations, such as masking, bitwise or, setting prefix lengths, switch address to prefix block, iterating through a subnet, checking containment, replacing address segments, reversing addresses, calculating subnet intersection, subtracting one subnet from another, and others.

Here is some example code for testing if an ipv6 address is in a given subnet:

    String ipv6 = "2001:db8:57AB:0000:0000:0000:0000:0001";
    String ipv6subnet = "2001:db8::/32";
    String ipv4 = "";
    try {
        IPAddressString ipv6addrstr = new IPAddressString(ipv6);
        IPAddressString ipv6addrsubnetstr = new IPAddressString(ipv6subnet);
        IPAddressString ipv4addrstr = new IPAddressString(ipv4);

        IPAddress ipv6addr = ipv6addrstr.toAddress();
        IPAddress ipv6addrsubnet = ipv6addrsubnetstr.toAddress();
        IPAddress ipv4mappedaddr = ipv4addrstr.toAddress().toIPv6();

        System.out.println(ipv6addrsubnet + " contains " + ipv6addr + ": " + ipv6addrsubnet.contains(ipv6addr)); //
        System.out.println(ipv6addrsubnet + " contains " + ipv4mappedaddr + ": " + ipv6addrsubnet.contains(ipv4mappedaddr)); //

    } catch(AddressStringException e) {
        //e.getMessage has validation error


2001:db8::/32 contains 2001:db8:57ab::1 is true
2001:db8::/32 contains ::ffff:102:304 is false

Answered by: Melissa812 | Posted: 24-02-2022

Similar questions

Java library for RMI on peers in a peer to peer network

I am looking for a p2p library for java which enables me to do a RMI on a peer node without sending other data over network than the method parameters. Required Features: peer-to-peer with automatic group discovery and joining automatic replication of shared objects (in case of a peer leaves the group) discover the nearest peer and execute a method on a shared object on this peer (wh...

java - Which library should I use to write an XLS from Linux / Python?

I'd love a good native Python library to write XLS, but it doesn't seem to exist. Happily, Jython does. So I'm trying to decide between jexcelapi and Apache HSSF: http://www.andykhan.com/jexcelapi/tutorial.html#writing http://poi.apache.org/hssf/quic...

How to browse for a file in java swing library?

I was wondering if there was some kind of J tool in the java swing library that opens up a file browser window and allows a user to choose a file. Then the ouput of the file would be the absolute path of the chosen file. Thanks in advance,

nlp - Fuzzy string search library in Java

Closed. This question does not meet Stack Overflow guid...

What is the best Java OXM library?

Even though I've been a developer for awhile I've been lucky enough to have avoided doing much work with XML. So now I've got a project where I've got to interact with some web services, and would like to use some kind of Object-to-XML Mapping solution. The only one I'm aware of is JAXB. Is that the best to go with? Are there any other recommendations? One catch - I'm stuck using Java 1.4, so I can't do an...

Java Chart library for OLAP

Closed. This question does not meet Stack Overflow guid...

Using a java library from python

I have a python app and java app. The python app generates input for the java app and invokes it on the command line. I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java. Any pointers? (FYI I'm v. new to Python) Clarification (at the cost of a long question: apologies) The py app (which I don't own) takes user input in the form of...

Is there a Java library to access the native Windows API?

Is there a Java library to access the native Windows API? Either with COM or JNI.

PDF to text tool or Java library?

java - How Can I Add the Apache POI Library in and Eclipse Project?

I download the Apache POI from apache.org but don't know how use it with one of my existing project in Eclipse.

Do you know of a Java library to access the native linux api?

Do you know of a Java library to access the native linux api? I guess something like this must use JNI. So be it.

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)