Merge changes from topic 'ofj-models-to-ofp-models'
[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 java.math.BigInteger;
11 import java.util.Arrays;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.io.BaseEncoding;
15
16 /**
17  * @author mirehak
18  */
19 public abstract class ByteUtil {
20
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);
27
28     /**
29      * @param bytes bytes that needs to be converted to hex
30      * @param delimiter string delimiter
31      * @return hexString containing bytes, separated with delimiter
32      */
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);
37         }
38         return be.encode(bytes);
39     }
40
41     /**
42      * Utility method to convert BigInteger to n element byte array
43      *
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.
47      */
48     public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
49         if (bigInteger == null) {
50             return null;
51         }
52         byte[] inputArray = bigInteger.toByteArray();
53         byte[] outputArray = new byte[numBytes];
54         if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
55             Arrays.fill(outputArray, (byte) -1);
56         } else {
57             Arrays.fill(outputArray, (byte) 0);
58         }
59         System.arraycopy(inputArray,
60                 Math.max(0, inputArray.length - outputArray.length),
61                 outputArray,
62                 Math.max(0, outputArray.length - inputArray.length),
63                 Math.min(outputArray.length, inputArray.length));
64         return outputArray;
65     }
66
67     /**
68      * Converts a 4 byte array of unsigned bytes to unsigned int
69      *
70      * @param bytes an array of 4 unsigned bytes
71      * @return a long representing the unsigned int
72      */
73     public static final long bytesToUnsignedInt(final byte[] bytes) {
74         Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
75         long unsignedInt = 0;
76         unsignedInt |= bytes[0] & 0xFF;
77         unsignedInt <<= 8;
78         unsignedInt |= bytes[1] & 0xFF;
79         unsignedInt <<= 8;
80         unsignedInt |= bytes[2] & 0xFF;
81         unsignedInt <<= 8;
82         unsignedInt |= bytes[3] & 0xFF;
83         return unsignedInt;
84     }
85
86     /**
87      * Converts a 3 byte array of unsigned bytes to unsigned int
88      *
89      * @param bytes an array of 4 unsigned bytes
90      * @return a long representing the unsigned int
91      */
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;
96         unsignedMedium <<= 8;
97         unsignedMedium |= bytes[1] & 0xFF;
98         unsignedMedium <<= 8;
99         unsignedMedium |= bytes[2] & 0xFF;
100         return unsignedMedium;
101     }
102
103     /**
104      * Converts a 2 byte array of unsigned bytes to unsigned short
105      *
106      * @param bytes an array of 2 unsigned bytes
107      * @return an int representing the unsigned short
108      */
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;
113         unsignedShort <<= 8;
114         unsignedShort |= bytes[1] & 0xFF;
115         return unsignedShort;
116     }
117
118     /**
119      * Converts unsigned integer to a 4 byte array of unsigned bytes
120      *
121      * @param unsignedInt representing the unsigned integer
122      * @return bytes an array of 4 unsigned bytes
123      */
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);
130         return bytes;
131     }
132
133     /**
134      * Converts unsigned integer to a 3 byte array of unsigned bytes
135      *
136      * @param unsignedInt representing the unsigned integer
137      * @return bytes an array of 3 unsigned bytes
138      */
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);
144         return bytes;
145     }
146
147     /**
148      * Converts unsigned short to a 2 byte array of unsigned bytes
149      *
150      * @param unsignedShort representing the unsigned short
151      * @return bytes an array of 2 unsigned bytes
152      */
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);
157         return bytes;
158     }
159 }