/** * Create an IPv6 address from a java.net.Inet6Address. * * @param inetAddress Inet6Address representation * @return IPv6 address */ public static IPv6Address fromInetAddress(final InetAddress inetAddress) { if (inetAddress == null) throw new IllegalArgumentException("can not construct from [null]"); return fromByteArray(inetAddress.getAddress()); }
/** * Create an IPv6 address from a java.net.Inet6Address. * * @param inetAddress Inet6Address representation * @return IPv6 address */ public static IPv6Address fromInetAddress(final InetAddress inetAddress) { if (inetAddress == null) throw new IllegalArgumentException("can not construct from [null]"); return fromByteArray(inetAddress.getAddress()); }
/** * Create an IPv6 address from a (positive) {@link java.math.BigInteger}. The magnitude of the {@link java.math.BigInteger} represents * the IPv6 address value. Or in other words, the {@link java.math.BigInteger} with value N defines the Nth possible IPv6 address. * * @param bigInteger {@link java.math.BigInteger} value * @return IPv6 address */ public static IPv6Address fromBigInteger(final BigInteger bigInteger) { if (bigInteger == null) throw new IllegalArgumentException("can not construct from [null]"); if (bigInteger.compareTo(BigInteger.ZERO) < 0) throw new IllegalArgumentException("can not construct from negative value"); if (bigInteger.compareTo(MAX.toBigInteger()) > 0) throw new IllegalArgumentException("bigInteger represents a value bigger than 2^128 - 1"); byte[] bytes = bigInteger.toByteArray(); if (bytes[0] == 0) { // a zero byte was added to represent the (always positive, hence zero) sign bit return fromByteArray(prefixWithZeroBytes(Arrays.copyOfRange(bytes, 1, bytes.length), N_BYTES)); } else { return fromByteArray(prefixWithZeroBytes(bytes, N_BYTES)); } }
/** * Create an IPv6 address from a (positive) {@link java.math.BigInteger}. The magnitude of the {@link java.math.BigInteger} represents * the IPv6 address value. Or in other words, the {@link java.math.BigInteger} with value N defines the Nth possible IPv6 address. * * @param bigInteger {@link java.math.BigInteger} value * @return IPv6 address */ public static IPv6Address fromBigInteger(final BigInteger bigInteger) { if (bigInteger == null) throw new IllegalArgumentException("can not construct from [null]"); if (bigInteger.compareTo(BigInteger.ZERO) < 0) throw new IllegalArgumentException("can not construct from negative value"); if (bigInteger.compareTo(MAX.toBigInteger()) > 0) throw new IllegalArgumentException("bigInteger represents a value bigger than 2^128 - 1"); byte[] bytes = bigInteger.toByteArray(); if (bytes[0] == 0) { // a zero byte was added to represent the (always positive, hence zero) sign bit return fromByteArray(prefixWithZeroBytes(Arrays.copyOfRange(bytes, 1, bytes.length), N_BYTES)); } else { return fromByteArray(prefixWithZeroBytes(bytes, N_BYTES)); } }