Author: nextgens
Date: 2007-11-29 20:10:52 +0000 (Thu, 29 Nov 2007)
New Revision: 16105
Modified:
trunk/freenet/src/freenet/io/AddressIdentifier.java
Log:
Improve AddressIdentifier compiling the regexp
Modified: trunk/freenet/src/freenet/io/AddressIdentifier.java
===================================================================
--- trunk/freenet/src/freenet/io/AddressIdentifier.java 2007-11-29 19:59:32 UTC
(rev 16104)
+++ trunk/freenet/src/freenet/io/AddressIdentifier.java 2007-11-29 20:10:52 UTC
(rev 16105)
@@ -31,6 +31,18 @@
* @version $Id$
*/
public class AddressIdentifier {
+ public static final Pattern ipv4Pattern, ipv6Pattern,
ipv6PatternWithPercentScopeID;
+
+ static {
+ String byteRegex = "([01]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])";
+ String ipv4AddressRegex = byteRegex + "\\.(" + byteRegex +
"\\.)?(" + byteRegex + "\\.)?" + byteRegex;
+ ipv4Pattern = Pattern.compile(ipv4AddressRegex);
+
+ String wordRegex = "([0-9a-fA-F]{1,4})";
+ String ipv6AddressRegex = wordRegex + "?:" + wordRegex + ':' +
wordRegex + ':' + wordRegex + ':' + wordRegex + ':' + wordRegex + ':' +
wordRegex + ':' + wordRegex;
+ ipv6Pattern = Pattern.compile(ipv6AddressRegex);
+ ipv6PatternWithPercentScopeID =
Pattern.compile(ipv6AddressRegex + "(?:%[0-9]{1,3})?");
+ }
public static class AddressType {
@@ -75,17 +87,9 @@
* otherwise
*/
public static AddressType getAddressType(String address, boolean
allowIPv6PercentScopeID) {
- String byteRegex = "([01]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])";
- String ipv4AddressRegex = byteRegex + "\\.(" + byteRegex +
"\\.)?(" + byteRegex + "\\.)?" + byteRegex;
- if (Pattern.matches(ipv4AddressRegex, address)) {
+ if (ipv4Pattern.matcher(address).matches()) {
return AddressType.IPv4;
- }
- String wordRegex = "([0-9a-fA-F]{1,4})";
- String ipv6AddressRegex = wordRegex + "?:" + wordRegex + ':' +
wordRegex + ':' + wordRegex + ':' + wordRegex + ':' + wordRegex + ':' +
wordRegex + ':' + wordRegex;
- if (allowIPv6PercentScopeID) {
- ipv6AddressRegex = ipv6AddressRegex +
"(?:%[0-9]{1,3})?";
- }
- if (Pattern.matches(ipv6AddressRegex, address)) {
+ }else if ((allowIPv6PercentScopeID ?
ipv6PatternWithPercentScopeID : ipv6Pattern).matcher(address).matches()) {
return AddressType.IPv6;
}
return AddressType.OTHER;