2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / nio / ByteBufferHelper.java
blob23e133c86b373c215d1939e145f232914b0b52d6
1 /* ByteBufferImpl.java --
2 Copyright (C) 2003 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 private static final void checkRemainingForRead (ByteBuffer buffer, int bytes)
47 if (buffer.remaining() < bytes)
48 throw new BufferUnderflowException();
51 private static final void checkRemainingForWrite (ByteBuffer buffer, int bytes)
53 if (buffer.remaining() < bytes)
54 throw new BufferOverflowException();
57 private static final void checkAvailableForRead (ByteBuffer buffer,
58 int index, int bytes)
60 if (buffer.limit() < (index + bytes))
61 throw new BufferUnderflowException();
64 private static final void checkAvailableForWrite (ByteBuffer buffer,
65 int index, int bytes)
67 if (buffer.limit() < (index + bytes))
68 throw new BufferOverflowException();
71 public static final char getChar (ByteBuffer buffer)
73 return (char) getShort (buffer);
76 public static final ByteBuffer putChar (ByteBuffer buffer, char value)
78 return putShort (buffer, (short) value);
81 public static final char getChar (ByteBuffer buffer, int index)
83 return (char) getShort (buffer, index);
86 public static final ByteBuffer putChar (ByteBuffer buffer, int index,
87 char value)
89 return putShort (buffer, index, (short) value);
92 public static final short getShort (ByteBuffer buffer)
94 checkRemainingForRead (buffer, 2);
96 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
98 return (short) ((buffer.get() & 0xff)
99 + (buffer.get() << 8));
102 return (short) ((buffer.get() << 8)
103 + (buffer.get() & 0xff));
106 public static final ByteBuffer putShort (ByteBuffer buffer, short value)
108 checkRemainingForWrite (buffer, 2);
110 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
112 buffer.put ((byte) value);
113 buffer.put ((byte) (value >> 8));
115 else
117 buffer.put ((byte) (value >> 8));
118 buffer.put ((byte) value);
121 return buffer;
124 public static final short getShort (ByteBuffer buffer, int index)
126 checkAvailableForRead (buffer, index, 2);
128 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
130 return (short) ((buffer.get (index) & 0xff)
131 + (buffer.get (++index) << 8));
134 return (short) ((buffer.get (index) << 8)
135 + (buffer.get (++index) & 0xff));
138 public static final ByteBuffer putShort (ByteBuffer buffer, int index,
139 short value)
141 checkAvailableForWrite (buffer, index, 2);
143 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
145 buffer.put (index, (byte) value);
146 buffer.put (++index, (byte) (value >> 8));
148 else
150 buffer.put (index, (byte) (value >> 8));
151 buffer.put (++index, (byte) value);
154 return buffer;
157 public static final int getInt (ByteBuffer buffer)
159 checkRemainingForRead (buffer, 4);
161 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
163 return ((buffer.get() & 0xff)
164 + ((buffer.get() & 0xff) << 8)
165 + ((buffer.get() & 0xff) << 16)
166 + (buffer.get() << 24));
169 return (int) ((buffer.get() << 24)
170 + ((buffer.get() & 0xff) << 16)
171 + ((buffer.get() & 0xff) << 8)
172 + (buffer.get() & 0xff));
175 public static final ByteBuffer putInt (ByteBuffer buffer, int value)
177 checkRemainingForWrite (buffer, 4);
179 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
181 buffer.put ((byte) value);
182 buffer.put ((byte) (value >> 8));
183 buffer.put ((byte) (value >> 16));
184 buffer.put ((byte) (value >> 24));
186 else
188 buffer.put ((byte) (value >> 24));
189 buffer.put ((byte) (value >> 16));
190 buffer.put ((byte) (value >> 8));
191 buffer.put ((byte) value);
194 return buffer;
197 public static final int getInt (ByteBuffer buffer, int index)
199 checkAvailableForRead (buffer, index, 4);
201 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
203 return ((buffer.get (index) & 0xff)
204 + ((buffer.get (++index) & 0xff) << 8)
205 + ((buffer.get (++index) & 0xff) << 16)
206 + (buffer.get (++index) << 24));
209 return ((buffer.get (index) << 24)
210 + ((buffer.get (++index) & 0xff) << 16)
211 + ((buffer.get (++index) & 0xff) << 8)
212 + (buffer.get (++index) & 0xff));
215 public static final ByteBuffer putInt (ByteBuffer buffer, int index,
216 int value)
218 checkAvailableForWrite (buffer, index, 4);
220 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
222 buffer.put (index, (byte) value);
223 buffer.put (++index, (byte) (value >> 8));
224 buffer.put (++index, (byte) (value >> 16));
225 buffer.put (++index, (byte) (value >> 24));
227 else
229 buffer.put (index, (byte) (value >> 24));
230 buffer.put (++index, (byte) (value >> 16));
231 buffer.put (++index, (byte) (value >> 8));
232 buffer.put (++index, (byte) value);
235 return buffer;
238 public static final long getLong (ByteBuffer buffer)
240 checkRemainingForRead (buffer, 8);
242 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
244 return ((buffer.get() & 0xff)
245 + (((buffer.get() & 0xff)) << 8)
246 + (((buffer.get() & 0xff)) << 16)
247 + (((buffer.get() & 0xffL)) << 24)
248 + (((buffer.get() & 0xffL)) << 32)
249 + (((buffer.get() & 0xffL)) << 40)
250 + (((buffer.get() & 0xffL)) << 48)
251 + (((long) buffer.get()) << 56));
254 return ((((long) buffer.get()) << 56)
255 + ((buffer.get() & 0xffL) << 48)
256 + ((buffer.get() & 0xffL) << 40)
257 + ((buffer.get() & 0xffL) << 32)
258 + ((buffer.get() & 0xffL) << 24)
259 + ((buffer.get() & 0xff) << 16)
260 + ((buffer.get() & 0xff) << 8)
261 + (buffer.get() & 0xff));
264 public static final ByteBuffer putLong (ByteBuffer buffer, long value)
266 checkRemainingForWrite (buffer, 8);
268 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
270 buffer.put ((byte) value);
271 buffer.put ((byte) (value >> 8));
272 buffer.put ((byte) (value >> 16));
273 buffer.put ((byte) (value >> 24));
274 buffer.put ((byte) (value >> 32));
275 buffer.put ((byte) (value >> 40));
276 buffer.put ((byte) (value >> 48));
277 buffer.put ((byte) (value >> 56));
279 else
281 buffer.put ((byte) (value >> 56));
282 buffer.put ((byte) (value >> 48));
283 buffer.put ((byte) (value >> 40));
284 buffer.put ((byte) (value >> 32));
285 buffer.put ((byte) (value >> 24));
286 buffer.put ((byte) (value >> 16));
287 buffer.put ((byte) (value >> 8));
288 buffer.put ((byte) value);
291 return buffer;
294 public static final long getLong (ByteBuffer buffer, int index)
296 checkAvailableForRead (buffer, index, 8);
298 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
300 return ((buffer.get (index) & 0xff)
301 + ((buffer.get (++index) & 0xff) << 8)
302 + ((buffer.get (++index) & 0xff) << 16)
303 + ((buffer.get (++index) & 0xffL) << 24)
304 + ((buffer.get (++index) & 0xffL) << 32)
305 + ((buffer.get (++index) & 0xffL) << 40)
306 + ((buffer.get (++index) & 0xffL) << 48)
307 + (((long) buffer.get (++index)) << 56));
310 return ((((long) buffer.get (index)) << 56)
311 + ((buffer.get (++index) & 0xffL) << 48)
312 + ((buffer.get (++index) & 0xffL) << 40)
313 + ((buffer.get (++index) & 0xffL) << 32)
314 + ((buffer.get (++index) & 0xffL) << 24)
315 + ((buffer.get (++index) & 0xff) << 16)
316 + ((buffer.get (++index) & 0xff) << 8)
317 + (buffer.get (++index) & 0xff));
320 public static final ByteBuffer putLong (ByteBuffer buffer, int index,
321 long value)
323 checkAvailableForWrite (buffer, index, 8);
325 if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
327 buffer.put (index, (byte) value);
328 buffer.put (++index, (byte) (value >> 8));
329 buffer.put (++index, (byte) (value >> 16));
330 buffer.put (++index, (byte) (value >> 24));
331 buffer.put (++index, (byte) (value >> 32));
332 buffer.put (++index, (byte) (value >> 40));
333 buffer.put (++index, (byte) (value >> 48));
334 buffer.put (++index, (byte) (value >> 56));
336 else
338 buffer.put (index, (byte) (value >> 56));
339 buffer.put (++index, (byte) (value >> 48));
340 buffer.put (++index, (byte) (value >> 40));
341 buffer.put (++index, (byte) (value >> 32));
342 buffer.put (++index, (byte) (value >> 24));
343 buffer.put (++index, (byte) (value >> 16));
344 buffer.put (++index, (byte) (value >> 8));
345 buffer.put (++index, (byte) value);
348 return buffer;
351 public static final float getFloat (ByteBuffer buffer)
353 return Float.intBitsToFloat (getInt (buffer));
356 public static final ByteBuffer putFloat (ByteBuffer buffer, float value)
358 return putInt (buffer, Float.floatToRawIntBits (value));
361 public static final float getFloat (ByteBuffer buffer, int index)
363 return Float.intBitsToFloat (getInt (buffer, index));
366 public static final ByteBuffer putFloat (ByteBuffer buffer, int index,
367 float value)
369 return putInt (buffer, index, Float.floatToRawIntBits (value));
372 public static final double getDouble (ByteBuffer buffer)
374 return Double.longBitsToDouble (getLong (buffer));
377 public static final ByteBuffer putDouble (ByteBuffer buffer, double value)
379 return putLong (buffer, Double.doubleToLongBits (value));
382 public static final double getDouble (ByteBuffer buffer, int index)
384 return Double.longBitsToDouble (getLong (buffer, index));
387 public static final ByteBuffer putDouble (ByteBuffer buffer, int index,
388 double value)
390 return putLong (buffer, index, Double.doubleToLongBits (value));
393 } // ByteBufferHelper