Java Network Interface Get getNetworkInterfaceArray(String name)

Here you can find the source of getNetworkInterfaceArray(String name)

Description

Method that searches for and returns an array whose elements are all the network interface(s) that correspond to the specified name.

License

Open Source License

Parameter

Parameter Description
name <code>String</code> referencing the name to which the desired network interface(s) correspond.

Exception

Parameter Description
SocketException if there is an error in the underlyingI/O subsystem and/or protocol.
NullPointerException if <code>null</code> is input for<code>name</code>.

Return

an array whose elements are each instances of NetworkInterface[], in which each such instance corresponds to the given name, or null if there is no network interface corresponding to that name value. Note that if the value given for the name parameter is the String "all", then this method will return an array containing all of the network interfaces installed on the current node, regardless of each interface's name.

Declaration

public static NetworkInterface[] getNetworkInterfaceArray(String name)
        throws SocketException 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  .  ja  v a2 s .  c o  m*/

 Copyright (C) SYSTAP, LLC 2006-2008.  All rights reserved.

 Contact:
 SYSTAP, LLC
 4501 Tower Road
 Greensboro, NC 27410
 licenses@bigdata.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; version 2 of the License.

 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, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

import java.net.InetAddress;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;

import java.util.List;

public class Main {
    /**
     * Method that searches for and returns an array whose elements
     * are all the network interface(s) that correspond to the specified
     * <code>name</code>.
     *
     * @param name <code>String</code> referencing the name to which
     *             the desired network interface(s) correspond.
     *
     * @return an array whose elements are each instances of 
     *         <code>NetworkInterface[]</code>, in which each such
     *         instance corresponds to the given <code>name</code>,
     *         or <code>null</code> if there is no network interface
     *         corresponding to that name value.
     *         
     *         Note that if the value given for the <code>name</code> 
     *         parameter is the <code>String</code> "all", then this
     *         method will return an array containing all of the
     *         network interfaces installed on the current node, 
     *         regardless of each interface's name.
     *
     * @throws SocketException if there is an error in the underlying
     *         I/O subsystem and/or protocol.
     *
     * @throws NullPointerException if <code>null</code> is input for
     *         <code>name</code>.
     */
    public static NetworkInterface[] getNetworkInterfaceArray(String name)
            throws SocketException {
        NetworkInterface[] nics = null;
        if (name.equals("all")) {
            Enumeration en = NetworkInterface.getNetworkInterfaces();
            List nicList = (en != null) ? Collections.list(en)
                    : Collections.EMPTY_LIST;
            nics = (NetworkInterface[]) (nicList
                    .toArray(new NetworkInterface[nicList.size()]));
        } else {
            nics = new NetworkInterface[1];
            nics[0] = NetworkInterface.getByName(name);
            if (nics[0] == null) {
                // try to lookup by IP address
                InetAddress targetIp = null;
                try {
                    targetIp = InetAddress.getByName(name);
                    nics[0] = NetworkInterface.getByInetAddress(targetIp);
                } catch (UnknownHostException uhe) {
                    // ignore, return null
                }
            }
        }
        return nics;
    }
}

Related

  1. getMacBytes()
  2. getNetworkErrorMessage(Throwable e)
  3. getNetworkInterface()
  4. getNetworkInterface()
  5. getNetworkInterface(String name)
  6. getNetworkinterfaceByName(String name)
  7. getNetworkInterfaceInfo(NetworkInterface pNif)
  8. getNetworkInterfaces( Predicate predicate)
  9. getNetworkInterfaces()