public static boolean isSameNetwork(
Inet6Address addr1, Inet6Address addr2, int prefixLength
) {
if (prefixLength < 0 || prefixLength > 128) {
throw new IllegalArgumentException(
"Invalid prefix length: " + prefixLength
);
}
if (prefixLength <= 64) {
long addr1Bitmap = ByteArrays.getLong(addr1.getAddress(), 0);
long addr2Bitmap = ByteArrays.getLong(addr2.getAddress(), 0);
return isSamePrefix(addr1Bitmap, addr2Bitmap, prefixLength);
}
else {
long addr1Bitmap1h = ByteArrays.getLong(addr1.getAddress(), 0);
long addr2Bitmap1h = ByteArrays.getLong(addr2.getAddress(), 0);
if (addr1Bitmap1h != addr2Bitmap1h) {
return false;
}
long addr1Bitmap2h = ByteArrays.getLong(addr1.getAddress(), 16);
long addr2Bitmap2h = ByteArrays.getLong(addr2.getAddress(), 16);
return isSamePrefix(addr1Bitmap2h, addr2Bitmap2h, prefixLength - 64);
}
}