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);
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
45 * @return byte array containing n * 8 bits.
47 public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
48 if (bigInteger == null) {
51 byte[] inputArray = bigInteger.toByteArray();
52 byte[] outputArray = new byte[numBytes];
53 if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
54 Arrays.fill(outputArray, (byte) -1);
56 Arrays.fill(outputArray, (byte) 0);
58 System.arraycopy(inputArray,
59 Math.max(0, inputArray.length - outputArray.length),
61 Math.max(0, outputArray.length - inputArray.length),
62 Math.min(outputArray.length, inputArray.length));
67 * Converts a 4 byte array of unsigned bytes to unsigned int
69 * @param bytes an array of 4 unsigned bytes
70 * @return a long representing the unsigned int
72 public static final long bytesToUnsignedInt(final byte[] bytes) {
73 Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
75 unsignedInt |= bytes[0] & 0xFF;
77 unsignedInt |= bytes[1] & 0xFF;
79 unsignedInt |= bytes[2] & 0xFF;
81 unsignedInt |= bytes[3] & 0xFF;
86 * Converts a 3 byte array of unsigned bytes to unsigned int
88 * @param bytes an array of 4 unsigned bytes
89 * @return a long representing the unsigned int
91 public static final long bytesToUnsignedMedium(final byte[] bytes) {
92 Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
93 long unsignedMedium = 0;
94 unsignedMedium |= bytes[0] & 0xFF;
96 unsignedMedium |= bytes[1] & 0xFF;
98 unsignedMedium |= bytes[2] & 0xFF;
99 return unsignedMedium;
103 * Converts a 2 byte array of unsigned bytes to unsigned short
105 * @param bytes an array of 2 unsigned bytes
106 * @return an int representing the unsigned short
108 public static final int bytesToUnsignedShort(final byte[] bytes) {
109 Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
110 int unsignedShort = 0;
111 unsignedShort |= bytes[0] & 0xFF;
113 unsignedShort |= bytes[1] & 0xFF;
114 return unsignedShort;
118 * Converts unsigned integer to a 4 byte array of unsigned bytes
120 * @param unsignedInt representing the unsigned integer
121 * @return bytes an array of 4 unsigned bytes
123 public static byte[] unsignedIntToBytes(final Long unsignedInt) {
124 byte[] bytes = new byte[4];
125 bytes[3] = (byte) (unsignedInt & 0xFF);
126 bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
127 bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
128 bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
133 * Converts unsigned integer to a 3 byte array of unsigned bytes
135 * @param unsignedInt representing the unsigned integer
136 * @return bytes an array of 3 unsigned bytes
138 public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
139 byte[] bytes = new byte[3];
140 bytes[2] = (byte) (unsignedInt & 0xFF);
141 bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
142 bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
147 * Converts unsigned short to a 2 byte array of unsigned bytes
149 * @param unsignedShort representing the unsigned short
150 * @return bytes an array of 2 unsigned bytes
152 public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
153 byte[] bytes = new byte[2];
154 bytes[1] = (byte) (unsignedShort & 0xFF);
155 bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);