Droptest: optimize macToString
[openflowplugin.git] / drop-test / src / main / java / org / opendaylight / openflowplugin / droptest / DropTestUtils.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.droptest;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12
13 public class DropTestUtils {
14     private static final char[] HEX = "0123456789ABCDEF".toCharArray();
15
16     private static final void appendByte(final StringBuilder sb, final byte b) {
17         int v = UnsignedBytes.toInt(b);
18         sb.append(HEX[v >> 4]);
19         sb.append(HEX[v & 15]);
20     }
21
22     public static String macToString(final byte[] mac) {
23         Preconditions.checkArgument(mac.length == 6);
24
25         final StringBuilder sb = new StringBuilder(17);
26         appendByte(sb, mac[0]);
27
28         for (int i = 1; i < mac.length; i++) {
29             sb.append(':');
30             appendByte(sb, mac[i]);
31         }
32
33         return sb.toString();
34     }
35 }