Initial opendaylight infrastructure commit!!
[controller.git] / third-party / openflowj / src / main / java / org / openflow / util / Unsigned.java
1 package org.openflow.util;
2
3 import java.math.BigInteger;
4 import java.nio.ByteBuffer;
5
6 /*****
7  * A util library class for dealing with the lack of unsigned datatypes in Java
8  *
9  * @author Rob Sherwood (rob.sherwood@stanford.edu)
10  * @author David Erickson (daviderickson@cs.stanford.edu)
11  */
12
13 public class Unsigned {
14     /**
15      * Get an unsigned byte from the current position of the ByteBuffer
16      *
17      * @param bb ByteBuffer to get the byte from
18      * @return an unsigned byte contained in a short
19      */
20     public static short getUnsignedByte(ByteBuffer bb) {
21         return ((short) (bb.get() & (short) 0xff));
22     }
23
24     /**
25      * Get an unsigned byte from the specified offset in the ByteBuffer
26      *
27      * @param bb ByteBuffer to get the byte from
28      * @param offset the offset to get the byte from
29      * @return an unsigned byte contained in a short
30      */
31     public static short getUnsignedByte(ByteBuffer bb, int offset) {
32         return ((short) (bb.get(offset) & (short) 0xff));
33     }
34
35     /**
36      * Put an unsigned byte into the specified ByteBuffer at the current
37      * position
38      *
39      * @param bb ByteBuffer to put the byte into
40      * @param v the short containing the unsigned byte
41      */
42     public static void putUnsignedByte(ByteBuffer bb, short v) {
43         bb.put((byte) (v & 0xff));
44     }
45
46     /**
47      * Put an unsigned byte into the specified ByteBuffer at the specified
48      * offset
49      *
50      * @param bb ByteBuffer to put the byte into
51      * @param v the short containing the unsigned byte
52      * @param offset the offset to insert the unsigned byte at
53      */
54     public static void putUnsignedByte(ByteBuffer bb, short v, int offset) {
55         bb.put(offset, (byte) (v & 0xff));
56     }
57
58     /**
59      * Get an unsigned short from the current position of the ByteBuffer
60      *
61      * @param bb ByteBuffer to get the byte from
62      * @return an unsigned short contained in a int
63      */
64     public static int getUnsignedShort(ByteBuffer bb) {
65         return (bb.getShort() & 0xffff);
66     }
67
68     /**
69      * Get an unsigned short from the specified offset in the ByteBuffer
70      *
71      * @param bb ByteBuffer to get the short from
72      * @param offset the offset to get the short from
73      * @return an unsigned short contained in a int
74      */
75     public static int getUnsignedShort(ByteBuffer bb, int offset) {
76         return (bb.getShort(offset) & 0xffff);
77     }
78
79     /**
80      * Put an unsigned short into the specified ByteBuffer at the current
81      * position
82      *
83      * @param bb ByteBuffer to put the short into
84      * @param v the int containing the unsigned short
85      */
86     public static void putUnsignedShort(ByteBuffer bb, int v) {
87         bb.putShort((short) (v & 0xffff));
88     }
89
90     /**
91      * Put an unsigned short into the specified ByteBuffer at the specified
92      * offset
93      *
94      * @param bb ByteBuffer to put the short into
95      * @param v the int containing the unsigned short
96      * @param offset the offset to insert the unsigned short at
97      */
98     public static void putUnsignedShort(ByteBuffer bb, int v, int offset) {
99         bb.putShort(offset, (short) (v & 0xffff));
100     }
101
102     /**
103      * Get an unsigned int from the current position of the ByteBuffer
104      *
105      * @param bb ByteBuffer to get the int from
106      * @return an unsigned int contained in a long
107      */
108     public static long getUnsignedInt(ByteBuffer bb) {
109         return ((long) bb.getInt() & 0xffffffffL);
110     }
111
112     /**
113      * Get an unsigned int from the specified offset in the ByteBuffer
114      *
115      * @param bb ByteBuffer to get the int from
116      * @param offset the offset to get the int from
117      * @return an unsigned int contained in a long
118      */
119     public static long getUnsignedInt(ByteBuffer bb, int offset) {
120         return ((long) bb.getInt(offset) & 0xffffffffL);
121     }
122
123     /**
124      * Put an unsigned int into the specified ByteBuffer at the current position
125      *
126      * @param bb ByteBuffer to put the int into
127      * @param v the long containing the unsigned int
128      */
129     public static void putUnsignedInt(ByteBuffer bb, long v) {
130         bb.putInt((int) (v & 0xffffffffL));
131     }
132
133     /**
134      * Put an unsigned int into the specified ByteBuffer at the specified offset
135      *
136      * @param bb ByteBuffer to put the int into
137      * @param v the long containing the unsigned int
138      * @param offset the offset to insert the unsigned int at
139      */
140     public static void putUnsignedInt(ByteBuffer bb, long v, int offset) {
141         bb.putInt(offset, (int) (v & 0xffffffffL));
142     }
143
144     /**
145      * Get an unsigned long from the current position of the ByteBuffer
146      *
147      * @param bb ByteBuffer to get the long from
148      * @return an unsigned long contained in a BigInteger
149      */
150     public static BigInteger getUnsignedLong(ByteBuffer bb) {
151         byte[] v = new byte[8];
152         for (int i = 0; i < 8; ++i) {
153             v[i] = bb.get(i);
154         }
155         return new BigInteger(1, v);
156     }
157
158     /**
159      * Get an unsigned long from the specified offset in the ByteBuffer
160      *
161      * @param bb ByteBuffer to get the long from
162      * @param offset the offset to get the long from
163      * @return an unsigned long contained in a BigInteger
164      */
165     public static BigInteger getUnsignedLong(ByteBuffer bb, int offset) {
166         byte[] v = new byte[8];
167         for (int i = 0; i < 8; ++i) {
168             v[i] = bb.get(offset+i);
169         }
170         return new BigInteger(1, v);
171     }
172
173     /**
174      * Put an unsigned long into the specified ByteBuffer at the current
175      * position
176      *
177      * @param bb ByteBuffer to put the long into
178      * @param v the BigInteger containing the unsigned long
179      */
180     public static void putUnsignedLong(ByteBuffer bb, BigInteger v) {
181         bb.putLong(v.longValue());
182     }
183
184     /**
185      * Put an unsigned long into the specified ByteBuffer at the specified
186      * offset
187      *
188      * @param bb  ByteBuffer to put the long into
189      * @param v the BigInteger containing the unsigned long
190      * @param offset the offset to insert the unsigned long at
191      */
192     public static void putUnsignedLong(ByteBuffer bb, BigInteger v, int offset) {
193         bb.putLong(offset, v.longValue());
194     }
195 }