1 package org.openflow.codec.util;
3 import java.math.BigInteger;
5 import org.openflow.codec.io.IDataBuffer;
8 * A util library class for dealing with the lack of unsigned datatypes in Java
10 * @author Rob Sherwood (rob.sherwood@stanford.edu)
11 * @author David Erickson (daviderickson@cs.stanford.edu)
14 public class Unsigned {
16 * Get an unsigned byte from the current position of the IDataBuffer
19 * IDataBuffer to get the byte from
20 * @return an unsigned byte contained in a short
22 public static short getUnsignedByte(IDataBuffer bb) {
23 return ((short) (bb.get() & (short) 0xff));
27 * Get an unsigned byte from the specified offset in the IDataBuffer
30 * IDataBuffer to get the byte from
32 * the offset to get the byte from
33 * @return an unsigned byte contained in a short
35 public static short getUnsignedByte(IDataBuffer bb, int offset) {
36 return ((short) (bb.get(offset) & (short) 0xff));
40 * Put an unsigned byte into the specified IDataBuffer at the current
44 * IDataBuffer to put the byte into
46 * the short containing the unsigned byte
48 public static void putUnsignedByte(IDataBuffer bb, short v) {
49 bb.put((byte) (v & 0xff));
53 * Put an unsigned byte into the specified IDataBuffer at the specified
57 * IDataBuffer to put the byte into
59 * the short containing the unsigned byte
61 * the offset to insert the unsigned byte at
63 public static void putUnsignedByte(IDataBuffer bb, short v, int offset) {
64 bb.put(offset, (byte) (v & 0xff));
68 * Get an unsigned short from the current position of the IDataBuffer
71 * IDataBuffer to get the byte from
72 * @return an unsigned short contained in a int
74 public static int getUnsignedShort(IDataBuffer bb) {
75 return (bb.getShort() & 0xffff);
79 * Get an unsigned short from the specified offset in the IDataBuffer
82 * IDataBuffer to get the short from
84 * the offset to get the short from
85 * @return an unsigned short contained in a int
87 public static int getUnsignedShort(IDataBuffer bb, int offset) {
88 return (bb.getShort(offset) & 0xffff);
92 * Put an unsigned short into the specified IDataBuffer at the current
96 * IDataBuffer to put the short into
98 * the int containing the unsigned short
100 public static void putUnsignedShort(IDataBuffer bb, int v) {
101 bb.putShort((short) (v & 0xffff));
105 * Put an unsigned short into the specified IDataBuffer at the specified
109 * IDataBuffer to put the short into
111 * the int containing the unsigned short
113 * the offset to insert the unsigned short at
115 public static void putUnsignedShort(IDataBuffer bb, int v, int offset) {
116 bb.putShort(offset, (short) (v & 0xffff));
120 * Get an unsigned int from the current position of the IDataBuffer
123 * IDataBuffer to get the int from
124 * @return an unsigned int contained in a long
126 public static long getUnsignedInt(IDataBuffer bb) {
127 return ((long) bb.getInt() & 0xffffffffL);
131 * Get an unsigned int from the specified offset in the IDataBuffer
134 * IDataBuffer to get the int from
136 * the offset to get the int from
137 * @return an unsigned int contained in a long
139 public static long getUnsignedInt(IDataBuffer bb, int offset) {
140 return ((long) bb.getInt(offset) & 0xffffffffL);
144 * Put an unsigned int into the specified IDataBuffer at the current
148 * IDataBuffer to put the int into
150 * the long containing the unsigned int
152 public static void putUnsignedInt(IDataBuffer bb, long v) {
153 bb.putInt((int) (v & 0xffffffffL));
157 * Put an unsigned int into the specified IDataBuffer at the specified
161 * IDataBuffer to put the int into
163 * the long containing the unsigned int
165 * the offset to insert the unsigned int at
167 public static void putUnsignedInt(IDataBuffer bb, long v, int offset) {
168 bb.putInt(offset, (int) (v & 0xffffffffL));
172 * Get an unsigned long from the current position of the IDataBuffer
175 * IDataBuffer to get the long from
176 * @return an unsigned long contained in a BigInteger
178 public static BigInteger getUnsignedLong(IDataBuffer bb) {
179 byte[] v = new byte[8];
180 for (int i = 0; i < 8; ++i) {
183 return new BigInteger(1, v);
187 * Get an unsigned long from the specified offset in the IDataBuffer
190 * IDataBuffer to get the long from
192 * the offset to get the long from
193 * @return an unsigned long contained in a BigInteger
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);
200 return new BigInteger(1, v);
204 * Put an unsigned long into the specified IDataBuffer at the current
208 * IDataBuffer to put the long into
210 * the BigInteger containing the unsigned long
212 public static void putUnsignedLong(IDataBuffer bb, BigInteger v) {
213 bb.putLong(v.longValue());
217 * Put an unsigned long into the specified IDataBuffer at the specified
221 * IDataBuffer to put the long into
223 * the BigInteger containing the unsigned long
225 * the offset to insert the unsigned long at
227 public static void putUnsignedLong(IDataBuffer bb, BigInteger v, int offset) {
228 bb.putLong(offset, v.longValue());