All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opsdatastore.util.NetworkAddressValidator Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.opsdatastore.util;

/*-
 * #%L
 * OpsDataStore SDK
 * %%
 * Copyright (C) 2017 OpsDataStore
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;

/**
 * Static utility class that contains method for validating and detecting ip4
 * and ip6
 * @author kguthrie
 */
public class NetworkAddressValidator {

    private static final Pattern macAddressFormat = Pattern.compile(
            "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");
   /**
    * determine if the given string is an ipv6 address
    * @param ip
    * @return
    */
   public static boolean isIPV6(String ip) {
       InetAddress address = null;
       try {
          address = InetAddress.getByName(ip);
       } catch (UnknownHostException e) {
           e.printStackTrace();
       }

       return address == null ? false : address instanceof Inet6Address;
   }

   /**
    * determine if the given string is an ipv4 address
    * @param ip
    * @return
    */
   public static boolean isIPV4(String ip) {
       InetAddress address = null;
       try {
           address = InetAddress.getByName(ip);
       } catch (UnknownHostException e) {
           e.printStackTrace();
       }
       return address == null ? false : address instanceof Inet4Address;
   }

   /**
    * determine if the given string represents a valid mac address
    * @param macAddress
    * @return
    */
   public static boolean isMacAddress(String macAddress) {
       return macAddressFormat.matcher(macAddress).matches();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy