| 21 | Address() {} |
| 22 | |
| 23 | private static byte [] |
| 24 | parseV4(String s) { |
| 25 | int numDigits; |
| 26 | int currentOctet; |
| 27 | byte [] values = new byte[4]; |
| 28 | int currentValue; |
| 29 | int length = s.length(); |
| 30 | |
| 31 | currentOctet = 0; |
| 32 | currentValue = 0; |
| 33 | numDigits = 0; |
| 34 | for (int i = 0; i < length; i++) { |
| 35 | char c = s.charAt(i); |
| 36 | if (c >= '0' && c <= '9') { |
| 37 | /* Can't have more than 3 digits per octet. */ |
| 38 | if (numDigits == 3) |
| 39 | return null; |
| 40 | /* Octets shouldn't start with 0, unless they are 0. */ |
| 41 | if (numDigits > 0 && currentValue == 0) |
| 42 | return null; |
| 43 | numDigits++; |
| 44 | currentValue *= 10; |
| 45 | currentValue += (c - '0'); |
| 46 | /* 255 is the maximum value for an octet. */ |
| 47 | if (currentValue > 255) |
| 48 | return null; |
| 49 | } else if (c == '.') { |
| 50 | /* Can't have more than 3 dots. */ |
| 51 | if (currentOctet == 3) |
| 52 | return null; |
| 53 | /* Two consecutive dots are bad. */ |
| 54 | if (numDigits == 0) |
| 55 | return null; |
| 56 | values[currentOctet++] = (byte) currentValue; |
| 57 | currentValue = 0; |
| 58 | numDigits = 0; |
| 59 | } else |
| 60 | return null; |
| 61 | } |
| 62 | /* Must have 4 octets. */ |
| 63 | if (currentOctet != 3) |
| 64 | return null; |
| 65 | /* The fourth octet can't be empty. */ |
| 66 | if (numDigits == 0) |
| 67 | return null; |
| 68 | values[currentOctet] = (byte) currentValue; |
| 69 | return values; |
| 70 | } |
| 71 | |
| 72 | private static byte [] |
| 73 | parseV6(String s) { |