Java Network Interface Get getCustomMACFormat(NetworkInterface inte)

Here you can find the source of getCustomMACFormat(NetworkInterface inte)

Description

This method returns the MAC address of Network Interface.

License

Open Source License

Parameter

Parameter Description
inte NetworkInterface to retrieve MAC address.

Exception

Parameter Description
SocketException if is not possible to retries MAC address as byte.
IllegalArgumentException if argument is null.

Return

of formatted MAC address.

Declaration

public static String getCustomMACFormat(NetworkInterface inte)
        throws SocketException, IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.NetworkInterface;
import java.net.SocketException;

public class Main {
    /**//from  w w  w  . ja v  a  2 s.c  o m
     * This method returns the MAC address of Network Interface.
     * @param inte {@link NetworkInterface} to retrieve MAC address.
     * @return {@link String} of formatted MAC address.
     * @throws SocketException if is not possible to retries MAC address as byte.
     * @throws IllegalArgumentException if argument is null.
     */
    public static String getCustomMACFormat(NetworkInterface inte)
            throws SocketException, IllegalArgumentException {
        if (inte == null)
            throw new IllegalArgumentException("Network Interface can not be null.");

        byte[] mac = inte.getHardwareAddress();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++)
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
        return sb.toString();
    }
}

Related

  1. getAllInterfaces()
  2. getAllLocalInterfaces()
  3. getAllLocalUsingNetworkInterface()
  4. getAvailableNetworkInterface()
  5. getBroadcast()
  6. getDefaultInterface()
  7. getDefaultNetworkInterface()
  8. getFirstMACAdress()
  9. getFreeTunnelInterface()