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 java.math.BigInteger;
11 import java.util.Arrays;
13 import com.google.common.base.Preconditions;
14 import com.google.common.io.BaseEncoding;
19 public abstract class ByteUtil {
21 /** default hex string separator */
22 private static final String DEFAULT_HEX_SEPARATOR = ":";
23 /** basic hex string encoding */
24 private static final BaseEncoding PLAIN_HEX_16_ENCODING = BaseEncoding.base16().lowerCase();
25 /** hex string encoding involving {@link #DEFAULT_HEX_SEPARATOR} as searator */
26 private static final BaseEncoding HEX_16_ENCODING = PLAIN_HEX_16_ENCODING.withSeparator(DEFAULT_HEX_SEPARATOR, 2);
29 * @param bytes bytes that needs to be converted to hex
30 * @param delimiter string delimiter
31 * @return hexString containing bytes, separated with delimiter
33 public static String bytesToHexstring(final byte[] bytes, final String delimiter) {
34 BaseEncoding be = HEX_16_ENCODING;
35 if (delimiter != DEFAULT_HEX_SEPARATOR) {
36 be = PLAIN_HEX_16_ENCODING.withSeparator(delimiter, 2);
38 return be.encode(bytes);
42 * Utility method to convert BigInteger to n element byte array
44 * @param bigInteger big integer value that needs to be converted to byte
45 * @param numBytes convert to number of bytes
46 * @return byte array containing n * 8 bits.
48 public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
49 if (bigInteger == null) {
52 byte[] inputArray = bigInteger.toByteArray();
53 byte[] outputArray = new byte[numBytes];
54 if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
55 Arrays.fill(outputArray, (byte) -1);
57 Arrays.fill(outputArray, (byte) 0);
59 System.arraycopy(inputArray,
60 Math.max(0, inputArray.length - outputArray.length),
62 Math.max(0, outputArray.length - inputArray.length),
63 Math.min(outputArray.length, inputArray.length));
68 * Converts a 4 byte array of unsigned bytes to unsigned int
70 * @param bytes an array of 4 unsigned bytes
71 * @return a long representing the unsigned int
73 public static final long bytesToUnsignedInt(final byte[] bytes) {
74 Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
76 unsignedInt |= bytes[0] & 0xFF;
78 unsignedInt |= bytes[1] & 0xFF;
80 unsignedInt |= bytes[2] & 0xFF;
82 unsignedInt |= bytes[3] & 0xFF;
87 * Converts a 3 byte array of unsigned bytes to unsigned int
89 * @param bytes an array of 4 unsigned bytes
90 * @return a long representing the unsigned int
92 public static final long bytesToUnsignedMedium(final byte[] bytes) {
93 Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
94 long unsignedMedium = 0;
95 unsignedMedium |= bytes[0] & 0xFF;
97 unsignedMedium |= bytes[1] & 0xFF;
99 unsignedMedium |= bytes[2] & 0xFF;
100 return unsignedMedium;
104 * Converts a 2 byte array of unsigned bytes to unsigned short
106 * @param bytes an array of 2 unsigned bytes
107 * @return an int representing the unsigned short
109 public static final int bytesToUnsignedShort(final byte[] bytes) {
110 Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
111 int unsignedShort = 0;
112 unsignedShort |= bytes[0] & 0xFF;
114 unsignedShort |= bytes[1] & 0xFF;
115 return unsignedShort;
119 * Converts unsigned integer to a 4 byte array of unsigned bytes
121 * @param unsignedInt representing the unsigned integer
122 * @return bytes an array of 4 unsigned bytes
124 public static byte[] unsignedIntToBytes(final Long unsignedInt) {
125 byte[] bytes = new byte[4];
126 bytes[3] = (byte) (unsignedInt & 0xFF);
127 bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
128 bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
129 bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
134 * Converts unsigned integer to a 3 byte array of unsigned bytes
136 * @param unsignedInt representing the unsigned integer
137 * @return bytes an array of 3 unsigned bytes
139 public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
140 byte[] bytes = new byte[3];
141 bytes[2] = (byte) (unsignedInt & 0xFF);
142 bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
143 bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
148 * Converts unsigned short to a 2 byte array of unsigned bytes
150 * @param unsignedShort representing the unsigned short
151 * @return bytes an array of 2 unsigned bytes
153 public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
154 byte[] bytes = new byte[2];
155 bytes[1] = (byte) (unsignedShort & 0xFF);
156 bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);