Class EthernetAddress

java.lang.Object
com.fasterxml.uuid.EthernetAddress
All Implemented Interfaces:
Serializable, Cloneable, Comparable<EthernetAddress>

public class EthernetAddress extends Object implements Serializable, Cloneable, Comparable<EthernetAddress>
EthernetAddress encapsulates the 6-byte MAC address defined in IEEE 802.1 standard.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final long
    48-bit MAC address, stored in 6 lowest-significant bytes (in big endian notation)
    protected static Random
    We may need a random number generator, for creating dummy ethernet address if no real interface is found.
  • Constructor Summary

    Constructors
    Constructor
    Description
    EthernetAddress(byte[] addr)
    Binary constructor that constructs an instance given the 6 byte (48-bit) address.
    EthernetAddress(long addr)
    Another binary constructor; constructs an instance from the given long argument; the lowest 6 bytes contain the address.
    String constructor; given a 'standard' ethernet MAC address string (like '00:C0:F0:3D:5B:7C'), constructs an EthernetAddress instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static Random
    Helper method for accessing configured random number generator
    byte[]
    Returns 6 byte byte array that contains the binary representation of this ethernet address; byte 0 is the most significant byte (and so forth)
    Default cloning behaviour (bitwise copy) is just fine...
    int
    Method that compares this EthernetAddress to one passed in as argument.
    Factory method that can be used to construct a random multicast address; to be used in cases where there is no "real" ethernet address to use.
    Factory method that can be used to construct a random multicast address; to be used in cases where there is no "real" ethernet address to use.
    boolean
     
    A factory method that will try to determine the ethernet address of the network interface that connects to the default network gateway.
    Factory method that locates a network interface that has a suitable mac address (ethernet cards, and things that emulate one), and return that address.
    A factory method to return the ethernet address of a specified network interface.
    Factory method that locates a network interface that has a suitable mac address (ethernet cards, and things that emulate one), and return that address.
    int
     
    boolean
    Method that can be used to check if this address refers to a "locally administered address" (see [http://en.wikipedia.org/wiki/MAC_address] for details).
    boolean
    Method that can be used to check if this address refers to a multicast address.
    byte[]
    Synonym to 'asByteArray()'
    void
    toByteArray(byte[] array)
     
    void
    toByteArray(byte[] array, int pos)
     
    long
     
    Returns the canonical string representation of this ethernet address.
    valueOf(byte[] addr)
    Constructs a new EthernetAddress given the byte array that contains binary representation of the address.
    valueOf(int[] addr)
    Constructs a new EthernetAddress given the byte array that contains binary representation of the address.
    valueOf(long addr)
    Constructs a new EthernetAddress given the long int value (64-bit) representation of the ethernet address (of which 48 LSB contain the definition) Note that calling this method returns the same result as would using the matching constructor.
    valueOf(String addrStr)
    Constructs a new EthernetAddress given a string representation of the ethernet address.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • _rnd

      protected static Random _rnd
      We may need a random number generator, for creating dummy ethernet address if no real interface is found.
    • _address

      protected final long _address
      48-bit MAC address, stored in 6 lowest-significant bytes (in big endian notation)
  • Constructor Details

    • EthernetAddress

      public EthernetAddress(String addrStr) throws NumberFormatException
      String constructor; given a 'standard' ethernet MAC address string (like '00:C0:F0:3D:5B:7C'), constructs an EthernetAddress instance. Note that string is case-insensitive, and also that leading zeroes may be omitted. Thus '00:C0:F0:3D:5B:7C' and '0:c0:f0:3d:5b:7c' are equivalent, and a 'null' address could be passed as ':::::' as well as '00:00:00:00:00:00' (or any other intermediate combination).
      Parameters:
      addrStr - String representation of the ethernet address
      Throws:
      NumberFormatException
    • EthernetAddress

      public EthernetAddress(byte[] addr) throws NumberFormatException
      Binary constructor that constructs an instance given the 6 byte (48-bit) address. Useful if an address is saved in binary format (for saving space for example).
      Throws:
      NumberFormatException
    • EthernetAddress

      public EthernetAddress(long addr)
      Another binary constructor; constructs an instance from the given long argument; the lowest 6 bytes contain the address.
      Parameters:
      addr - long that contains the MAC address in 6 least significant bytes.
  • Method Details

    • clone

      public Object clone()
      Default cloning behaviour (bitwise copy) is just fine...
      Overrides:
      clone in class Object
    • valueOf

      public static EthernetAddress valueOf(byte[] addr) throws NumberFormatException
      Constructs a new EthernetAddress given the byte array that contains binary representation of the address. Note that calling this method returns the same result as would using the matching constructor.
      Parameters:
      addr - Binary representation of the ethernet address
      Throws:
      NumberFormatException - if addr is invalid (less or more than 6 bytes in array)
    • valueOf

      public static EthernetAddress valueOf(int[] addr) throws NumberFormatException
      Constructs a new EthernetAddress given the byte array that contains binary representation of the address. Note that calling this method returns the same result as would using the matching constructor.
      Parameters:
      addr - Binary representation of the ethernet address
      Throws:
      NumberFormatException - if addr is invalid (less or more than 6 ints in array)
    • valueOf

      public static EthernetAddress valueOf(String addrStr) throws NumberFormatException
      Constructs a new EthernetAddress given a string representation of the ethernet address. Note that calling this method returns the same result as would using the matching constructor.
      Parameters:
      addrStr - String representation of the ethernet address
      Throws:
      NumberFormatException - if addr representation is invalid
    • valueOf

      public static EthernetAddress valueOf(long addr)
      Constructs a new EthernetAddress given the long int value (64-bit) representation of the ethernet address (of which 48 LSB contain the definition) Note that calling this method returns the same result as would using the matching constructor.
      Parameters:
      addr - Long int representation of the ethernet address
    • fromInterface

      public static EthernetAddress fromInterface()
      Factory method that locates a network interface that has a suitable mac address (ethernet cards, and things that emulate one), and return that address. If there are multiple applicable interfaces, one of them is returned; which one is returned is not specified. Method is meant for accessing an address needed to construct generator for time+location based UUID generation method.
      Returns:
      Ethernet address of one of interfaces system has; not including local or loopback addresses; if one exists, null if no such interfaces are found.
    • fromInterface

      public static EthernetAddress fromInterface(NetworkInterface nint)
      A factory method to return the ethernet address of a specified network interface.
      Since:
      4.2
    • fromPreferredInterface

      public static EthernetAddress fromPreferredInterface()
      Factory method that locates a network interface that has a suitable mac address (ethernet cards, and things that emulate one), and return that address. It will first try to identify an egress interface, and failing that, it will select indeterminately from all non-loopback interfaces found. Method is meant for accessing an address needed to construct generator for time+location based UUID generation method.
      Returns:
      Ethernet address of one of interfaces system has; not including local or loopback addresses; if one exists, null if no such interfaces are found.
      Since:
      4.2
    • fromEgressInterface

      public static EthernetAddress fromEgressInterface()
      A factory method that will try to determine the ethernet address of the network interface that connects to the default network gateway. To do this it will try to open a connection to one of the root DNS servers, or barring that, to address 1.1.1.1, or finally if that also fails then to IPv6 address "1::1". If a connection can be opened then the interface through which that connection is routed is determined to be the default egress interface, and the corresponding address of that interface will be returned. If all attempts are unsuccessful, null will be returned.
      Since:
      4.2
    • constructMulticastAddress

      public static EthernetAddress constructMulticastAddress()
      Factory method that can be used to construct a random multicast address; to be used in cases where there is no "real" ethernet address to use. Address to generate should be a multicast address to avoid accidental collision with real manufacturer-assigned MAC addresses.

      Internally a SecureRandom instance is used for generating random number to base address on.

    • constructMulticastAddress

      public static EthernetAddress constructMulticastAddress(Random rnd)
      Factory method that can be used to construct a random multicast address; to be used in cases where there is no "real" ethernet address to use. Address to generate should be a multicast address to avoid accidental collision with real manufacturer-assigned MAC addresses.

      Address is created using specified random number generator.

    • asByteArray

      public byte[] asByteArray()
      Returns 6 byte byte array that contains the binary representation of this ethernet address; byte 0 is the most significant byte (and so forth)
      Returns:
      6 byte byte array that contains the binary representation
    • toByteArray

      public byte[] toByteArray()
      Synonym to 'asByteArray()'
      Returns:
      6 byte byte array that contains the binary representation
    • toByteArray

      public void toByteArray(byte[] array)
    • toByteArray

      public void toByteArray(byte[] array, int pos)
    • toLong

      public long toLong()
    • isMulticastAddress

      public boolean isMulticastAddress()
      Method that can be used to check if this address refers to a multicast address. Such addresses are never assigned to individual network cards.
    • isLocallyAdministeredAddress

      public boolean isLocallyAdministeredAddress()
      Method that can be used to check if this address refers to a "locally administered address" (see [http://en.wikipedia.org/wiki/MAC_address] for details). Such addresses are not assigned to individual network cards.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(EthernetAddress other)
      Method that compares this EthernetAddress to one passed in as argument. Comparison is done simply by comparing individual address bytes in the order.
      Specified by:
      compareTo in interface Comparable<EthernetAddress>
      Returns:
      negative number if this EthernetAddress should be sorted before the parameter address if they are equal, os positive non-zero number if this address should be sorted after parameter
    • toString

      public String toString()
      Returns the canonical string representation of this ethernet address. Canonical means that all characters are lower-case and string length is always 17 characters (ie. leading zeroes are not omitted).
      Overrides:
      toString in class Object
      Returns:
      Canonical string representation of this ethernet address.
    • _randomNumberGenerator

      protected static Random _randomNumberGenerator()
      Helper method for accessing configured random number generator