26import java.nio.ByteOrder;
27import java.util.Arrays;
29import org.jau.util.BasicTypes;
39 public static final EUI48Sub ANY_DEVICE =
new EUI48Sub(
new byte[] { (byte)0x00, (
byte)0x00, (byte)0x00, (
byte)0x00, (byte)0x00, (
byte)0x00 }, 0, 6, ByteOrder.LITTLE_ENDIAN );
41 public static final EUI48Sub ALL_DEVICE =
new EUI48Sub(
new byte[] { (byte)0xff, (
byte)0xff, (byte)0xff, (
byte)0xff, (byte)0xff, (
byte)0xff }, 0, 6, ByteOrder.LITTLE_ENDIAN );
43 public static final EUI48Sub LOCAL_DEVICE =
new EUI48Sub(
new byte[] { (byte)0x00, (
byte)0x00, (byte)0x00, (
byte)0xff, (byte)0xff, (
byte)0xff }, 0, 6, ByteOrder.LITTLE_ENDIAN );
48 public final byte b[];
50 private volatile int hash;
71 final int str_len = str.length();
75 errmsg.append(
"EUI48 sub-string must be less or equal length 17 but "+str.length()+
": "+str);
78 final byte b_[] =
new byte[ 6 ];
82 boolean exp_colon =
false;
83 while( j+1 < str_len ) {
84 final boolean is_colon =
':' == str.charAt(j);
85 if( exp_colon && !is_colon ) {
86 errmsg.append(
"EUI48Sub sub-string not in format '01:02:03:0A:0B:0C', but '"+str+
"', colon missing, pos "+j+
", len "+str_len);
88 }
else if( is_colon ) {
92 b_[len_] = Integer.valueOf(str.substring(j, j+2), 16).byteValue();
100 if( ByteOrder.LITTLE_ENDIAN == ByteOrder.nativeOrder() ) {
101 for(j=0; j<len_; ++j) {
102 dest.
b[j] = b_[len_-1-j];
105 System.arraycopy(b_, 0, dest.
b, 0, len_);
108 }
catch (
final NumberFormatException e) {
109 errmsg.append(
"EUI48 sub-string not in format '01:02:03:0A:0B:0C' but "+str+
", pos "+j+
", len "+str_len);
131 public EUI48Sub(
final String str)
throws IllegalArgumentException {
132 final StringBuilder errmsg =
new StringBuilder();
135 throw new IllegalArgumentException(errmsg.toString());
148 public EUI48Sub(
final byte stream[],
final int pos,
final int len_,
final ByteOrder byte_order) {
149 if( len_ >
EUI48.byte_size || pos + len_ > stream.length ) {
150 throw new IllegalArgumentException(
"EUI48 stream ( pos "+pos+
", len "+len_+
" > EUI48 size "+
EUI48.byte_size+
" or stream.length "+stream.length);
153 if( byte_order == ByteOrder.nativeOrder() ) {
154 System.arraycopy(stream, pos,
b, 0, len_);
162 public final boolean equals(
final Object obj) {
166 if (obj ==
null || !(obj instanceof
EUI48Sub)) {
169 final int length2 = ((
EUI48Sub)obj).length;
173 final byte[] b2 = ((
EUI48Sub)obj).b;
174 return Arrays.equals(
b, 0,
length, b2, 0, length2);
190 for(
int i=0; i<
length; i++) {
191 h = ( ( h << 5 ) - h ) +
b[i];
210 b[0] = 0;
b[1] = 0;
b[2] = 0;
211 b[3] = 0;
b[4] = 0;
b[5] = 0;
229 public static int indexOf(
final byte haystack_b[],
final int haystack_length,
230 final byte needle_b[],
final int needle_length,
231 final ByteOrder byte_order) {
232 if( 0 == needle_length ) {
235 if( haystack_length < needle_length ) {
238 final byte first = needle_b[0];
239 final int outerEnd = haystack_length - needle_length + 1;
241 for (
int i = 0; i < outerEnd; i++) {
243 while( haystack_b[i] != first ) {
244 if( ++i == outerEnd ) {
250 final int innerEnd = i + needle_length;
253 if( ++j == innerEnd ) {
255 if( ByteOrder.nativeOrder() == byte_order ) {
258 return 5 - i - ( needle_length - 1 );
261 }
while( haystack_b[j] == needle_b[++k] );
290 return 0 <=
indexOf(needle, ByteOrder.nativeOrder());
307 final StringBuilder sb =
new StringBuilder(3 *
length - 1);
308 if( ByteOrder.LITTLE_ENDIAN == ByteOrder.nativeOrder() ) {
309 for(
int i=
length-1; 0 <= i; --i) {
316 for(
int i=0; i <
length; ++i) {
323 return sb.toString();
325 return new String(
":");
A 48 bit EUI-48 sub-identifier, see EUI48.
void clearHash()
Method clears the cached hash value.
static final EUI48Sub ALL_DEVICE
EUI48Sub MAC address matching all device, i.e.
static final EUI48Sub LOCAL_DEVICE
EUI48Sub MAC address matching local device, i.e.
final byte b[]
The EUI48 sub-address, always 6 bytes reserved.
int indexOf(final EUI48Sub needle, final ByteOrder byte_order)
Finds the index of given EUI48Sub needle within this instance haystack in the given byte order.
static boolean scanEUI48Sub(final String str, final EUI48Sub dest, final StringBuilder errmsg)
Fills given EUI48Sub instance via given string representation.
EUI48Sub(final String str)
Construct a sub EUI48 via given string representation.
static int indexOf(final byte haystack_b[], final int haystack_length, final byte needle_b[], final int needle_length, final ByteOrder byte_order)
Find index of needle within haystack in the given byte order.
EUI48Sub()
Construct empty unset instance.
int length
The actual length in bytes of the EUI48 sub-address, less or equal 6 bytes.
EUI48Sub(final byte stream[], final int pos, final int len_, final ByteOrder byte_order)
Copy len_ address bytes from given source and byte order, while converting them to ByteOrder#nativeOr...
static final EUI48Sub ANY_DEVICE
EUI48Sub MAC address matching any device, i.e.
boolean contains(final EUI48Sub needle)
Returns true, if given EUI48Sub needle is contained in this instance haystack.
final boolean equals(final Object obj)
void clear()
Method clears the underlying byte array b and sets length to zero.
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
static StringBuilder byteHexString(final StringBuilder sb, final byte value, final boolean lowerCase)
Produce a hexadecimal string representation of the given byte value.
static void bswap(final byte[] source, final int source_pos, final byte[] sink, final int sink_pos, final int len)