Java Mac Address Get getMacAddress()

Here you can find the source of getMacAddress()

Description

Grabs the mac address of computer and makes it 10 times longer

License

Minecraft Mod Public

Return

a byte array containing mac address

Declaration

public static byte[] getMacAddress() 

Method Source Code

//package com.java2s;

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

import java.util.Enumeration;

public class Main {
    private static byte[] cachedMacAddress;

    /**//from   w  w w  .  j  av a2 s.  c o  m
     * Grabs the mac address of computer and makes it 10 times longer
     *
     * @return a byte array containing mac address
     */
    public static byte[] getMacAddress() {
        if (cachedMacAddress != null && cachedMacAddress.length >= 10) {
            return cachedMacAddress;
        }
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface network = networkInterfaces.nextElement();
                byte[] mac = network.getHardwareAddress();
                if (mac != null && mac.length > 0) {
                    cachedMacAddress = new byte[mac.length * 10];
                    for (int i = 0; i < cachedMacAddress.length; i++) {
                        cachedMacAddress[i] = mac[i - (Math.round(i / mac.length) * mac.length)];
                    }
                    return cachedMacAddress;
                }
            }
        } catch (SocketException ignored) {
        }
        return new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    }
}

Related

  1. getMACAddress()
  2. getMacAddress()
  3. getMACAddress()
  4. getMacAddress()
  5. getMacAddress()
  6. getMacAddress()
  7. getMACAddress()
  8. getMacAddress()
  9. getMacAddress()