Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / nio / ByteBufferHelper.java
blob799d41c2a04e4967ef1c1d46c3191eb0737467d3
1 /* ByteBufferImpl.java --
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
38 package java.nio;
40 /**
41 * @author Michael Koch (konqueror@gmx.de)
43 final class ByteBufferHelper
45 public static char getChar (ByteBuffer buffer, ByteOrder order)
47 return (char) getShort (buffer, order);
50 public static void putChar (ByteBuffer buffer, char value, ByteOrder order)
52 putShort (buffer, (short) value, order);
55 public static char getChar (ByteBuffer buffer, int index, ByteOrder order)
57 return (char) getShort (buffer, index, order);
60 public static void putChar (ByteBuffer buffer, int index,
61 char value, ByteOrder order)
63 putShort (buffer, index, (short) value, order);
66 public static short getShort (ByteBuffer buffer, ByteOrder order)
68 buffer.checkForUnderflow(2);
70 if (order == ByteOrder.LITTLE_ENDIAN)
72 return (short) ((buffer.get() & 0xff)
73 + (buffer.get() << 8));
76 return (short) ((buffer.get() << 8)
77 + (buffer.get() & 0xff));
80 public static void putShort (ByteBuffer buffer, short value, ByteOrder order)
82 buffer.checkForOverflow(2);
84 if (order == ByteOrder.LITTLE_ENDIAN)
86 buffer.put ((byte) value);
87 buffer.put ((byte) (value >> 8));
89 else
91 buffer.put ((byte) (value >> 8));
92 buffer.put ((byte) value);
96 public static short getShort (ByteBuffer buffer,
97 int index, ByteOrder order)
99 if (order == ByteOrder.LITTLE_ENDIAN)
101 return (short) ((buffer.get (index) & 0xff)
102 + (buffer.get (++index) << 8));
105 return (short) ((buffer.get (index) << 8)
106 + (buffer.get (++index) & 0xff));
109 public static void putShort (ByteBuffer buffer, int index,
110 short value, ByteOrder order)
112 if (order == ByteOrder.LITTLE_ENDIAN)
114 buffer.put (index, (byte) value);
115 buffer.put (++index, (byte) (value >> 8));
117 else
119 buffer.put (index, (byte) (value >> 8));
120 buffer.put (++index, (byte) value);
124 public static int getInt (ByteBuffer buffer, ByteOrder order)
126 buffer.checkForUnderflow(4);
128 if (order == ByteOrder.LITTLE_ENDIAN)
130 return ((buffer.get() & 0xff)
131 + ((buffer.get() & 0xff) << 8)
132 + ((buffer.get() & 0xff) << 16)
133 + (buffer.get() << 24));
136 return (int) ((buffer.get() << 24)
137 + ((buffer.get() & 0xff) << 16)
138 + ((buffer.get() & 0xff) << 8)
139 + (buffer.get() & 0xff));
142 public static void putInt (ByteBuffer buffer, int value, ByteOrder order)
144 buffer.checkForOverflow(4);
146 if (order == ByteOrder.LITTLE_ENDIAN)
148 buffer.put ((byte) value);
149 buffer.put ((byte) (value >> 8));
150 buffer.put ((byte) (value >> 16));
151 buffer.put ((byte) (value >> 24));
153 else
155 buffer.put ((byte) (value >> 24));
156 buffer.put ((byte) (value >> 16));
157 buffer.put ((byte) (value >> 8));
158 buffer.put ((byte) value);
162 public static int getInt (ByteBuffer buffer, int index, ByteOrder order)
164 if (order == ByteOrder.LITTLE_ENDIAN)
166 return ((buffer.get (index) & 0xff)
167 + ((buffer.get (++index) & 0xff) << 8)
168 + ((buffer.get (++index) & 0xff) << 16)
169 + (buffer.get (++index) << 24));
172 return ((buffer.get (index) << 24)
173 + ((buffer.get (++index) & 0xff) << 16)
174 + ((buffer.get (++index) & 0xff) << 8)
175 + (buffer.get (++index) & 0xff));
178 public static void putInt (ByteBuffer buffer, int index,
179 int value, ByteOrder order)
181 if (order == ByteOrder.LITTLE_ENDIAN)
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));
188 else
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);
197 public static long getLong (ByteBuffer buffer, ByteOrder order)
199 buffer.checkForUnderflow(8);
201 if (order == ByteOrder.LITTLE_ENDIAN)
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));
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));
223 public static void putLong (ByteBuffer buffer, long value, ByteOrder order)
225 buffer.checkForOverflow(8);
227 if (order == ByteOrder.LITTLE_ENDIAN)
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));
238 else
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);
251 public static long getLong (ByteBuffer buffer, int index, ByteOrder order)
253 if (order == ByteOrder.LITTLE_ENDIAN)
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));
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));
275 public static void putLong (ByteBuffer buffer, int index,
276 long value, ByteOrder order)
278 if (order == ByteOrder.LITTLE_ENDIAN)
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));
289 else
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);
302 public static float getFloat (ByteBuffer buffer, ByteOrder order)
304 return Float.intBitsToFloat (getInt (buffer, order));
307 public static void putFloat (ByteBuffer buffer, float value, ByteOrder order)
309 putInt (buffer, Float.floatToRawIntBits (value), order);
312 public static float getFloat (ByteBuffer buffer, int index, ByteOrder order)
314 return Float.intBitsToFloat (getInt (buffer, index, order));
317 public static void putFloat (ByteBuffer buffer, int index,
318 float value, ByteOrder order)
320 putInt (buffer, index, Float.floatToRawIntBits (value), order);
323 public static double getDouble (ByteBuffer buffer, ByteOrder order)
325 return Double.longBitsToDouble (getLong (buffer, order));
328 public static void putDouble (ByteBuffer buffer, double value, ByteOrder order)
330 putLong (buffer, Double.doubleToRawLongBits (value), order);
333 public static double getDouble (ByteBuffer buffer, int index, ByteOrder order)
335 return Double.longBitsToDouble (getLong (buffer, index, order));
338 public static void putDouble (ByteBuffer buffer, int index,
339 double value, ByteOrder order)
341 putLong (buffer, index, Double.doubleToRawLongBits (value), order);
343 } // ByteBufferHelper