1 package org.openflow.util;
3 import java.math.BigInteger;
4 import java.nio.ByteBuffer;
7 * A util library class for dealing with the lack of unsigned datatypes in Java
9 * @author Rob Sherwood (rob.sherwood@stanford.edu)
10 * @author David Erickson (daviderickson@cs.stanford.edu)
13 public class Unsigned {
15 * Get an unsigned byte from the current position of the ByteBuffer
17 * @param bb ByteBuffer to get the byte from
18 * @return an unsigned byte contained in a short
20 public static short getUnsignedByte(ByteBuffer bb) {
21 return ((short) (bb.get() & (short) 0xff));
25 * Get an unsigned byte from the specified offset in the ByteBuffer
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
31 public static short getUnsignedByte(ByteBuffer bb, int offset) {
32 return ((short) (bb.get(offset) & (short) 0xff));
36 * Put an unsigned byte into the specified ByteBuffer at the current
39 * @param bb ByteBuffer to put the byte into
40 * @param v the short containing the unsigned byte
42 public static void putUnsignedByte(ByteBuffer bb, short v) {
43 bb.put((byte) (v & 0xff));
47 * Put an unsigned byte into the specified ByteBuffer at the specified
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
54 public static void putUnsignedByte(ByteBuffer bb, short v, int offset) {
55 bb.put(offset, (byte) (v & 0xff));
59 * Get an unsigned short from the current position of the ByteBuffer
61 * @param bb ByteBuffer to get the byte from
62 * @return an unsigned short contained in a int
64 public static int getUnsignedShort(ByteBuffer bb) {
65 return (bb.getShort() & 0xffff);
69 * Get an unsigned short from the specified offset in the ByteBuffer
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
75 public static int getUnsignedShort(ByteBuffer bb, int offset) {
76 return (bb.getShort(offset) & 0xffff);
80 * Put an unsigned short into the specified ByteBuffer at the current
83 * @param bb ByteBuffer to put the short into
84 * @param v the int containing the unsigned short
86 public static void putUnsignedShort(ByteBuffer bb, int v) {
87 bb.putShort((short) (v & 0xffff));
91 * Put an unsigned short into the specified ByteBuffer at the specified
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
98 public static void putUnsignedShort(ByteBuffer bb, int v, int offset) {
99 bb.putShort(offset, (short) (v & 0xffff));
103 * Get an unsigned int from the current position of the ByteBuffer
105 * @param bb ByteBuffer to get the int from
106 * @return an unsigned int contained in a long
108 public static long getUnsignedInt(ByteBuffer bb) {
109 return ((long) bb.getInt() & 0xffffffffL);
113 * Get an unsigned int from the specified offset in the ByteBuffer
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
119 public static long getUnsignedInt(ByteBuffer bb, int offset) {
120 return ((long) bb.getInt(offset) & 0xffffffffL);
124 * Put an unsigned int into the specified ByteBuffer at the current position
126 * @param bb ByteBuffer to put the int into
127 * @param v the long containing the unsigned int
129 public static void putUnsignedInt(ByteBuffer bb, long v) {
130 bb.putInt((int) (v & 0xffffffffL));
134 * Put an unsigned int into the specified ByteBuffer at the specified offset
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
140 public static void putUnsignedInt(ByteBuffer bb, long v, int offset) {
141 bb.putInt(offset, (int) (v & 0xffffffffL));
145 * Get an unsigned long from the current position of the ByteBuffer
147 * @param bb ByteBuffer to get the long from
148 * @return an unsigned long contained in a BigInteger
150 public static BigInteger getUnsignedLong(ByteBuffer bb) {
151 byte[] v = new byte[8];
152 for (int i = 0; i < 8; ++i) {
155 return new BigInteger(1, v);
159 * Get an unsigned long from the specified offset in the ByteBuffer
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
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);
170 return new BigInteger(1, v);
174 * Put an unsigned long into the specified ByteBuffer at the current
177 * @param bb ByteBuffer to put the long into
178 * @param v the BigInteger containing the unsigned long
180 public static void putUnsignedLong(ByteBuffer bb, BigInteger v) {
181 bb.putLong(v.longValue());
185 * Put an unsigned long into the specified ByteBuffer at the specified
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
192 public static void putUnsignedLong(ByteBuffer bb, BigInteger v, int offset) {
193 bb.putLong(offset, v.longValue());