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 com.google.common.io.BaseEncoding;
12 import com.google.common.primitives.Longs;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.math.BigInteger;
15 import java.util.Arrays;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.Uint16;
18 import org.opendaylight.yangtools.yang.common.Uint32;
19 import org.opendaylight.yangtools.yang.common.Uint64;
21 public final class ByteUtil {
25 /** default hex string separator. */
26 private static final String DEFAULT_HEX_SEPARATOR = ":";
28 /** basic hex string encoding. */
29 private static final BaseEncoding PLAIN_HEX_16_ENCODING = BaseEncoding.base16().lowerCase();
31 /** hex string encoding involving {@link #DEFAULT_HEX_SEPARATOR} as separator. */
32 private static final BaseEncoding HEX_16_ENCODING = PLAIN_HEX_16_ENCODING.withSeparator(DEFAULT_HEX_SEPARATOR, 2);
35 * Converts bytes to a hex string.
37 * @param bytes bytes that needs to be converted to hex
38 * @param delimiter string delimiter
39 * @return hexString containing bytes, separated with delimiter
41 public static String bytesToHexstring(final byte[] bytes, final String delimiter) {
42 BaseEncoding be = HEX_16_ENCODING;
43 if (!DEFAULT_HEX_SEPARATOR.equals(delimiter)) {
44 be = PLAIN_HEX_16_ENCODING.withSeparator(delimiter, 2);
46 return be.encode(bytes);
50 * Utility method to convert BigInteger to n element byte array.
52 * @param bigInteger big integer value that needs to be converted to byte
53 * @param numBytes convert to number of bytes
54 * @return byte array containing n * 8 bits.
56 @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
57 public static byte @Nullable[] convertBigIntegerToNBytes(final @Nullable BigInteger bigInteger,
59 if (bigInteger == null) {
62 byte[] inputArray = bigInteger.toByteArray();
63 byte[] outputArray = new byte[numBytes];
64 if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
65 Arrays.fill(outputArray, (byte) -1);
67 Arrays.fill(outputArray, (byte) 0);
69 System.arraycopy(inputArray,
70 Math.max(0, inputArray.length - outputArray.length),
72 Math.max(0, outputArray.length - inputArray.length),
73 Math.min(outputArray.length, inputArray.length));
78 * Utility method to convert an Uint64 to an 8-byte array.
80 * @param uint Unsigned long, potentially null.
81 * @return byte array, or null if input was null.
83 @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
84 public static byte @Nullable[] uint64toBytes(@Nullable final Uint64 uint) {
85 return uint == null ? null : Longs.toByteArray(uint.longValue());
89 * Converts a 4 byte array of unsigned bytes to unsigned int.
91 * @param bytes an array of 4 unsigned bytes
92 * @return a long representing the unsigned int
94 public static Uint32 bytesToUnsignedInt(final byte[] bytes) {
95 Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
97 unsignedInt |= bytes[0] & 0xFF;
99 unsignedInt |= bytes[1] & 0xFF;
101 unsignedInt |= bytes[2] & 0xFF;
103 unsignedInt |= bytes[3] & 0xFF;
104 return Uint32.fromIntBits(unsignedInt);
108 * Converts a 3 byte array of unsigned bytes to unsigned int.
110 * @param bytes an array of 4 unsigned bytes
111 * @return a long representing the unsigned int
113 public static long bytesToUnsignedMedium(final byte[] bytes) {
114 Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
115 long unsignedMedium = 0;
116 unsignedMedium |= bytes[0] & 0xFF;
117 unsignedMedium <<= 8;
118 unsignedMedium |= bytes[1] & 0xFF;
119 unsignedMedium <<= 8;
120 unsignedMedium |= bytes[2] & 0xFF;
121 return unsignedMedium;
125 * Converts a 2 byte array of unsigned bytes to unsigned short.
127 * @param bytes an array of 2 unsigned bytes
128 * @return an int representing the unsigned short
130 public static int bytesToUnsignedShort(final byte[] bytes) {
131 Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
132 int unsignedShort = 0;
133 unsignedShort |= bytes[0] & 0xFF;
135 unsignedShort |= bytes[1] & 0xFF;
136 return unsignedShort;
140 * Converts unsigned integer to a 4 byte array of unsigned bytes.
142 * @param unsignedInt representing the unsigned integer
143 * @return bytes an array of 4 unsigned bytes
145 public static byte[] unsignedIntToBytes(final Long unsignedInt) {
146 return unsignedIntToBytes(unsignedInt.longValue());
150 * Converts unsigned integer to a 4 byte array of unsigned bytes.
152 * @param unsignedInt representing the unsigned integer
153 * @return bytes an array of 4 unsigned bytes
155 public static byte[] unsignedIntToBytes(final Uint32 unsignedInt) {
156 return unsignedIntToBytes(unsignedInt.toJava());
160 * Converts unsigned integer to a 4 byte array of unsigned bytes.
162 * @param unsignedInt representing the unsigned integer
163 * @return bytes an array of 4 unsigned bytes
165 private static byte[] unsignedIntToBytes(final long unsignedInt) {
166 byte[] bytes = new byte[4];
167 bytes[3] = (byte) (unsignedInt & 0xFF);
168 bytes[2] = (byte) (unsignedInt >> 8 & 0xFF);
169 bytes[1] = (byte) (unsignedInt >> 16 & 0xFF);
170 bytes[0] = (byte) (unsignedInt >> 24 & 0xFF);
175 * Converts unsigned integer to a 3 byte array of unsigned bytes.
177 * @param unsignedInt representing the unsigned integer
178 * @return bytes an array of 3 unsigned bytes
180 public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
181 return unsignedMediumToBytes(unsignedInt.longValue());
185 * Converts unsigned integer to a 3 byte array of unsigned bytes.
187 * @param unsignedInt representing the unsigned integer
188 * @return bytes an array of 3 unsigned bytes
190 public static byte[] unsignedMediumToBytes(final Uint32 unsignedInt) {
191 return unsignedMediumToBytes(unsignedInt.toJava());
195 * Converts unsigned integer to a 3 byte array of unsigned bytes.
197 * @param unsignedInt representing the unsigned integer
198 * @return bytes an array of 3 unsigned bytes
200 private static byte[] unsignedMediumToBytes(final long unsignedInt) {
201 byte[] bytes = new byte[3];
202 bytes[2] = (byte) (unsignedInt & 0xFF);
203 bytes[1] = (byte) (unsignedInt >> 8 & 0xFF);
204 bytes[0] = (byte) (unsignedInt >> 16 & 0xFF);
209 * Converts unsigned short to a 2 byte array of unsigned bytes.
211 * @param unsignedShort representing the unsigned short
212 * @return bytes an array of 2 unsigned bytes
214 public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
215 return unsignedShortToBytes(unsignedShort.intValue());
219 * Converts unsigned short to a 2 byte array of unsigned bytes.
221 * @param unsignedShort representing the unsigned short
222 * @return bytes an array of 2 unsigned bytes
224 public static byte[] unsignedShortToBytes(final Uint16 unsignedShort) {
225 return unsignedShortToBytes(unsignedShort.toJava());
229 * Converts unsigned short to a 2 byte array of unsigned bytes.
231 * @param unsignedShort representing the unsigned short
232 * @return bytes an array of 2 unsigned bytes
234 private static byte[] unsignedShortToBytes(final int unsignedShort) {
235 byte[] bytes = new byte[2];
236 bytes[1] = (byte) (unsignedShort & 0xFF);
237 bytes[0] = (byte) (unsignedShort >> 8 & 0xFF);