+
+ public static final byte[] convertIpv6ArbitraryMaskToByteArray(final Ipv6ArbitraryMask mask) {
+ String maskValue;
+ if (mask.getValue() != null) {
+ maskValue = mask.getValue();
+ } else {
+ maskValue = DEFAULT_IPV6_ARBITRARY_BITMASK;
+ }
+ InetAddress maskInIpFormat = null;
+ try {
+ maskInIpFormat = InetAddress.getByName(maskValue);
+ } catch (UnknownHostException e) {
+ LOG.error ("Failed to convert mask string to ipv6 format mask ",e);
+ }
+ return maskInIpFormat.getAddress();
+ }
+
+ public static boolean isIpv6ArbitraryBitMask(final byte[] byteMask) {
+ if (byteMask == null) {
+ return false;
+ } else {
+ ArrayList<Integer> integerMaskArrayList = new ArrayList<Integer>();
+ String maskInBits;
+ // converting byte array to bits
+ maskInBits = new BigInteger(1, byteMask).toString(2);
+ ArrayList<String> stringMaskArrayList = new ArrayList<String>(Arrays.asList(maskInBits.split("(?!^)")));
+ for (String string:stringMaskArrayList) {
+ integerMaskArrayList.add(Integer.parseInt(string));
+ }
+ return checkIpv6ArbitraryBitMask(integerMaskArrayList);
+ }
+ }
+
+ private static boolean checkIpv6ArbitraryBitMask(final ArrayList<Integer> arrayList) {
+ // checks 0*1* case - Leading zeros in arrayList are truncated
+ if (arrayList.size() > 0 && arrayList.size() < IPV6_ADDRESS_LENGTH) {
+ return true;
+ } else {
+ //checks 1*0*1 case
+ for (int i=0; i<arrayList.size()-1;i++) {
+ if (arrayList.get(i) ==0 && arrayList.get(i+1) == 1) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ public static String compressedIpv6Format(final String ipv6Address) {
+ String compressedIpv6Address;
+ compressedIpv6Address = ipv6Address.replaceAll("((?::0+\\b){2,}):?(?!\\S*\\b\\1:0\\b)(\\S*)", "::$2");
+ return compressedIpv6Address;
+ }
+
+ public static Ipv6ArbitraryMask compressedIpv6MaskFormat(final Ipv6ArbitraryMask ipv6Mask) {
+ String stringIpv6Mask = ipv6Mask.getValue();
+ return new Ipv6ArbitraryMask(compressedIpv6Format(stringIpv6Mask));
+ }