Java InetAddress to toIntArray(InetAddress addr)

Here you can find the source of toIntArray(InetAddress addr)

Description

Convert an IP address int array.

License

Apache License

Parameter

Parameter Description
addr the source address to convert.

Return

an array of int values, or null if the source could not be parsed.

Declaration

public static int[] toIntArray(InetAddress addr) 

Method Source Code

//package com.java2s;
/*//from   ww  w . j ava 2  s  .co  m
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.*;

import java.util.regex.Pattern;

public class Main {
    /**
     * Convert an IP address int array.
     * @param addr the source address to convert.
     * @return an array of int values, or null if the source could not be parsed.
     */
    public static int[] toIntArray(InetAddress addr) {
        try {
            byte[] bytes = addr.getAddress();
            String ip = addr.getHostAddress();
            int[] result = null;
            if (addr instanceof Inet6Address) {
                result = new int[8];
                String[] comp = ip.split(":");
                for (int i = 0; i < comp.length; i++)
                    result[i] = Integer
                            .decode("0x" + comp[i].toLowerCase());
            } else {
                result = new int[8];
                String[] comp = ip.split("\\.");
                for (int i = 0; i < comp.length; i++)
                    result[i] = Integer.valueOf(comp[i]);
            }
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Convert a string with <code>separator</code>-separated values into an int array.
     * @param source the source string to convert.
     * @param separatorPattern the values separator, expressed as a regular expression, must comply with the specifications for {@link java.util.regex.Pattern}.
     * @return an array of int value, or null if the source cvould not be parsed.
     */
    public static int[] toIntArray(String source, Pattern separatorPattern) {
        try {
            String[] vals = separatorPattern.split(source);
            int[] result = new int[vals.length];
            for (int i = 0; i < vals.length; i++)
                result[i] = Integer.valueOf(vals[i]);
            return result;
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. ipv4AddressToInt(InetAddress addr)
  2. str(InetAddress address)
  3. toAddressString(InetAddress ip)
  4. toAddrString(InetAddress ip)
  5. toDotString(InetAddress ip)
  6. toIpAddrString(final InetAddress addr)
  7. toLong(InetAddress addr)
  8. toLong(InetAddress ip)