Merge "device contexts holds and provides meter registry"
[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
30      * @param 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
45      * @return byte array containing n * 8 bits.
46      */
47     public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
48         if (bigInteger == null) {
49             return null;
50         }
51         byte[] inputArray = bigInteger.toByteArray();
52         byte[] outputArray = new byte[numBytes];
53         if (bigInteger.compareTo(BigInteger.ZERO) < 0) {
54             Arrays.fill(outputArray, (byte) -1);
55         } else {
56             Arrays.fill(outputArray, (byte) 0);
57         }
58         System.arraycopy(inputArray,
59                 Math.max(0, inputArray.length - outputArray.length),
60                 outputArray,
61                 Math.max(0, outputArray.length - inputArray.length),
62                 Math.min(outputArray.length, inputArray.length));
63         return outputArray;
64     }
65
66     /**
67      * Converts a 4 byte array of unsigned bytes to unsigned int
68      *
69      * @param bytes an array of 4 unsigned bytes
70      * @return a long representing the unsigned int
71      */
72     public static final long bytesToUnsignedInt(final byte[] bytes) {
73         Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
74         long unsignedInt = 0;
75         unsignedInt |= bytes[0] & 0xFF;
76         unsignedInt <<= 8;
77         unsignedInt |= bytes[1] & 0xFF;
78         unsignedInt <<= 8;
79         unsignedInt |= bytes[2] & 0xFF;
80         unsignedInt <<= 8;
81         unsignedInt |= bytes[3] & 0xFF;
82         return unsignedInt;
83     }
84
85     /**
86      * Converts a 3 byte array of unsigned bytes to unsigned int
87      *
88      * @param bytes an array of 4 unsigned bytes
89      * @return a long representing the unsigned int
90      */
91     public static final long bytesToUnsignedMedium(final byte[] bytes) {
92         Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
93         long unsignedMedium = 0;
94         unsignedMedium |= bytes[0] & 0xFF;
95         unsignedMedium <<= 8;
96         unsignedMedium |= bytes[1] & 0xFF;
97         unsignedMedium <<= 8;
98         unsignedMedium |= bytes[2] & 0xFF;
99         return unsignedMedium;
100     }
101
102     /**
103      * Converts a 2 byte array of unsigned bytes to unsigned short
104      *
105      * @param bytes an array of 2 unsigned bytes
106      * @return an int representing the unsigned short
107      */
108     public static final int bytesToUnsignedShort(final byte[] bytes) {
109         Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
110         int unsignedShort = 0;
111         unsignedShort |= bytes[0] & 0xFF;
112         unsignedShort <<= 8;
113         unsignedShort |= bytes[1] & 0xFF;
114         return unsignedShort;
115     }
116
117     /**
118      * Converts unsigned integer to a 4 byte array of unsigned bytes
119      *
120      * @param unsignedInt representing the unsigned integer
121      * @return bytes an array of 4 unsigned bytes
122      */
123     public static byte[] unsignedIntToBytes(final Long unsignedInt) {
124         byte[] bytes = new byte[4];
125         bytes[3] = (byte) (unsignedInt & 0xFF);
126         bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
127         bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
128         bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
129         return bytes;
130     }
131
132     /**
133      * Converts unsigned integer to a 3 byte array of unsigned bytes
134      *
135      * @param unsignedInt representing the unsigned integer
136      * @return bytes an array of 3 unsigned bytes
137      */
138     public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
139         byte[] bytes = new byte[3];
140         bytes[2] = (byte) (unsignedInt & 0xFF);
141         bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
142         bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
143         return bytes;
144     }
145
146     /**
147      * Converts unsigned short to a 2 byte array of unsigned bytes
148      *
149      * @param unsignedShort representing the unsigned short
150      * @return bytes an array of 2 unsigned bytes
151      */
152     public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
153         byte[] bytes = new byte[2];
154         bytes[1] = (byte) (unsignedShort & 0xFF);
155         bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);
156         return bytes;
157     }
158 }