From 47130c05897d0d68310075a63dd6d81d6b36d49e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Sat, 21 Jan 2017 22:42:25 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E6=8F=90=E5=8D=87IPAddress=E7=B1=BB?= =?UTF-8?q?=E4=B8=ADparseIPAddress=E6=96=B9=E6=B3=95=E6=80=A7=E8=83=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../druid/support/http/util/IPAddress.java | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/alibaba/druid/support/http/util/IPAddress.java b/src/main/java/com/alibaba/druid/support/http/util/IPAddress.java index 9443d20f91..f2a64add09 100644 --- a/src/main/java/com/alibaba/druid/support/http/util/IPAddress.java +++ b/src/main/java/com/alibaba/druid/support/http/util/IPAddress.java @@ -15,6 +15,7 @@ */ package com.alibaba.druid.support.http.util; +import java.util.NoSuchElementException; /** * @author Marcel Dullaart @@ -121,42 +122,50 @@ public class IPAddress implements Cloneable { } try { - int len = ipAddressStr.length(); - // temp value - int temp = 0; + String tmp = ipAddressStr; + // get the 3 first numbers int offset = 0; - for (int i = 0; i < len; i++) { - char c = ipAddressStr.charAt(i); - switch (c) { - case '.': - if (temp < 0 || temp > 255) { + for (int i = 0; i < 3; i++) { + + // get the position of the first dot + int index = tmp.indexOf('.'); + + // if there is not a dot then the ip string representation is + // not compliant to the decimal-dotted notation. + if (index != -1) { + + // get the number before the dot and convert it into + // an integer. + String numberStr = tmp.substring(0, index); + int number = Integer.parseInt(numberStr); + if ((number < 0) || (number > 255)) { throw new IllegalArgumentException("Invalid IP Address [" + ipAddressStr + "]"); } - result += temp << offset; - temp = 0; + + result += number << offset; offset += 8; - break; - default: - // char to num - int num = c - '0'; - if (num > -1 && num < 10) { - temp = (temp * 10) + num; - } else { - throw new IllegalArgumentException("Invalid IP Address [" + ipAddressStr + "]"); - } - break; + tmp = tmp.substring(index + 1); + } else { + throw new IllegalArgumentException("Invalid IP Address [" + ipAddressStr + "]"); } } - // IP address must contain three dot - if (offset == 24 && temp > 0 && temp < 256) { - result += temp << offset; + // the remaining part of the string should be the last number. + if (tmp.length() > 0) { + int number = Integer.parseInt(tmp); + if ((number < 0) || (number > 255)) { + throw new IllegalArgumentException("Invalid IP Address [" + ipAddressStr + "]"); + } + + result += number << offset; ipAddress = result; } else { throw new IllegalArgumentException("Invalid IP Address [" + ipAddressStr + "]"); } - } catch (Exception ex) { + } catch (NoSuchElementException ex) { + throw new IllegalArgumentException("Invalid IP Address [" + ipAddressStr + "]", ex); + } catch (NumberFormatException ex) { throw new IllegalArgumentException("Invalid IP Address [" + ipAddressStr + "]", ex); }