1 |
771 |
jeremybenn |
/* ByteBufferImpl.java --
|
2 |
|
|
Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package java.nio;
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* @author Michael Koch (konqueror@gmx.de)
|
42 |
|
|
*/
|
43 |
|
|
final class ByteBufferHelper
|
44 |
|
|
{
|
45 |
|
|
public static char getChar (ByteBuffer buffer, ByteOrder order)
|
46 |
|
|
{
|
47 |
|
|
return (char) getShort (buffer, order);
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
public static void putChar (ByteBuffer buffer, char value, ByteOrder order)
|
51 |
|
|
{
|
52 |
|
|
putShort (buffer, (short) value, order);
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
public static char getChar (ByteBuffer buffer, int index, ByteOrder order)
|
56 |
|
|
{
|
57 |
|
|
return (char) getShort (buffer, index, order);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
public static void putChar (ByteBuffer buffer, int index,
|
61 |
|
|
char value, ByteOrder order)
|
62 |
|
|
{
|
63 |
|
|
putShort (buffer, index, (short) value, order);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
public static short getShort (ByteBuffer buffer, ByteOrder order)
|
67 |
|
|
{
|
68 |
|
|
buffer.checkForUnderflow(2);
|
69 |
|
|
|
70 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
71 |
|
|
{
|
72 |
|
|
return (short) ((buffer.get() & 0xff)
|
73 |
|
|
+ (buffer.get() << 8));
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
return (short) ((buffer.get() << 8)
|
77 |
|
|
+ (buffer.get() & 0xff));
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
public static void putShort (ByteBuffer buffer, short value, ByteOrder order)
|
81 |
|
|
{
|
82 |
|
|
buffer.checkForOverflow(2);
|
83 |
|
|
|
84 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
85 |
|
|
{
|
86 |
|
|
buffer.put ((byte) value);
|
87 |
|
|
buffer.put ((byte) (value >> 8));
|
88 |
|
|
}
|
89 |
|
|
else
|
90 |
|
|
{
|
91 |
|
|
buffer.put ((byte) (value >> 8));
|
92 |
|
|
buffer.put ((byte) value);
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
public static short getShort (ByteBuffer buffer,
|
97 |
|
|
int index, ByteOrder order)
|
98 |
|
|
{
|
99 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
100 |
|
|
{
|
101 |
|
|
return (short) ((buffer.get (index) & 0xff)
|
102 |
|
|
+ (buffer.get (++index) << 8));
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
return (short) ((buffer.get (index) << 8)
|
106 |
|
|
+ (buffer.get (++index) & 0xff));
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
public static void putShort (ByteBuffer buffer, int index,
|
110 |
|
|
short value, ByteOrder order)
|
111 |
|
|
{
|
112 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
113 |
|
|
{
|
114 |
|
|
buffer.put (index, (byte) value);
|
115 |
|
|
buffer.put (++index, (byte) (value >> 8));
|
116 |
|
|
}
|
117 |
|
|
else
|
118 |
|
|
{
|
119 |
|
|
buffer.put (index, (byte) (value >> 8));
|
120 |
|
|
buffer.put (++index, (byte) value);
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
public static int getInt (ByteBuffer buffer, ByteOrder order)
|
125 |
|
|
{
|
126 |
|
|
buffer.checkForUnderflow(4);
|
127 |
|
|
|
128 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
129 |
|
|
{
|
130 |
|
|
return ((buffer.get() & 0xff)
|
131 |
|
|
+ ((buffer.get() & 0xff) << 8)
|
132 |
|
|
+ ((buffer.get() & 0xff) << 16)
|
133 |
|
|
+ (buffer.get() << 24));
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
return (int) ((buffer.get() << 24)
|
137 |
|
|
+ ((buffer.get() & 0xff) << 16)
|
138 |
|
|
+ ((buffer.get() & 0xff) << 8)
|
139 |
|
|
+ (buffer.get() & 0xff));
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
public static void putInt (ByteBuffer buffer, int value, ByteOrder order)
|
143 |
|
|
{
|
144 |
|
|
buffer.checkForOverflow(4);
|
145 |
|
|
|
146 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
147 |
|
|
{
|
148 |
|
|
buffer.put ((byte) value);
|
149 |
|
|
buffer.put ((byte) (value >> 8));
|
150 |
|
|
buffer.put ((byte) (value >> 16));
|
151 |
|
|
buffer.put ((byte) (value >> 24));
|
152 |
|
|
}
|
153 |
|
|
else
|
154 |
|
|
{
|
155 |
|
|
buffer.put ((byte) (value >> 24));
|
156 |
|
|
buffer.put ((byte) (value >> 16));
|
157 |
|
|
buffer.put ((byte) (value >> 8));
|
158 |
|
|
buffer.put ((byte) value);
|
159 |
|
|
}
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
public static int getInt (ByteBuffer buffer, int index, ByteOrder order)
|
163 |
|
|
{
|
164 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
165 |
|
|
{
|
166 |
|
|
return ((buffer.get (index) & 0xff)
|
167 |
|
|
+ ((buffer.get (++index) & 0xff) << 8)
|
168 |
|
|
+ ((buffer.get (++index) & 0xff) << 16)
|
169 |
|
|
+ (buffer.get (++index) << 24));
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
return ((buffer.get (index) << 24)
|
173 |
|
|
+ ((buffer.get (++index) & 0xff) << 16)
|
174 |
|
|
+ ((buffer.get (++index) & 0xff) << 8)
|
175 |
|
|
+ (buffer.get (++index) & 0xff));
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
public static void putInt (ByteBuffer buffer, int index,
|
179 |
|
|
int value, ByteOrder order)
|
180 |
|
|
{
|
181 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
182 |
|
|
{
|
183 |
|
|
buffer.put (index, (byte) value);
|
184 |
|
|
buffer.put (++index, (byte) (value >> 8));
|
185 |
|
|
buffer.put (++index, (byte) (value >> 16));
|
186 |
|
|
buffer.put (++index, (byte) (value >> 24));
|
187 |
|
|
}
|
188 |
|
|
else
|
189 |
|
|
{
|
190 |
|
|
buffer.put (index, (byte) (value >> 24));
|
191 |
|
|
buffer.put (++index, (byte) (value >> 16));
|
192 |
|
|
buffer.put (++index, (byte) (value >> 8));
|
193 |
|
|
buffer.put (++index, (byte) value);
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
public static long getLong (ByteBuffer buffer, ByteOrder order)
|
198 |
|
|
{
|
199 |
|
|
buffer.checkForUnderflow(8);
|
200 |
|
|
|
201 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
202 |
|
|
{
|
203 |
|
|
return ((buffer.get() & 0xff)
|
204 |
|
|
+ (((buffer.get() & 0xff)) << 8)
|
205 |
|
|
+ (((buffer.get() & 0xff)) << 16)
|
206 |
|
|
+ (((buffer.get() & 0xffL)) << 24)
|
207 |
|
|
+ (((buffer.get() & 0xffL)) << 32)
|
208 |
|
|
+ (((buffer.get() & 0xffL)) << 40)
|
209 |
|
|
+ (((buffer.get() & 0xffL)) << 48)
|
210 |
|
|
+ (((long) buffer.get()) << 56));
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
return ((((long) buffer.get()) << 56)
|
214 |
|
|
+ ((buffer.get() & 0xffL) << 48)
|
215 |
|
|
+ ((buffer.get() & 0xffL) << 40)
|
216 |
|
|
+ ((buffer.get() & 0xffL) << 32)
|
217 |
|
|
+ ((buffer.get() & 0xffL) << 24)
|
218 |
|
|
+ ((buffer.get() & 0xff) << 16)
|
219 |
|
|
+ ((buffer.get() & 0xff) << 8)
|
220 |
|
|
+ (buffer.get() & 0xff));
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
public static void putLong (ByteBuffer buffer, long value, ByteOrder order)
|
224 |
|
|
{
|
225 |
|
|
buffer.checkForOverflow(8);
|
226 |
|
|
|
227 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
228 |
|
|
{
|
229 |
|
|
buffer.put ((byte) value);
|
230 |
|
|
buffer.put ((byte) (value >> 8));
|
231 |
|
|
buffer.put ((byte) (value >> 16));
|
232 |
|
|
buffer.put ((byte) (value >> 24));
|
233 |
|
|
buffer.put ((byte) (value >> 32));
|
234 |
|
|
buffer.put ((byte) (value >> 40));
|
235 |
|
|
buffer.put ((byte) (value >> 48));
|
236 |
|
|
buffer.put ((byte) (value >> 56));
|
237 |
|
|
}
|
238 |
|
|
else
|
239 |
|
|
{
|
240 |
|
|
buffer.put ((byte) (value >> 56));
|
241 |
|
|
buffer.put ((byte) (value >> 48));
|
242 |
|
|
buffer.put ((byte) (value >> 40));
|
243 |
|
|
buffer.put ((byte) (value >> 32));
|
244 |
|
|
buffer.put ((byte) (value >> 24));
|
245 |
|
|
buffer.put ((byte) (value >> 16));
|
246 |
|
|
buffer.put ((byte) (value >> 8));
|
247 |
|
|
buffer.put ((byte) value);
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
public static long getLong (ByteBuffer buffer, int index, ByteOrder order)
|
252 |
|
|
{
|
253 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
254 |
|
|
{
|
255 |
|
|
return ((buffer.get (index) & 0xff)
|
256 |
|
|
+ ((buffer.get (++index) & 0xff) << 8)
|
257 |
|
|
+ ((buffer.get (++index) & 0xff) << 16)
|
258 |
|
|
+ ((buffer.get (++index) & 0xffL) << 24)
|
259 |
|
|
+ ((buffer.get (++index) & 0xffL) << 32)
|
260 |
|
|
+ ((buffer.get (++index) & 0xffL) << 40)
|
261 |
|
|
+ ((buffer.get (++index) & 0xffL) << 48)
|
262 |
|
|
+ (((long) buffer.get (++index)) << 56));
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
return ((((long) buffer.get (index)) << 56)
|
266 |
|
|
+ ((buffer.get (++index) & 0xffL) << 48)
|
267 |
|
|
+ ((buffer.get (++index) & 0xffL) << 40)
|
268 |
|
|
+ ((buffer.get (++index) & 0xffL) << 32)
|
269 |
|
|
+ ((buffer.get (++index) & 0xffL) << 24)
|
270 |
|
|
+ ((buffer.get (++index) & 0xff) << 16)
|
271 |
|
|
+ ((buffer.get (++index) & 0xff) << 8)
|
272 |
|
|
+ (buffer.get (++index) & 0xff));
|
273 |
|
|
}
|
274 |
|
|
|
275 |
|
|
public static void putLong (ByteBuffer buffer, int index,
|
276 |
|
|
long value, ByteOrder order)
|
277 |
|
|
{
|
278 |
|
|
if (order == ByteOrder.LITTLE_ENDIAN)
|
279 |
|
|
{
|
280 |
|
|
buffer.put (index, (byte) value);
|
281 |
|
|
buffer.put (++index, (byte) (value >> 8));
|
282 |
|
|
buffer.put (++index, (byte) (value >> 16));
|
283 |
|
|
buffer.put (++index, (byte) (value >> 24));
|
284 |
|
|
buffer.put (++index, (byte) (value >> 32));
|
285 |
|
|
buffer.put (++index, (byte) (value >> 40));
|
286 |
|
|
buffer.put (++index, (byte) (value >> 48));
|
287 |
|
|
buffer.put (++index, (byte) (value >> 56));
|
288 |
|
|
}
|
289 |
|
|
else
|
290 |
|
|
{
|
291 |
|
|
buffer.put (index, (byte) (value >> 56));
|
292 |
|
|
buffer.put (++index, (byte) (value >> 48));
|
293 |
|
|
buffer.put (++index, (byte) (value >> 40));
|
294 |
|
|
buffer.put (++index, (byte) (value >> 32));
|
295 |
|
|
buffer.put (++index, (byte) (value >> 24));
|
296 |
|
|
buffer.put (++index, (byte) (value >> 16));
|
297 |
|
|
buffer.put (++index, (byte) (value >> 8));
|
298 |
|
|
buffer.put (++index, (byte) value);
|
299 |
|
|
}
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
public static float getFloat (ByteBuffer buffer, ByteOrder order)
|
303 |
|
|
{
|
304 |
|
|
return Float.intBitsToFloat (getInt (buffer, order));
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
public static void putFloat (ByteBuffer buffer, float value, ByteOrder order)
|
308 |
|
|
{
|
309 |
|
|
putInt (buffer, Float.floatToRawIntBits (value), order);
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
public static float getFloat (ByteBuffer buffer, int index, ByteOrder order)
|
313 |
|
|
{
|
314 |
|
|
return Float.intBitsToFloat (getInt (buffer, index, order));
|
315 |
|
|
}
|
316 |
|
|
|
317 |
|
|
public static void putFloat (ByteBuffer buffer, int index,
|
318 |
|
|
float value, ByteOrder order)
|
319 |
|
|
{
|
320 |
|
|
putInt (buffer, index, Float.floatToRawIntBits (value), order);
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
public static double getDouble (ByteBuffer buffer, ByteOrder order)
|
324 |
|
|
{
|
325 |
|
|
return Double.longBitsToDouble (getLong (buffer, order));
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
public static void putDouble (ByteBuffer buffer, double value, ByteOrder order)
|
329 |
|
|
{
|
330 |
|
|
putLong (buffer, Double.doubleToRawLongBits (value), order);
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
public static double getDouble (ByteBuffer buffer, int index, ByteOrder order)
|
334 |
|
|
{
|
335 |
|
|
return Double.longBitsToDouble (getLong (buffer, index, order));
|
336 |
|
|
}
|
337 |
|
|
|
338 |
|
|
public static void putDouble (ByteBuffer buffer, int index,
|
339 |
|
|
double value, ByteOrder order)
|
340 |
|
|
{
|
341 |
|
|
putLong (buffer, index, Double.doubleToRawLongBits (value), order);
|
342 |
|
|
}
|
343 |
|
|
} // ByteBufferHelper
|