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