Here you can find the source of get2428InetSocketAddress(String arg)
Parameter | Description |
---|---|
arg | a parameter |
public static InetSocketAddress get2428InetSocketAddress(String arg)
//package com.java2s; /**/*from w w w. java 2s . co m*/ * This file is part of Waarp Project. * * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the * COPYRIGHT.txt in the distribution for a full listing of individual contributors. * * All Waarp Project 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. * * Waarp 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 Waarp . If not, see * <http://www.gnu.org/licenses/>. */ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; public class Main { /** * Get the (RFC2428) InetSocketAddress corresponding to the FTP format of address (RFC2428) * * @param arg * @return the InetSocketAddress or null if an error occurs */ public static InetSocketAddress get2428InetSocketAddress(String arg) { // Format: #a#net-addr#tcp-port# where a = 1 IPV4 or 2 IPV6, other will // not be supported if (arg == null || arg.length() == 0) { // bad args return null; } String delim = arg.substring(0, 1); String[] infos = arg.split(delim); if (infos.length != 3) { // bad format return null; } boolean isIPV4 = true; if (infos[0].equals("1")) { isIPV4 = true; } else if (infos[0].equals("2")) { isIPV4 = false; } else { // not supported return null; } byte[] address = null; if (isIPV4) { // IPV4 address = new byte[4]; String[] elements = infos[1].split("."); if (elements.length != 4) { return null; } for (int i = 0; i < 4; i++) { try { address[i] = (byte) Integer.parseInt(elements[i]); } catch (NumberFormatException e) { return null; } } } else { // IPV6 address = new byte[16]; int[] value = new int[8]; String[] elements = infos[1].split(":"); if (elements.length != 8) { return null; } for (int i = 0, j = 0; i < 8; i++) { if (elements[i] == null || elements[i].length() == 0) { value[i] = 0; } else { try { value[i] = Integer.parseInt(elements[i]); } catch (NumberFormatException e) { return null; } } address[j++] = (byte) (value[i] >> 8); address[j++] = (byte) (value[i] & 0xFF); } } int port = 0; try { port = Integer.parseInt(infos[2]); } catch (NumberFormatException e) { return null; } InetAddress inetAddress; try { inetAddress = InetAddress.getByAddress(address); } catch (UnknownHostException e) { return null; } return new InetSocketAddress(inetAddress, port); } }