Here you can find the source of getInetAddress(String str)
private static InetAddress getInetAddress(String str)
//package com.java2s; /*// w ww . j ava2 s . c o m * Copyright 2012-2013 James Geboski <jgeboski@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; public class Main { private static InetAddress getInetAddress(String str) { byte addr[]; addr = getNumericAddress4(str); if (addr == null) addr = getNumericAddress6(str); if (addr == null) return null; try { return InetAddress.getByAddress(new String(), addr); } catch (UnknownHostException e) { return null; } } private static byte[] getNumericAddress4(String str) { String ss[]; byte bs[]; int i; int v; ss = str.split("\\."); if (ss.length != 4) return null; bs = new byte[4]; try { for (i = 0; i < bs.length; i++) { v = Integer.parseInt(ss[i]); if ((v < 0) || (v > 255)) return null; bs[i] = (byte) v; } } catch (NumberFormatException e) { return null; } return bs; } private static byte[] getNumericAddress6(String str) { char cs[]; byte bs[]; int i; int c; int o; int val; int vch; boolean cold; cs = str.toCharArray(); bs = new byte[16]; cold = false; val = i = c = 0; if ((cs[0] == ':') && (cs[1] != ':')) return null; while ((i < cs.length) && (c < bs.length)) { if (cs[i] == ':') { cold = true; if (++i >= cs.length) return null; if (cs[i] != ':') continue; if (++i >= cs.length) return bs; o = i - 1; c += bs.length - (c + 2); while ((o = Arrays.binarySearch(cs, ++o, cs.length, ':')) >= 0) c += 2; if (c >= bs.length) return null; } while (i < cs.length) { vch = Character.digit(cs[i], 16); if (vch < 0) { if (cs[i] == ':') break; return null; } val <<= 4; val |= (byte) vch; i++; } if (val > 0xFFFF) return null; bs[c++] = (byte) ((val >> 8) & 0xFF); bs[c++] = (byte) (val & 0xFF); val = 0; } return cold ? bs : null; } }