程式扎記: [ Java 代碼範本 ] Determine if the given string is a valid IPv4 or IPv6 address.

標籤

2015年1月30日 星期五

[ Java 代碼範本 ] Determine if the given string is a valid IPv4 or IPv6 address.

Source From Here
- Sample Code (1)
  1. /** 
  2. * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999. 
  3. * 
  4. * This program is free software; you can redistribute it and/or modify 
  5. * it under the terms of the latest version of the GNU Lesser General 
  6. * Public License as published by the Free Software Foundation; 
  7. * 
  8. * This program is distributed in the hope that it will be useful, 
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  11. * GNU Lesser General Public License for more details. 
  12. * 
  13. * You should have received a copy of the GNU Lesser General Public License 
  14. * along with this program (LICENSE.txt); if not, write to the Free Software 
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
  16. */  
  17. //package org.jamwiki.utils;  
  18.   
  19. import java.io.File;  
  20. import java.io.FileNotFoundException;  
  21. import java.io.IOException;  
  22. import java.io.UnsupportedEncodingException;  
  23. import java.lang.reflect.Constructor;  
  24. import java.lang.reflect.InvocationTargetException;  
  25. import java.net.URL;  
  26. import java.net.URLDecoder;  
  27. import java.net.URLEncoder;  
  28. import java.text.MessageFormat;  
  29. import java.util.Arrays;  
  30. import java.util.HashMap;  
  31. import java.util.Iterator;  
  32. import java.util.List;  
  33. import java.util.Locale;  
  34. import java.util.Map;  
  35. import java.util.ResourceBundle;  
  36. import java.util.regex.Matcher;  
  37. import java.util.regex.Pattern;  
  38. import java.util.regex.PatternSyntaxException;  
  39. /** 
  40. * This class provides a variety of basic utility methods that are not 
  41. * dependent on any other classes within the org.jamwiki package structure. 
  42. */  
  43. public class Utilities {  
  44.   private static Pattern VALID_IPV4_PATTERN = null;  
  45.   private static Pattern VALID_IPV6_PATTERN = null;  
  46.   private static final String ipv4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";  
  47.   private static final String ipv6Pattern = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";  
  48.   
  49.   static {  
  50.     try {  
  51.       VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE);  
  52.       VALID_IPV6_PATTERN = Pattern.compile(ipv6Pattern, Pattern.CASE_INSENSITIVE);  
  53.     } catch (PatternSyntaxException e) {  
  54.       //logger.severe("Unable to compile pattern", e);  
  55.     }  
  56.   }  
  57.   
  58.   /** 
  59.    * Determine if the given string is a valid IPv4 or IPv6 address.  This method 
  60.    * uses pattern matching to see if the given string could be a valid IP address. 
  61.    * 
  62.    * @param ipAddress A string that is to be examined to verify whether or not 
  63.    *  it could be a valid IP address. 
  64.    * @return true if the string is a value that is a valid IP address, 
  65.    *  false otherwise. 
  66.    */  
  67.   public static boolean isIpAddress(String ipAddress) {  
  68.   
  69.     Matcher m1 = Utilities.VALID_IPV4_PATTERN.matcher(ipAddress);  
  70.     if (m1.matches()) {  
  71.       return true;  
  72.     }  
  73.     Matcher m2 = Utilities.VALID_IPV6_PATTERN.matcher(ipAddress);  
  74.     return m2.matches();  
  75.   }  
  76. }  
- Sample Code In Groovy (2)
  1. enum IF{IPv4, IPv6}  
  2. public IF isIpAddress(String ipAddress)  
  3. {  
  4.     try  
  5.     {  
  6.         InetAddress addr = InetAddress.getByName(ipAddress)  
  7.         if(addr.class.name.equals("java.net.Inet4Address")) return IF.IPv4  
  8.         else if(addr.class.name.equals("java.net.Inet6Address")) return IF.IPv6  
  9.     }  
  10.     catch(Exception e){}  
  11.     return null  
  12. }  
  13.   
  14. for(ipAddr in ["1.2.3.4",  
  15.                "2001:db8:0:1:1:1:1:1",  
  16.                "abc"])  
  17. {  
  18.     def type = isIpAddress(ipAddr)  
  19.     printf("%s is %s\n", ipAddr, type!=null?type.toString():"invalid")  
  20. }  
Execution Result:
1.2.3.4 is IPv4
2001:db8:0:1:1:1:1:1 is IPv6
abc is invalid


沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!