2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.util;
10 import com.google.common.base.Preconditions;
12 import java.math.BigInteger;
13 import java.util.Arrays;
18 public abstract class ByteUtil {
23 * @return hexString containing bytes, separated with delimiter
25 public static String bytesToHexstring(final byte[] bytes, final String delimiter) {
26 StringBuffer sb = new StringBuffer();
27 for (byte b : bytes) {
28 sb.append(String.format("%02x%s", b, delimiter));
34 * Utility method to convert BigInteger to n element byte array
37 * @return byte array containing n * 8 bits.
39 public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
40 if (bigInteger == null) {
43 byte[] inputArray = bigInteger.toByteArray();
44 byte[] outputArray = new byte[numBytes];
45 if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
46 Arrays.fill(outputArray, (byte) -1);
48 Arrays.fill(outputArray, (byte) 0);
50 System.arraycopy(inputArray,
51 Math.max(0, inputArray.length - outputArray.length),
53 Math.max(0, outputArray.length - inputArray.length),
54 Math.min(outputArray.length, inputArray.length));
59 * Converts a 4 byte array of unsigned bytes to unsigned int
61 * @param bytes an array of 4 unsigned bytes
62 * @return a long representing the unsigned int
64 public static final long bytesToUnsignedInt(final byte[] bytes) {
65 Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly two bytes long.");
67 unsignedInt |= bytes[0] & 0xFF;
69 unsignedInt |= bytes[1] & 0xFF;
71 unsignedInt |= bytes[2] & 0xFF;
73 unsignedInt |= bytes[3] & 0xFF;
78 * Converts a 2 byte array of unsigned bytes to unsigned short
80 * @param bytes an array of 2 unsigned bytes
81 * @return an int representing the unsigned short
83 public static final int bytesToUnsignedShort(final byte[] bytes) {
84 Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
85 int unsignedShort = 0;
86 unsignedShort |= bytes[0] & 0xFF;
88 unsignedShort |= bytes[1] & 0xFF;
93 * Converts unsigned integer to a 4 byte array of unsigned bytes
95 * @param unsignedInt representing the unsigned integer
96 * @return bytes an array of 4 unsigned bytes
98 public static byte[] unsignedIntToBytes(final Long unsignedInt) {
99 byte[] bytes = new byte[4];
100 bytes[3] = (byte) (unsignedInt & 0xFF);
101 bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
102 bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
103 bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
108 * Converts unsigned short to a 2 byte array of unsigned bytes
110 * @param unsignedShort representing the unsigned short
111 * @return bytes an array of 2 unsigned bytes
113 public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
114 byte[] bytes = new byte[2];
115 bytes[1] = (byte) (unsignedShort & 0xFF);
116 bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);