Move integration tests to mdsal-it-parent
[lispflowmapping.git] / mappingservice / yangmodel / src / main / java / org / opendaylight / lispflowmapping / lisp / util / ByteUtil.java
1 /*
2  * Copyright (c) 2014 Contextream, 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
9 package org.opendaylight.lispflowmapping.lisp.util;
10
11 import java.nio.ByteBuffer;
12
13 public class ByteUtil {
14
15     final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
16
17     public static int getUnsignedByte(byte[] inBuffer, int pos) {
18         return inBuffer[pos] & 0xFF;
19     }
20
21     public static int getUnsignedByte(ByteBuffer inBuffer, int pos) {
22         return inBuffer.get(pos) & 0xFF;
23     }
24
25     public static int getUnsignedByte(ByteBuffer inBuffer) {
26         return inBuffer.get() & 0xFF;
27     }
28
29     public static int getUnsignedShort(byte[] inBuffer, int pos) {
30         return asUnsignedShort(getShort(inBuffer, pos));
31     }
32
33     public static int getUnsignedShort(ByteBuffer inBuffer, int pos) {
34         return asUnsignedShort(inBuffer.getShort(pos));
35     }
36
37     public static short getShort(byte[] inBuffer, int pos) {
38         return ByteBuffer.wrap(inBuffer, pos, 2).getShort();
39     }
40
41     public static int getInt(byte[] inBuffer, int pos) {
42         return ByteBuffer.wrap(inBuffer, pos, 4).getInt();
43     }
44
45     public static int getPartialInt(byte[] inBuffer) {
46         ByteBuffer buffer = ByteBuffer.allocate(4);
47         buffer.position(4 - inBuffer.length);
48         buffer.put(inBuffer);
49         buffer.position(0);
50         return buffer.getInt();
51     }
52
53     public static short asUnsignedByte(byte b) {
54         return (short) ((short) b & 0xFF);
55     }
56
57     public static int asUnsignedShort(short s) {
58         return s & 0xFFFF;
59     }
60
61     public static long asUnsignedInteger(int i) {
62         return i & 0xFFFFFFFF;
63     }
64
65     public static byte[] partialIntToByteArray(int number, int length) {
66         ByteBuffer buffer = ByteBuffer.allocate(4);
67         buffer.putInt(number);
68         byte[] result = new byte[length];
69         buffer.position(4 - length);
70         buffer.get(result);
71         return result;
72     }
73
74     public static long getLong(byte[] inBuffer, int pos) {
75         return ByteBuffer.wrap(inBuffer, pos, 8).getLong();
76     }
77
78     public static boolean extractBit(byte byteValue, int bitMask) {
79         return (byteValue & bitMask) == bitMask;
80     }
81
82     public static byte boolToBit(boolean flag, int bit) {
83         return (byte) (flag ? bit : 0x00);
84     }
85
86     public static String bytesToHex(byte[] bytes, int length) {
87         char[] hexChars = new char[length * 2];
88         for ( int j = 0; j < length; j++ ) {
89             int v = bytes[j] & 0xFF;
90             hexChars[j * 2] = hexArray[v >>> 4];
91             hexChars[j * 2 + 1] = hexArray[v & 0x0F];
92         }
93         return new String(hexChars);
94     }
95 }