Bug 2280 - MatchConvertorImpl bugs found during test creation
[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 import java.math.BigInteger;
12 import java.util.Arrays;
13
14 /**
15  * @author mirehak
16  */
17 public abstract class ByteUtil {
18
19     /**
20      * @param bytes
21      * @param delimiter
22      * @return hexString containing bytes, separated with delimiter
23      */
24     public static String bytesToHexstring(final byte[] bytes, final String delimiter) {
25         StringBuffer sb = new StringBuffer();
26         for (byte b : bytes) {
27             sb.append(String.format("%02x%s", b, delimiter));
28         }
29         return sb.toString();
30     }
31
32     /**
33      * Utility method to convert BigInteger to n element byte array
34      *
35      * @param bigInteger
36      * @return byte array containing n * 8 bits.
37      */
38     public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
39         if (bigInteger == null) {
40             return null;
41         }
42         byte[] inputArray = bigInteger.toByteArray();
43         byte[] outputArray = new byte[numBytes];
44         if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
45             Arrays.fill(outputArray, (byte) -1);
46         } else {
47             Arrays.fill(outputArray, (byte) 0);
48         }
49         System.arraycopy(inputArray,
50                 Math.max(0, inputArray.length - outputArray.length),
51                 outputArray,
52                 Math.max(0, outputArray.length - inputArray.length),
53                 Math.min(outputArray.length, inputArray.length));
54         return outputArray;
55     }
56
57     /**
58      * Converts a 4 byte array of unsigned bytes to unsigned int
59      *
60      * @param bytes an array of 4 unsigned bytes
61      * @return a long representing the unsigned int
62      */
63     public static final long bytesToUnsignedInt(final byte[] bytes) {
64         Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
65         long unsignedInt = 0;
66         unsignedInt |= bytes[0] & 0xFF;
67         unsignedInt <<= 8;
68         unsignedInt |= bytes[1] & 0xFF;
69         unsignedInt <<= 8;
70         unsignedInt |= bytes[2] & 0xFF;
71         unsignedInt <<= 8;
72         unsignedInt |= bytes[3] & 0xFF;
73         return unsignedInt;
74     }
75
76     /**
77      * Converts a 3 byte array of unsigned bytes to unsigned int
78      *
79      * @param bytes an array of 4 unsigned bytes
80      * @return a long representing the unsigned int
81      */
82     public static final long bytesToUnsignedMedium(final byte[] bytes) {
83         Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
84         long unsignedMedium = 0;
85         unsignedMedium |= bytes[0] & 0xFF;
86         unsignedMedium <<= 8;
87         unsignedMedium |= bytes[1] & 0xFF;
88         unsignedMedium <<= 8;
89         unsignedMedium |= bytes[2] & 0xFF;
90         return unsignedMedium;
91     }
92
93     /**
94      * Converts a 2 byte array of unsigned bytes to unsigned short
95      *
96      * @param bytes an array of 2 unsigned bytes
97      * @return an int representing the unsigned short
98      */
99     public static final int bytesToUnsignedShort(final byte[] bytes) {
100         Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
101         int unsignedShort = 0;
102         unsignedShort |= bytes[0] & 0xFF;
103         unsignedShort <<= 8;
104         unsignedShort |= bytes[1] & 0xFF;
105         return unsignedShort;
106     }
107
108     /**
109      * Converts unsigned integer to a 4 byte array of unsigned bytes
110      *
111      * @param unsignedInt representing the unsigned integer
112      * @return bytes an array of 4 unsigned bytes
113      */
114     public static byte[] unsignedIntToBytes(final Long unsignedInt) {
115         byte[] bytes = new byte[4];
116         bytes[3] = (byte) (unsignedInt & 0xFF);
117         bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
118         bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
119         bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
120         return bytes;
121     }
122
123     /**
124      * Converts unsigned integer to a 3 byte array of unsigned bytes
125      *
126      * @param unsignedInt representing the unsigned integer
127      * @return bytes an array of 3 unsigned bytes
128      */
129     public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
130         byte[] bytes = new byte[3];
131         bytes[2] = (byte) (unsignedInt & 0xFF);
132         bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
133         bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
134         return bytes;
135     }
136
137     /**
138      * Converts unsigned short to a 2 byte array of unsigned bytes
139      *
140      * @param unsignedShort representing the unsigned short
141      * @return bytes an array of 2 unsigned bytes
142      */
143     public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
144         byte[] bytes = new byte[2];
145         bytes[1] = (byte) (unsignedShort & 0xFF);
146         bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);
147         return bytes;
148     }
149 }