blob: 0754a3f73d751e0296ecd95d6671ec2db7c78dff [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3* University
4*
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
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
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
15* under the License.
16**/
17
18package org.openflow.util;
19
20import java.math.BigInteger;
21import java.nio.ByteBuffer;
22
23/*****
24 * A util library class for dealing with the lack of unsigned datatypes in Java
25 *
26 * @author Rob Sherwood (rob.sherwood@stanford.edu)
27 * @author David Erickson (daviderickson@cs.stanford.edu)
28 */
29
30public class Unsigned {
31 /**
32 * Get an unsigned byte from the current position of the ByteBuffer
33 *
34 * @param bb ByteBuffer to get the byte from
35 * @return an unsigned byte contained in a short
36 */
37 public static short getUnsignedByte(ByteBuffer bb) {
38 return ((short) (bb.get() & (short) 0xff));
39 }
40
41 /**
42 * Get an unsigned byte from the specified offset in the ByteBuffer
43 *
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
47 */
48 public static short getUnsignedByte(ByteBuffer bb, int offset) {
49 return ((short) (bb.get(offset) & (short) 0xff));
50 }
51
52 /**
53 * Put an unsigned byte into the specified ByteBuffer at the current
54 * position
55 *
56 * @param bb ByteBuffer to put the byte into
57 * @param v the short containing the unsigned byte
58 */
59 public static void putUnsignedByte(ByteBuffer bb, short v) {
60 bb.put((byte) (v & 0xff));
61 }
62
63 /**
64 * Put an unsigned byte into the specified ByteBuffer at the specified
65 * offset
66 *
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
70 */
71 public static void putUnsignedByte(ByteBuffer bb, short v, int offset) {
72 bb.put(offset, (byte) (v & 0xff));
73 }
74
75 /**
76 * Get an unsigned short from the current position of the ByteBuffer
77 *
78 * @param bb ByteBuffer to get the byte from
79 * @return an unsigned short contained in a int
80 */
81 public static int getUnsignedShort(ByteBuffer bb) {
82 return (bb.getShort() & 0xffff);
83 }
84
85 /**
86 * Get an unsigned short from the specified offset in the ByteBuffer
87 *
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
91 */
92 public static int getUnsignedShort(ByteBuffer bb, int offset) {
93 return (bb.getShort(offset) & 0xffff);
94 }
95
96 /**
97 * Put an unsigned short into the specified ByteBuffer at the current
98 * position
99 *
100 * @param bb ByteBuffer to put the short into
101 * @param v the int containing the unsigned short
102 */
103 public static void putUnsignedShort(ByteBuffer bb, int v) {
104 bb.putShort((short) (v & 0xffff));
105 }
106
107 /**
108 * Put an unsigned short into the specified ByteBuffer at the specified
109 * offset
110 *
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
114 */
115 public static void putUnsignedShort(ByteBuffer 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 ByteBuffer
121 *
122 * @param bb ByteBuffer to get the int from
123 * @return an unsigned int contained in a long
124 */
125 public static long getUnsignedInt(ByteBuffer bb) {
126 return ((long) bb.getInt() & 0xffffffffL);
127 }
128
129 /**
130 * Get an unsigned int from the specified offset in the ByteBuffer
131 *
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
135 */
136 public static long getUnsignedInt(ByteBuffer bb, int offset) {
137 return ((long) bb.getInt(offset) & 0xffffffffL);
138 }
139
140 /**
141 * Put an unsigned int into the specified ByteBuffer at the current position
142 *
143 * @param bb ByteBuffer to put the int into
144 * @param v the long containing the unsigned int
145 */
146 public static void putUnsignedInt(ByteBuffer bb, long v) {
147 bb.putInt((int) (v & 0xffffffffL));
148 }
149
150 /**
151 * Put an unsigned int into the specified ByteBuffer at the specified offset
152 *
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
156 */
157 public static void putUnsignedInt(ByteBuffer bb, long v, int offset) {
158 bb.putInt(offset, (int) (v & 0xffffffffL));
159 }
160
161 /**
162 * Get an unsigned long from the current position of the ByteBuffer
163 *
164 * @param bb ByteBuffer to get the long from
165 * @return an unsigned long contained in a BigInteger
166 */
167 public static BigInteger getUnsignedLong(ByteBuffer bb) {
168 byte[] v = new byte[8];
169 for (int i = 0; i < 8; ++i) {
170 v[i] = bb.get(i);
171 }
172 return new BigInteger(1, v);
173 }
174
175 /**
176 * Get an unsigned long from the specified offset in the ByteBuffer
177 *
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
181 */
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);
186 }
187 return new BigInteger(1, v);
188 }
189
190 /**
191 * Put an unsigned long into the specified ByteBuffer at the current
192 * position
193 *
194 * @param bb ByteBuffer to put the long into
195 * @param v the BigInteger containing the unsigned long
196 */
197 public static void putUnsignedLong(ByteBuffer bb, BigInteger v) {
198 bb.putLong(v.longValue());
199 }
200
201 /**
202 * Put an unsigned long into the specified ByteBuffer at the specified
203 * offset
204 *
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
208 */
209 public static void putUnsignedLong(ByteBuffer bb, BigInteger v, int offset) {
210 bb.putLong(offset, v.longValue());
211 }
212}