Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / nio / ByteBuffer.java
blob4d3b1dc7d736c2d5c4aa0e0d491fc9dd159768e8
1 /* ByteBuffer.java --
2 Copyright (C) 2002, 2003, 2004 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. */
39 package java.nio;
41 /**
42 * @since 1.4
44 public abstract class ByteBuffer extends Buffer
45 implements Comparable
47 ByteOrder endian = ByteOrder.BIG_ENDIAN;
49 int array_offset;
50 byte[] backing_buffer;
52 ByteBuffer (int capacity, int limit, int position, int mark)
54 super (capacity, limit, position, mark);
57 /**
58 * Allocates a new direct byte buffer.
59 */
60 public static ByteBuffer allocateDirect (int capacity)
62 return DirectByteBufferImpl.allocate (capacity);
65 /**
66 * Allocates a new <code>ByteBuffer</code> object with a given capacity.
68 public static ByteBuffer allocate (int capacity)
70 return wrap(new byte[capacity], 0, capacity);
73 /**
74 * Wraps a <code>byte</code> array into a <code>ByteBuffer</code>
75 * object.
77 * @exception IndexOutOfBoundsException If the preconditions on the offset
78 * and length parameters do not hold
80 public static final ByteBuffer wrap (byte[] array, int offset, int length)
82 // FIXME: In GCJ and other implementations where arrays may not
83 // move we might consider, at least when offset==0:
84 // return new DirectByteBufferImpl(array,
85 // address_of_data(array) + offset,
86 // length, length, 0, false);
87 // This may be more efficient, mainly because we can then use the
88 // same logic for all ByteBuffers.
90 return new ByteBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
93 /**
94 * Wraps a <code>byte</code> array into a <code>ByteBuffer</code>
95 * object.
97 public static final ByteBuffer wrap (byte[] array)
99 return wrap (array, 0, array.length);
103 * This method transfers <code>byte</code>s from this buffer into the given
104 * destination array. Before the transfer, it checks if there are fewer than
105 * length <code>byte</code>s remaining in this buffer.
107 * @param dst The destination array
108 * @param offset The offset within the array of the first <code>byte</code>
109 * to be written; must be non-negative and no larger than dst.length.
110 * @param length The maximum number of bytes to be written to the given array;
111 * must be non-negative and no larger than dst.length - offset.
113 * @exception BufferUnderflowException If there are fewer than length
114 * <code>byte</code>s remaining in this buffer.
115 * @exception IndexOutOfBoundsException If the preconditions on the offset
116 * and length parameters do not hold.
118 public ByteBuffer get (byte[] dst, int offset, int length)
120 checkArraySize(dst.length, offset, length);
121 checkForUnderflow(length);
123 for (int i = offset; i < offset + length; i++)
125 dst [i] = get ();
128 return this;
132 * This method transfers <code>byte</code>s from this buffer into the given
133 * destination array.
135 * @param dst The byte array to write into.
137 * @exception BufferUnderflowException If there are fewer than dst.length
138 * <code>byte</code>s remaining in this buffer.
140 public ByteBuffer get (byte[] dst)
142 return get (dst, 0, dst.length);
146 * Writes the content of the the <code>ByteBUFFER</code> src
147 * into the buffer. Before the transfer, it checks if there is fewer than
148 * <code>src.remaining()</code> space remaining in this buffer.
150 * @param src The source data.
152 * @exception BufferOverflowException If there is insufficient space in this
153 * buffer for the remaining <code>byte</code>s in the source buffer.
154 * @exception IllegalArgumentException If the source buffer is this buffer.
155 * @exception ReadOnlyBufferException If this buffer is read-only.
157 public ByteBuffer put (ByteBuffer src)
159 if (src == this)
160 throw new IllegalArgumentException ();
162 checkForOverflow(src.remaining());
164 if (src.remaining () > 0)
166 byte[] toPut = new byte [src.remaining ()];
167 src.get (toPut);
168 put (toPut);
171 return this;
175 * Writes the content of the the <code>byte array</code> src
176 * into the buffer. Before the transfer, it checks if there is fewer than
177 * length space remaining in this buffer.
179 * @param src The array to copy into the buffer.
180 * @param offset The offset within the array of the first byte to be read;
181 * must be non-negative and no larger than src.length.
182 * @param length The number of bytes to be read from the given array;
183 * must be non-negative and no larger than src.length - offset.
185 * @exception BufferOverflowException If there is insufficient space in this
186 * buffer for the remaining <code>byte</code>s in the source array.
187 * @exception IndexOutOfBoundsException If the preconditions on the offset
188 * and length parameters do not hold
189 * @exception ReadOnlyBufferException If this buffer is read-only.
191 public ByteBuffer put (byte[] src, int offset, int length)
193 checkArraySize(src.length, offset, length);
194 checkForOverflow(length);
196 for (int i = offset; i < offset + length; i++)
197 put (src [i]);
199 return this;
203 * Writes the content of the the <code>byte array</code> src
204 * into the buffer.
206 * @param src The array to copy into the buffer.
208 * @exception BufferOverflowException If there is insufficient space in this
209 * buffer for the remaining <code>byte</code>s in the source array.
210 * @exception ReadOnlyBufferException If this buffer is read-only.
212 public final ByteBuffer put (byte[] src)
214 return put (src, 0, src.length);
218 * Tells whether ot not this buffer is backed by an accessible
219 * <code>byte</code> array.
221 public final boolean hasArray ()
223 return (backing_buffer != null
224 && !isReadOnly ());
228 * Returns the <code>byte</code> array that backs this buffer.
230 * @exception ReadOnlyBufferException If this buffer is read-only.
231 * @exception UnsupportedOperationException If this buffer is not backed
232 * by an accessible array.
234 public final byte[] array ()
236 if (backing_buffer == null)
237 throw new UnsupportedOperationException ();
239 checkIfReadOnly();
241 return backing_buffer;
245 * Returns the offset within this buffer's backing array of the first element.
247 * @exception ReadOnlyBufferException If this buffer is read-only.
248 * @exception UnsupportedOperationException If this buffer is not backed
249 * by an accessible array.
251 public final int arrayOffset ()
253 if (backing_buffer == null)
254 throw new UnsupportedOperationException ();
256 checkIfReadOnly();
258 return array_offset;
262 * Calculates a hash code for this buffer.
264 * This is done with <code>int</code> arithmetic,
265 * where ** represents exponentiation, by this formula:<br>
266 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
267 * (s[limit()-1]+30)*31**(limit()-1)</code>.
268 * Where s is the buffer data. Note that the hashcode is dependent
269 * on buffer content, and therefore is not useful if the buffer
270 * content may change.
272 * @return the hash code
274 public int hashCode ()
276 int hashCode = get(position()) + 31;
277 int multiplier = 1;
278 for (int i = position() + 1; i < limit(); ++i)
280 multiplier *= 31;
281 hashCode += (get(i) + 30)*multiplier;
283 return hashCode;
287 * Checks if this buffer is equal to obj.
289 public boolean equals (Object obj)
291 if (obj instanceof ByteBuffer)
293 return compareTo (obj) == 0;
296 return false;
300 * Compares two <code>ByteBuffer</code> objects.
302 * @exception ClassCastException If obj is not an object derived from
303 * <code>ByteBuffer</code>.
305 public int compareTo (Object obj)
307 ByteBuffer other = (ByteBuffer) obj;
309 int num = Math.min(remaining(), other.remaining());
310 int pos_this = position();
311 int pos_other = other.position();
313 for (int count = 0; count < num; count++)
315 byte a = get(pos_this++);
316 byte b = other.get(pos_other++);
318 if (a == b)
319 continue;
321 if (a < b)
322 return -1;
324 return 1;
327 return remaining() - other.remaining();
331 * Returns the byte order of this buffer.
333 public final ByteOrder order ()
335 return endian;
339 * Modifies this buffer's byte order.
341 public final ByteBuffer order (ByteOrder endian)
343 this.endian = endian;
344 return this;
348 * Reads the <code>byte</code> at this buffer's current position,
349 * and then increments the position.
351 * @exception BufferUnderflowException If there are no remaining
352 * <code>byte</code>s in this buffer.
354 public abstract byte get ();
357 * Writes the <code>byte</code> at this buffer's current position,
358 * and then increments the position.
360 * @exception BufferOverflowException If there no remaining
361 * <code>byte</code>s in this buffer.
362 * @exception ReadOnlyBufferException If this buffer is read-only.
364 public abstract ByteBuffer put (byte b);
367 * Absolute get method.
369 * @exception IndexOutOfBoundsException If index is negative or not smaller
370 * than the buffer's limit.
372 public abstract byte get (int index);
375 * Absolute put method.
377 * @exception IndexOutOfBoundsException If index is negative or not smaller
378 * than the buffer's limit.
379 * @exception ReadOnlyBufferException If this buffer is read-only.
381 public abstract ByteBuffer put (int index, byte b);
384 * Compacts this buffer.
386 * @exception ReadOnlyBufferException If this buffer is read-only.
388 public abstract ByteBuffer compact ();
390 void shiftDown (int dst_offset, int src_offset, int count)
392 for (int i = 0; i < count; i++)
393 put(dst_offset + i, get(src_offset + i));
397 * Tells whether or not this buffer is direct.
399 public abstract boolean isDirect ();
402 * Creates a new <code>ByteBuffer</code> whose content is a shared
403 * subsequence of this buffer's content.
405 public abstract ByteBuffer slice ();
408 * Creates a new <code>ByteBuffer</code> that shares this buffer's
409 * content.
411 public abstract ByteBuffer duplicate ();
414 * Creates a new read-only <code>ByteBuffer</code> that shares this
415 * buffer's content.
417 public abstract ByteBuffer asReadOnlyBuffer ();
420 * Creates a view of this byte buffer as a short buffer.
422 public abstract ShortBuffer asShortBuffer ();
425 * Creates a view of this byte buffer as a char buffer.
427 public abstract CharBuffer asCharBuffer ();
430 * Creates a view of this byte buffer as an integer buffer.
432 public abstract IntBuffer asIntBuffer ();
435 * Creates a view of this byte buffer as a long buffer.
437 public abstract LongBuffer asLongBuffer ();
440 * Creates a view of this byte buffer as a float buffer.
442 public abstract FloatBuffer asFloatBuffer ();
445 * Creates a view of this byte buffer as a double buffer.
447 public abstract DoubleBuffer asDoubleBuffer ();
450 * Relative get method for reading a character value.
452 * @exception BufferUnderflowException If there are fewer than two bytes
453 * remaining in this buffer.
455 public abstract char getChar ();
458 * Relative put method for writing a character value.
460 * @exception BufferOverflowException If this buffer's current position is
461 * not smaller than its limit.
463 public abstract ByteBuffer putChar (char value);
466 * Absolute get method for reading a character value.
468 * @exception IndexOutOfBoundsException If there are fewer than two bytes
469 * remaining in this buffer
471 public abstract char getChar (int index);
474 * Absolute put method for writing a character value.
476 * @exception IndexOutOfBoundsException If index is negative or not smaller
477 * than the buffer's limit, minus one.
479 public abstract ByteBuffer putChar (int index, char value);
482 * Relative get method for reading a short value.
484 * @exception BufferUnderflowException If index is negative or not smaller
485 * than the buffer's limit, minus one.
487 public abstract short getShort ();
490 * Relative put method for writing a short value.
492 * @exception BufferOverflowException If this buffer's current position is
493 * not smaller than its limit.
495 public abstract ByteBuffer putShort (short value);
498 * Absolute get method for reading a short value.
500 * @exception IndexOutOfBoundsException If there are fewer than two bytes
501 * remaining in this buffer
503 public abstract short getShort (int index);
506 * Absolute put method for writing a short value.
508 * @exception IndexOutOfBoundsException If index is negative or not smaller
509 * than the buffer's limit, minus one.
511 public abstract ByteBuffer putShort (int index, short value);
514 * Relative get method for reading an integer value.
516 * @exception BufferUnderflowException If there are fewer than four bytes
517 * remaining in this buffer.
519 public abstract int getInt ();
522 * Relative put method for writing an integer value.
524 * @exception BufferOverflowException If this buffer's current position is
525 * not smaller than its limit.
527 public abstract ByteBuffer putInt (int value);
530 * Absolute get method for reading an integer value.
532 * @exception IndexOutOfBoundsException If index is negative or not smaller
533 * than the buffer's limit, minus three.
535 public abstract int getInt (int index);
538 * Absolute put method for writing an integer value.
540 * @exception IndexOutOfBoundsException If index is negative or not smaller
541 * than the buffer's limit, minus three.
543 public abstract ByteBuffer putInt (int index, int value);
546 * Relative get method for reading a long value.
548 * @exception BufferUnderflowException If there are fewer than eight bytes
549 * remaining in this buffer.
551 public abstract long getLong ();
554 * Relative put method for writing a long value.
556 * @exception BufferOverflowException If this buffer's current position is
557 * not smaller than its limit.
559 public abstract ByteBuffer putLong (long value);
562 * Absolute get method for reading a long value.
564 * @exception IndexOutOfBoundsException If index is negative or not smaller
565 * than the buffer's limit, minus seven.
567 public abstract long getLong (int index);
570 * Absolute put method for writing a float value.
572 * @exception IndexOutOfBoundsException If index is negative or not smaller
573 * than the buffer's limit, minus seven.
575 public abstract ByteBuffer putLong (int index, long value);
578 * Relative get method for reading a float value.
580 * @exception BufferUnderflowException If there are fewer than four bytes
581 * remaining in this buffer.
583 public abstract float getFloat ();
586 * Relative put method for writing a float value.
588 * @exception BufferOverflowException If there are fewer than four bytes
589 * remaining in this buffer.
591 public abstract ByteBuffer putFloat (float value);
594 * Absolute get method for reading a float value.
596 * @exception IndexOutOfBoundsException If index is negative or not smaller
597 * than the buffer's limit, minus three.
599 public abstract float getFloat (int index);
602 * Relative put method for writing a float value.
604 * @exception IndexOutOfBoundsException If index is negative or not smaller
605 * than the buffer's limit, minus three.
607 public abstract ByteBuffer putFloat (int index, float value);
610 * Relative get method for reading a double value.
612 * @exception BufferUnderflowException If there are fewer than eight bytes
613 * remaining in this buffer.
615 public abstract double getDouble ();
618 * Relative put method for writing a double value.
620 * @exception BufferOverflowException If this buffer's current position is
621 * not smaller than its limit.
623 public abstract ByteBuffer putDouble (double value);
626 * Absolute get method for reading a double value.
628 * @exception IndexOutOfBoundsException If index is negative or not smaller
629 * than the buffer's limit, minus seven.
631 public abstract double getDouble (int index);
634 * Absolute put method for writing a double value.
636 * @exception IndexOutOfBoundsException If index is negative or not smaller
637 * than the buffer's limit, minus seven.
639 public abstract ByteBuffer putDouble (int index, double value);
642 * Returns a string summarizing the state of this buffer.
644 public String toString ()
646 return getClass ().getName () +
647 "[pos=" + position () +
648 " lim=" + limit () +
649 " cap=" + capacity () + "]";