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;
11 import java.math.BigInteger;
12 import java.util.Arrays;
17 public abstract class ByteUtil {
22 * @return hexString containing bytes, separated with delimiter
24 public static String bytesToHexstring(final byte[] bytes, final String delimiter) {
25 StringBuffer sb = new StringBuffer();
26 for (byte b : bytes) {
27 sb.append(String.format("%02x%s", b, delimiter));
33 * Utility method to convert BigInteger to n element byte array
36 * @return byte array containing n * 8 bits.
38 public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
39 if (bigInteger == null) {
42 byte[] inputArray = bigInteger.toByteArray();
43 byte[] outputArray = new byte[numBytes];
44 if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
45 Arrays.fill(outputArray, (byte) -1);
47 Arrays.fill(outputArray, (byte) 0);
49 System.arraycopy(inputArray,
50 Math.max(0, inputArray.length - outputArray.length),
52 Math.max(0, outputArray.length - inputArray.length),
53 Math.min(outputArray.length, inputArray.length));
58 * Converts a 4 byte array of unsigned bytes to unsigned int
60 * @param bytes an array of 4 unsigned bytes
61 * @return a long representing the unsigned int
63 public static final long bytesToUnsignedInt(final byte[] bytes) {
64 Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
66 unsignedInt |= bytes[0] & 0xFF;
68 unsignedInt |= bytes[1] & 0xFF;
70 unsignedInt |= bytes[2] & 0xFF;
72 unsignedInt |= bytes[3] & 0xFF;
77 * Converts a 3 byte array of unsigned bytes to unsigned int
79 * @param bytes an array of 4 unsigned bytes
80 * @return a long representing the unsigned int
82 public static final long bytesToUnsignedMedium(final byte[] bytes) {
83 Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
84 long unsignedMedium = 0;
85 unsignedMedium |= bytes[0] & 0xFF;
87 unsignedMedium |= bytes[1] & 0xFF;
89 unsignedMedium |= bytes[2] & 0xFF;
90 return unsignedMedium;
94 * Converts a 2 byte array of unsigned bytes to unsigned short
96 * @param bytes an array of 2 unsigned bytes
97 * @return an int representing the unsigned short
99 public static final int bytesToUnsignedShort(final byte[] bytes) {
100 Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
101 int unsignedShort = 0;
102 unsignedShort |= bytes[0] & 0xFF;
104 unsignedShort |= bytes[1] & 0xFF;
105 return unsignedShort;
109 * Converts unsigned integer to a 4 byte array of unsigned bytes
111 * @param unsignedInt representing the unsigned integer
112 * @return bytes an array of 4 unsigned bytes
114 public static byte[] unsignedIntToBytes(final Long unsignedInt) {
115 byte[] bytes = new byte[4];
116 bytes[3] = (byte) (unsignedInt & 0xFF);
117 bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
118 bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
119 bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
124 * Converts unsigned integer to a 3 byte array of unsigned bytes
126 * @param unsignedInt representing the unsigned integer
127 * @return bytes an array of 3 unsigned bytes
129 public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
130 byte[] bytes = new byte[3];
131 bytes[2] = (byte) (unsignedInt & 0xFF);
132 bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
133 bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
138 * Converts unsigned short to a 2 byte array of unsigned bytes
140 * @param unsignedShort representing the unsigned short
141 * @return bytes an array of 2 unsigned bytes
143 public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
144 byte[] bytes = new byte[2];
145 bytes[1] = (byte) (unsignedShort & 0xFF);
146 bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);