*/
package org.opendaylight.openflowplugin.openflow.md.util;
+import com.google.common.base.Preconditions;
+
import java.math.BigInteger;
import java.util.Arrays;
/**
* @author mirehak
- *
*/
public abstract class ByteUtil {
/**
* Utility method to convert BigInteger to n element byte array
+ *
* @param bigInteger
* @return byte array containing n * 8 bits.
*/
Arrays.fill(outputArray, (byte) 0);
}
System.arraycopy(inputArray,
- Math.max(0, inputArray.length - outputArray.length),
- outputArray,
- Math.max(0, outputArray.length - inputArray.length),
- Math.min(outputArray.length, inputArray.length));
+ Math.max(0, inputArray.length - outputArray.length),
+ outputArray,
+ Math.max(0, outputArray.length - inputArray.length),
+ Math.min(outputArray.length, inputArray.length));
return outputArray;
}
/**
* Converts a 4 byte array of unsigned bytes to unsigned int
+ *
* @param bytes an array of 4 unsigned bytes
* @return a long representing the unsigned int
*/
- public static final long bytesToUnsignedInt(final byte[] bytes)
- {
- long unsignedInt = 0;
- unsignedInt |= bytes[0] & 0xFF;
- unsignedInt <<= 8;
- unsignedInt |= bytes[1] & 0xFF;
- unsignedInt <<= 8;
- unsignedInt |= bytes[2] & 0xFF;
- unsignedInt <<= 8;
- unsignedInt |= bytes[3] & 0xFF;
- return unsignedInt;
+ public static final long bytesToUnsignedInt(final byte[] bytes) {
+ Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly two bytes long.");
+ long unsignedInt = 0;
+ unsignedInt |= bytes[0] & 0xFF;
+ unsignedInt <<= 8;
+ unsignedInt |= bytes[1] & 0xFF;
+ unsignedInt <<= 8;
+ unsignedInt |= bytes[2] & 0xFF;
+ unsignedInt <<= 8;
+ unsignedInt |= bytes[3] & 0xFF;
+ return unsignedInt;
}
/**
* Converts a 2 byte array of unsigned bytes to unsigned short
+ *
* @param bytes an array of 2 unsigned bytes
* @return an int representing the unsigned short
*/
- public static final int bytesToUnsignedShort(final byte[] bytes)
- {
- int unsignedShort = 0;
- unsignedShort |= bytes[0] & 0xFF;
- unsignedShort <<= 8;
- unsignedShort |= bytes[1] & 0xFF;
- return unsignedShort;
+ public static final int bytesToUnsignedShort(final byte[] bytes) {
+ Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
+ int unsignedShort = 0;
+ unsignedShort |= bytes[0] & 0xFF;
+ unsignedShort <<= 8;
+ unsignedShort |= bytes[1] & 0xFF;
+ return unsignedShort;
}
/**
* Converts unsigned integer to a 4 byte array of unsigned bytes
+ *
* @param unsignedInt representing the unsigned integer
* @return bytes an array of 4 unsigned bytes
*/
- public static byte[] unsignedIntToBytes(final Long unsignedInt)
- {
- byte[] bytes = new byte[4];
- bytes[3] = (byte) (unsignedInt & 0xFF);
- bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
- bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
- bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
- return bytes;
+ public static byte[] unsignedIntToBytes(final Long unsignedInt) {
+ byte[] bytes = new byte[4];
+ bytes[3] = (byte) (unsignedInt & 0xFF);
+ bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
+ bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
+ bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
+ return bytes;
}
/**
* Converts unsigned short to a 2 byte array of unsigned bytes
+ *
* @param unsignedShort representing the unsigned short
* @return bytes an array of 2 unsigned bytes
*/
- public static byte[] unsignedShortToBytes(final Integer unsignedShort)
- {
- byte[] bytes = new byte[2];
- bytes[1] = (byte) (unsignedShort & 0xFF);
- bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);
- return bytes;
+ public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
+ byte[] bytes = new byte[2];
+ bytes[1] = (byte) (unsignedShort & 0xFF);
+ bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);
+ return bytes;
}
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.openflow.md.util;
+
+import org.junit.Test;
+
+import java.math.BigInteger;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Created by Martin Bobak mbobak@cisco.com on 6/30/14.
+ */
+public class ByteUtilTest {
+
+ private static final String hexString = "64,65,66,ff,";
+ private static final String hexString00 = "00,00,00,00,";
+ private static final String hexStringFF = "ff,ff,ff,ff,";
+
+ private static final byte[] testBytes = {100, 101, 102, (byte) 255};
+ private static final byte[] testBytes00 = {0, 0, 0, 0};
+ private static final byte[] testBytesFF = {(byte) 255, (byte) 255, (byte) 255, (byte) 255};
+
+ private static final BigInteger bigInteger = new BigInteger("1684367103");
+ private static final BigInteger bigIntFF = new BigInteger("4294967295");
+ private static final int int00 = 0;
+
+ private static final int shortByteLength = 2;
+ private static final int intByteLength = 4;
+
+ @Test
+ public void testBytesToHexstring() {
+ assertEquals(hexString, ByteUtil.bytesToHexstring(testBytes, ","));
+ assertEquals(hexString00, ByteUtil.bytesToHexstring(testBytes00, ","));
+ assertEquals(hexStringFF, ByteUtil.bytesToHexstring(testBytesFF, ","));
+ }
+
+ @Test
+ public void testConvertBigIntegerToNBytes() {
+ byte[] bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 4);
+ assertEquals(4, bigIntAsBytes.length);
+
+ bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 6);
+ assertEquals(6, bigIntAsBytes.length);
+
+ bigIntAsBytes = ByteUtil.convertBigIntegerToNBytes(bigInteger, 8);
+ assertEquals(8, bigIntAsBytes.length);
+ }
+
+ @Test
+ public void testBytesToUnsignedInt() {
+ long unsigned = ByteUtil.bytesToUnsignedInt(testBytes);
+ assertEquals(bigInteger.longValue(), unsigned);
+
+ unsigned = ByteUtil.bytesToUnsignedInt(testBytes00);
+ assertEquals(0, unsigned);
+
+ unsigned = ByteUtil.bytesToUnsignedInt(testBytesFF);
+ assertEquals(bigIntFF.longValue(), unsigned);
+ }
+
+ @Test
+ public void testBytesToUnsignedShort() {
+
+ byte[] twoBytes = {100, 101};
+ int unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
+ assertEquals(bigInteger.shiftRight(16).shortValue(), unsigned);
+
+ twoBytes = new byte[]{0, 0};
+ unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
+ assertEquals(int00, unsigned);
+
+ twoBytes = new byte[]{(byte) 255, (byte) 255};
+ unsigned = ByteUtil.bytesToUnsignedShort(twoBytes);
+ assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void exceptionTestBytesToUnsignedShort() {
+ ByteUtil.bytesToUnsignedShort(testBytes);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void exceptionTestBytesToUnsignedInt() {
+ byte[] fiveBytes = {0, 0, 0, 0, 0};
+ ByteUtil.bytesToUnsignedInt(fiveBytes);
+ }
+
+ @Test
+ public void testUnsignedIntToBytes() {
+ long intValue = 255;
+ byte[] bytes = ByteUtil.unsignedIntToBytes(intValue);
+
+ assertTrue(bytes.length == intByteLength);
+
+ intValue += 256;
+ bytes = ByteUtil.unsignedIntToBytes(intValue);
+ assertTrue(bytes.length == intByteLength);
+
+ intValue += 256;
+ bytes = ByteUtil.unsignedIntToBytes(intValue);
+ assertTrue(bytes.length == intByteLength);
+ }
+
+ @Test
+ public void testUnsignedShortToBytes() {
+ int intValue = 255;
+ byte[] bytes = ByteUtil.unsignedShortToBytes(intValue);
+
+ assertTrue(bytes.length == shortByteLength);
+
+ intValue += 256;
+ bytes = ByteUtil.unsignedShortToBytes(intValue);
+ assertTrue(bytes.length == shortByteLength);
+
+ intValue += 256;
+ bytes = ByteUtil.unsignedShortToBytes(intValue);
+ assertTrue(bytes.length == shortByteLength);
+ }
+}