+
+ public static final byte[] convertArbitraryMaskToByteArray(DottedQuad mask) {
+ String maskValue;
+ if (mask != null && mask.getValue() != null){
+ maskValue = mask.getValue();
+ }
+ else {
+ maskValue = DEFAULT_ARBITRARY_BIT_MASK;
+ }
+ InetAddress maskInIpFormat = null;
+ try {
+ maskInIpFormat = InetAddress.getByName(maskValue);
+ } catch (UnknownHostException e) {
+ LOG.error ("Failed to resolve the ip address of the mask",e);
+ }
+ byte[] bytes = maskInIpFormat.getAddress();
+ return bytes;
+ }
+
+ public static boolean isArbitraryBitMask(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 checkArbitraryBitMask(integerMaskArrayList);
+ }
+ }
+
+ private static boolean checkArbitraryBitMask(ArrayList<Integer> arrayList) {
+ // checks 0*1* case - Leading zeros in arrayList are truncated
+ if(arrayList.size()>0 && arrayList.size()<32) {
+ return true;
+ }
+ //checks 1*0*1 case
+ else {
+ for(int i=0; i<arrayList.size()-1;i++) {
+ if(arrayList.get(i) ==0 && arrayList.get(i+1) == 1) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }