2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
18 package org.openflow.util;
20 import java.math.BigInteger;
21 import java.nio.ByteBuffer;
24 * A util library class for dealing with the lack of unsigned datatypes in Java
26 * @author Rob Sherwood (rob.sherwood@stanford.edu)
27 * @author David Erickson (daviderickson@cs.stanford.edu)
30 public class Unsigned {
32 * Get an unsigned byte from the current position of the ByteBuffer
34 * @param bb ByteBuffer to get the byte from
35 * @return an unsigned byte contained in a short
37 public static short getUnsignedByte(ByteBuffer bb) {
38 return ((short) (bb.get() & (short) 0xff));
42 * Get an unsigned byte from the specified offset in the ByteBuffer
44 * @param bb ByteBuffer to get the byte from
45 * @param offset the offset to get the byte from
46 * @return an unsigned byte contained in a short
48 public static short getUnsignedByte(ByteBuffer bb, int offset) {
49 return ((short) (bb.get(offset) & (short) 0xff));
53 * Put an unsigned byte into the specified ByteBuffer at the current
56 * @param bb ByteBuffer to put the byte into
57 * @param v the short containing the unsigned byte
59 public static void putUnsignedByte(ByteBuffer bb, short v) {
60 bb.put((byte) (v & 0xff));
64 * Put an unsigned byte into the specified ByteBuffer at the specified
67 * @param bb ByteBuffer to put the byte into
68 * @param v the short containing the unsigned byte
69 * @param offset the offset to insert the unsigned byte at
71 public static void putUnsignedByte(ByteBuffer bb, short v, int offset) {
72 bb.put(offset, (byte) (v & 0xff));
76 * Get an unsigned short from the current position of the ByteBuffer
78 * @param bb ByteBuffer to get the byte from
79 * @return an unsigned short contained in a int
81 public static int getUnsignedShort(ByteBuffer bb) {
82 return (bb.getShort() & 0xffff);
86 * Get an unsigned short from the specified offset in the ByteBuffer
88 * @param bb ByteBuffer to get the short from
89 * @param offset the offset to get the short from
90 * @return an unsigned short contained in a int
92 public static int getUnsignedShort(ByteBuffer bb, int offset) {
93 return (bb.getShort(offset) & 0xffff);
97 * Put an unsigned short into the specified ByteBuffer at the current
100 * @param bb ByteBuffer to put the short into
101 * @param v the int containing the unsigned short
103 public static void putUnsignedShort(ByteBuffer bb, int v) {
104 bb.putShort((short) (v & 0xffff));
108 * Put an unsigned short into the specified ByteBuffer at the specified
111 * @param bb ByteBuffer to put the short into
112 * @param v the int containing the unsigned short
113 * @param offset the offset to insert the unsigned short at
115 public static void putUnsignedShort(ByteBuffer bb, int v, int offset) {
116 bb.putShort(offset, (short) (v & 0xffff));
120 * Get an unsigned int from the current position of the ByteBuffer
122 * @param bb ByteBuffer to get the int from
123 * @return an unsigned int contained in a long
125 public static long getUnsignedInt(ByteBuffer bb) {
126 return ((long) bb.getInt() & 0xffffffffL);
130 * Get an unsigned int from the specified offset in the ByteBuffer
132 * @param bb ByteBuffer to get the int from
133 * @param offset the offset to get the int from
134 * @return an unsigned int contained in a long
136 public static long getUnsignedInt(ByteBuffer bb, int offset) {
137 return ((long) bb.getInt(offset) & 0xffffffffL);
141 * Put an unsigned int into the specified ByteBuffer at the current position
143 * @param bb ByteBuffer to put the int into
144 * @param v the long containing the unsigned int
146 public static void putUnsignedInt(ByteBuffer bb, long v) {
147 bb.putInt((int) (v & 0xffffffffL));
151 * Put an unsigned int into the specified ByteBuffer at the specified offset
153 * @param bb ByteBuffer to put the int into
154 * @param v the long containing the unsigned int
155 * @param offset the offset to insert the unsigned int at
157 public static void putUnsignedInt(ByteBuffer bb, long v, int offset) {
158 bb.putInt(offset, (int) (v & 0xffffffffL));
162 * Get an unsigned long from the current position of the ByteBuffer
164 * @param bb ByteBuffer to get the long from
165 * @return an unsigned long contained in a BigInteger
167 public static BigInteger getUnsignedLong(ByteBuffer bb) {
168 byte[] v = new byte[8];
169 for (int i = 0; i < 8; ++i) {
172 return new BigInteger(1, v);
176 * Get an unsigned long from the specified offset in the ByteBuffer
178 * @param bb ByteBuffer to get the long from
179 * @param offset the offset to get the long from
180 * @return an unsigned long contained in a BigInteger
182 public static BigInteger getUnsignedLong(ByteBuffer bb, int offset) {
183 byte[] v = new byte[8];
184 for (int i = 0; i < 8; ++i) {
185 v[i] = bb.get(offset+i);
187 return new BigInteger(1, v);
191 * Put an unsigned long into the specified ByteBuffer at the current
194 * @param bb ByteBuffer to put the long into
195 * @param v the BigInteger containing the unsigned long
197 public static void putUnsignedLong(ByteBuffer bb, BigInteger v) {
198 bb.putLong(v.longValue());
202 * Put an unsigned long into the specified ByteBuffer at the specified
205 * @param bb ByteBuffer to put the long into
206 * @param v the BigInteger containing the unsigned long
207 * @param offset the offset to insert the unsigned long at
209 public static void putUnsignedLong(ByteBuffer bb, BigInteger v, int offset) {
210 bb.putLong(offset, v.longValue());