bf2631fc02666897dec12e087f7d34a10ca27b9f
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / util / ByteUtil.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.openflow.md.util;
9
10 import com.google.common.base.Preconditions;
11
12 import java.math.BigInteger;
13 import java.util.Arrays;
14
15 /**
16  * @author mirehak
17  */
18 public abstract class ByteUtil {
19
20     /**
21      * @param bytes
22      * @param delimiter
23      * @return hexString containing bytes, separated with delimiter
24      */
25     public static String bytesToHexstring(final byte[] bytes, final String delimiter) {
26         StringBuffer sb = new StringBuffer();
27         for (byte b : bytes) {
28             sb.append(String.format("%02x%s", b, delimiter));
29         }
30         return sb.toString();
31     }
32
33     /**
34      * Utility method to convert BigInteger to n element byte array
35      *
36      * @param bigInteger
37      * @return byte array containing n * 8 bits.
38      */
39     public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
40         if (bigInteger == null) {
41             return null;
42         }
43         byte[] inputArray = bigInteger.toByteArray();
44         byte[] outputArray = new byte[numBytes];
45         if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
46             Arrays.fill(outputArray, (byte) -1);
47         } else {
48             Arrays.fill(outputArray, (byte) 0);
49         }
50         System.arraycopy(inputArray,
51                 Math.max(0, inputArray.length - outputArray.length),
52                 outputArray,
53                 Math.max(0, outputArray.length - inputArray.length),
54                 Math.min(outputArray.length, inputArray.length));
55         return outputArray;
56     }
57
58     /**
59      * Converts a 4 byte array of unsigned bytes to unsigned int
60      *
61      * @param bytes an array of 4 unsigned bytes
62      * @return a long representing the unsigned int
63      */
64     public static final long bytesToUnsignedInt(final byte[] bytes) {
65         Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly two bytes long.");
66         long unsignedInt = 0;
67         unsignedInt |= bytes[0] & 0xFF;
68         unsignedInt <<= 8;
69         unsignedInt |= bytes[1] & 0xFF;
70         unsignedInt <<= 8;
71         unsignedInt |= bytes[2] & 0xFF;
72         unsignedInt <<= 8;
73         unsignedInt |= bytes[3] & 0xFF;
74         return unsignedInt;
75     }
76
77     /**
78      * Converts a 2 byte array of unsigned bytes to unsigned short
79      *
80      * @param bytes an array of 2 unsigned bytes
81      * @return an int representing the unsigned short
82      */
83     public static final int bytesToUnsignedShort(final byte[] bytes) {
84         Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
85         int unsignedShort = 0;
86         unsignedShort |= bytes[0] & 0xFF;
87         unsignedShort <<= 8;
88         unsignedShort |= bytes[1] & 0xFF;
89         return unsignedShort;
90     }
91
92     /**
93      * Converts unsigned integer to a 4 byte array of unsigned bytes
94      *
95      * @param unsignedInt representing the unsigned integer
96      * @return bytes an array of 4 unsigned bytes
97      */
98     public static byte[] unsignedIntToBytes(final Long unsignedInt) {
99         byte[] bytes = new byte[4];
100         bytes[3] = (byte) (unsignedInt & 0xFF);
101         bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
102         bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
103         bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
104         return bytes;
105     }
106
107     /**
108      * Converts unsigned short to a 2 byte array of unsigned bytes
109      *
110      * @param unsignedShort representing the unsigned short
111      * @return bytes an array of 2 unsigned bytes
112      */
113     public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
114         byte[] bytes = new byte[2];
115         bytes[1] = (byte) (unsignedShort & 0xFF);
116         bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);
117         return bytes;
118     }
119 }