Java InetAddress Create getInetAddress(final int[] octets, final int offset, final int length)

Here you can find the source of getInetAddress(final int[] octets, final int offset, final int length)

Description

getInetAddress

License

Open Source License

Parameter

Parameter Description
ipAddrOctets an array of byte.

Return

a object.

Declaration

public static InetAddress getInetAddress(final int[] octets, final int offset, final int length) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of OpenNMS(R).// w  ww.j a v a  2 s .  co  m
 *
 * Copyright (C) 2007-2012 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) 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.
 *
 * OpenNMS(R) 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 OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    /**
     * <p>getInetAddress</p>
     *
     * @param ipAddrOctets an array of byte.
     * @return a {@link java.net.InetAddress} object.
     */
    public static InetAddress getInetAddress(final int[] octets, final int offset, final int length) {
        final byte[] addressBytes = new byte[length];
        for (int i = 0; i < addressBytes.length; i++) {
            addressBytes[i] = Integer.valueOf(octets[i + offset]).byteValue();
        }
        return getInetAddress(addressBytes);
    }

    /**
     * <p>getInetAddress</p>
     *
     * @param ipAddrOctets an array of byte.
     * @return a {@link java.net.InetAddress} object.
     */
    public static InetAddress getInetAddress(final byte[] ipAddrOctets) {
        try {
            return InetAddress.getByAddress(ipAddrOctets);
        } catch (final UnknownHostException e) {
            throw new IllegalArgumentException(
                    "Invalid IPAddress " + ipAddrOctets + " with length " + ipAddrOctets.length);
        }

    }

    /**
     * <p>getInetAddress</p>
     *
     * @param dottedNotation a {@link java.lang.String} object.
     * @return a {@link java.net.InetAddress} object.
     */
    public static InetAddress getInetAddress(final String dottedNotation) {
        try {
            return InetAddress.getByName(dottedNotation);
        } catch (final UnknownHostException e) {
            throw new IllegalArgumentException("Invalid IPAddress " + dottedNotation);
        }
    }
}

Related

  1. getHostNameWithoutDomain(final InetAddress addr)
  2. getInetAddress()
  3. getInetAddress()
  4. getInetAddress()
  5. getInetAddress()
  6. getInetAddress(final String addressOrName)
  7. getInetAddress(long ip)
  8. getInetAddress(Socket socket)
  9. getInetAddress(String dottedNotation)