libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / nio / ByteBuffer.java
blob94aae4bf02186648b731779351cd456883a3de11
1 /* ByteBuffer.java --
2 Copyright (C) 2002, 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 // GCJ LOCAL: Change gnu.classpath.Pointer to RawData
42 import gnu.gcj.RawData;
43 import gnu.classpath.Pointer;
45 /**
46 * @since 1.4
48 public abstract class ByteBuffer extends Buffer
49 implements Comparable<ByteBuffer>
51 ByteOrder endian = ByteOrder.BIG_ENDIAN;
52 final byte[] backing_buffer;
53 final int array_offset;
55 ByteBuffer (int capacity, int limit, int position, int mark,
56 RawData address, byte[] backing_buffer, int array_offset)
58 super (capacity, limit, position, mark, address);
59 this.backing_buffer = backing_buffer;
60 this.array_offset = array_offset;
63 /**
64 * Allocates a new direct byte buffer.
65 */
66 public static ByteBuffer allocateDirect (int capacity)
68 return DirectByteBufferImpl.allocate (capacity);
71 /**
72 * Allocates a new <code>ByteBuffer</code> object with a given capacity.
74 public static ByteBuffer allocate (int capacity)
76 return wrap(new byte[capacity], 0, capacity);
79 /**
80 * Wraps a <code>byte</code> array into a <code>ByteBuffer</code>
81 * object.
83 * @exception IndexOutOfBoundsException If the preconditions on the offset
84 * and length parameters do not hold
86 public static final ByteBuffer wrap (byte[] array, int offset, int length)
88 // FIXME: In GCJ and other implementations where arrays may not
89 // move we might consider, at least when offset==0:
90 // return new DirectByteBufferImpl(array,
91 // address_of_data(array) + offset,
92 // length, length, 0, false);
93 // This may be more efficient, mainly because we can then use the
94 // same logic for all ByteBuffers.
96 return new ByteBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
99 /**
100 * Wraps a <code>byte</code> array into a <code>ByteBuffer</code>
101 * object.
103 public static final ByteBuffer wrap (byte[] array)
105 return wrap (array, 0, array.length);
109 * This method transfers <code>byte</code>s from this buffer into the given
110 * destination array. Before the transfer, it checks if there are fewer than
111 * length <code>byte</code>s remaining in this buffer.
113 * @param dst The destination array
114 * @param offset The offset within the array of the first <code>byte</code>
115 * to be written; must be non-negative and no larger than dst.length.
116 * @param length The maximum number of bytes to be written to the given array;
117 * must be non-negative and no larger than dst.length - offset.
119 * @exception BufferUnderflowException If there are fewer than length
120 * <code>byte</code>s remaining in this buffer.
121 * @exception IndexOutOfBoundsException If the preconditions on the offset
122 * and length parameters do not hold.
124 public ByteBuffer get (byte[] dst, int offset, int length)
126 checkArraySize(dst.length, offset, length);
127 checkForUnderflow(length);
129 for (int i = offset; i < offset + length; i++)
131 dst [i] = get ();
134 return this;
138 * This method transfers <code>byte</code>s from this buffer into the given
139 * destination array.
141 * @param dst The byte array to write into.
143 * @exception BufferUnderflowException If there are fewer than dst.length
144 * <code>byte</code>s remaining in this buffer.
146 public ByteBuffer get (byte[] dst)
148 return get (dst, 0, dst.length);
152 * Writes the content of the the <code>ByteBUFFER</code> src
153 * into the buffer. Before the transfer, it checks if there is fewer than
154 * <code>src.remaining()</code> space remaining in this buffer.
156 * @param src The source data.
158 * @exception BufferOverflowException If there is insufficient space in this
159 * buffer for the remaining <code>byte</code>s in the source buffer.
160 * @exception IllegalArgumentException If the source buffer is this buffer.
161 * @exception ReadOnlyBufferException If this buffer is read-only.
163 public ByteBuffer put (ByteBuffer src)
165 if (src == this)
166 throw new IllegalArgumentException ();
168 checkForOverflow(src.remaining());
170 if (src.remaining () > 0)
172 byte[] toPut = new byte [src.remaining ()];
173 src.get (toPut);
174 put (toPut);
177 return this;
181 * Writes the content of the the <code>byte array</code> src
182 * into the buffer. Before the transfer, it checks if there is fewer than
183 * length space remaining in this buffer.
185 * @param src The array to copy into the buffer.
186 * @param offset The offset within the array of the first byte to be read;
187 * must be non-negative and no larger than src.length.
188 * @param length The number of bytes to be read from the given array;
189 * must be non-negative and no larger than src.length - offset.
191 * @exception BufferOverflowException If there is insufficient space in this
192 * buffer for the remaining <code>byte</code>s in the source array.
193 * @exception IndexOutOfBoundsException If the preconditions on the offset
194 * and length parameters do not hold
195 * @exception ReadOnlyBufferException If this buffer is read-only.
197 public ByteBuffer put (byte[] src, int offset, int length)
199 checkArraySize(src.length, offset, length);
200 checkForOverflow(length);
202 for (int i = offset; i < offset + length; i++)
203 put (src [i]);
205 return this;
209 * Writes the content of the the <code>byte array</code> src
210 * into the buffer.
212 * @param src The array to copy into the buffer.
214 * @exception BufferOverflowException If there is insufficient space in this
215 * buffer for the remaining <code>byte</code>s in the source array.
216 * @exception ReadOnlyBufferException If this buffer is read-only.
218 public final ByteBuffer put (byte[] src)
220 return put (src, 0, src.length);
224 * Tells whether ot not this buffer is backed by an accessible
225 * <code>byte</code> array.
227 public final boolean hasArray ()
229 return (backing_buffer != null
230 && !isReadOnly ());
234 * Returns the <code>byte</code> array that backs this buffer.
236 * @exception ReadOnlyBufferException If this buffer is read-only.
237 * @exception UnsupportedOperationException If this buffer is not backed
238 * by an accessible array.
240 public final byte[] array ()
242 if (backing_buffer == null)
243 throw new UnsupportedOperationException ();
245 checkIfReadOnly();
247 return backing_buffer;
251 * Returns the offset within this buffer's backing array of the first element.
253 * @exception ReadOnlyBufferException If this buffer is read-only.
254 * @exception UnsupportedOperationException If this buffer is not backed
255 * by an accessible array.
257 public final int arrayOffset ()
259 if (backing_buffer == null)
260 throw new UnsupportedOperationException ();
262 checkIfReadOnly();
264 return array_offset;
268 * Calculates a hash code for this buffer.
270 * This is done with <code>int</code> arithmetic,
271 * where ** represents exponentiation, by this formula:<br>
272 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
273 * (s[limit()-1]+30)*31**(limit()-1)</code>.
274 * Where s is the buffer data. Note that the hashcode is dependent
275 * on buffer content, and therefore is not useful if the buffer
276 * content may change.
278 * @return the hash code
280 public int hashCode ()
282 int hashCode = get(position()) + 31;
283 int multiplier = 1;
284 for (int i = position() + 1; i < limit(); ++i)
286 multiplier *= 31;
287 hashCode += (get(i) + 30)*multiplier;
289 return hashCode;
293 * Checks if this buffer is equal to obj.
295 public boolean equals (Object obj)
297 if (obj instanceof ByteBuffer)
299 return compareTo ((ByteBuffer) obj) == 0;
302 return false;
306 * Compares two <code>ByteBuffer</code> objects.
308 * @exception ClassCastException If obj is not an object derived from
309 * <code>ByteBuffer</code>.
311 public int compareTo (ByteBuffer other)
313 int num = Math.min(remaining(), other.remaining());
314 int pos_this = position();
315 int pos_other = other.position();
317 for (int count = 0; count < num; count++)
319 byte a = get(pos_this++);
320 byte b = other.get(pos_other++);
322 if (a == b)
323 continue;
325 if (a < b)
326 return -1;
328 return 1;
331 return remaining() - other.remaining();
335 * Returns the byte order of this buffer.
337 public final ByteOrder order ()
339 return endian;
343 * Modifies this buffer's byte order.
345 public final ByteBuffer order (ByteOrder endian)
347 this.endian = endian;
348 return this;
352 * Reads the <code>byte</code> at this buffer's current position,
353 * and then increments the position.
355 * @exception BufferUnderflowException If there are no remaining
356 * <code>byte</code>s in this buffer.
358 public abstract byte get ();
361 * Writes the <code>byte</code> at this buffer's current position,
362 * and then increments the position.
364 * @exception BufferOverflowException If there no remaining
365 * <code>byte</code>s in this buffer.
366 * @exception ReadOnlyBufferException If this buffer is read-only.
368 public abstract ByteBuffer put (byte b);
371 * Absolute get method.
373 * @exception IndexOutOfBoundsException If index is negative or not smaller
374 * than the buffer's limit.
376 public abstract byte get (int index);
379 * Absolute put method.
381 * @exception IndexOutOfBoundsException If index is negative or not smaller
382 * than the buffer's limit.
383 * @exception ReadOnlyBufferException If this buffer is read-only.
385 public abstract ByteBuffer put (int index, byte b);
388 * Compacts this buffer.
390 * @exception ReadOnlyBufferException If this buffer is read-only.
392 public abstract ByteBuffer compact ();
394 void shiftDown (int dst_offset, int src_offset, int count)
396 for (int i = 0; i < count; i++)
397 put(dst_offset + i, get(src_offset + i));
401 * Tells whether or not this buffer is direct.
403 public abstract boolean isDirect ();
406 * Creates a new <code>ByteBuffer</code> whose content is a shared
407 * subsequence of this buffer's content.
409 public abstract ByteBuffer slice ();
412 * Creates a new <code>ByteBuffer</code> that shares this buffer's
413 * content.
415 public abstract ByteBuffer duplicate ();
418 * Creates a new read-only <code>ByteBuffer</code> that shares this
419 * buffer's content.
421 public abstract ByteBuffer asReadOnlyBuffer ();
424 * Creates a view of this byte buffer as a short buffer.
426 public abstract ShortBuffer asShortBuffer ();
429 * Creates a view of this byte buffer as a char buffer.
431 public abstract CharBuffer asCharBuffer ();
434 * Creates a view of this byte buffer as an integer buffer.
436 public abstract IntBuffer asIntBuffer ();
439 * Creates a view of this byte buffer as a long buffer.
441 public abstract LongBuffer asLongBuffer ();
444 * Creates a view of this byte buffer as a float buffer.
446 public abstract FloatBuffer asFloatBuffer ();
449 * Creates a view of this byte buffer as a double buffer.
451 public abstract DoubleBuffer asDoubleBuffer ();
454 * Relative get method for reading a character value.
456 * @exception BufferUnderflowException If there are fewer than two bytes
457 * remaining in this buffer.
459 public abstract char getChar ();
462 * Relative put method for writing a character value.
464 * @exception BufferOverflowException If this buffer's current position is
465 * not smaller than its limit.
467 public abstract ByteBuffer putChar (char value);
470 * Absolute get method for reading a character value.
472 * @exception IndexOutOfBoundsException If there are fewer than two bytes
473 * remaining in this buffer
475 public abstract char getChar (int index);
478 * Absolute put method for writing a character value.
480 * @exception IndexOutOfBoundsException If index is negative or not smaller
481 * than the buffer's limit, minus one.
483 public abstract ByteBuffer putChar (int index, char value);
486 * Relative get method for reading a short value.
488 * @exception BufferUnderflowException If index is negative or not smaller
489 * than the buffer's limit, minus one.
491 public abstract short getShort ();
494 * Relative put method for writing a short value.
496 * @exception BufferOverflowException If this buffer's current position is
497 * not smaller than its limit.
499 public abstract ByteBuffer putShort (short value);
502 * Absolute get method for reading a short value.
504 * @exception IndexOutOfBoundsException If there are fewer than two bytes
505 * remaining in this buffer
507 public abstract short getShort (int index);
510 * Absolute put method for writing a short value.
512 * @exception IndexOutOfBoundsException If index is negative or not smaller
513 * than the buffer's limit, minus one.
515 public abstract ByteBuffer putShort (int index, short value);
518 * Relative get method for reading an integer value.
520 * @exception BufferUnderflowException If there are fewer than four bytes
521 * remaining in this buffer.
523 public abstract int getInt ();
526 * Relative put method for writing an integer value.
528 * @exception BufferOverflowException If this buffer's current position is
529 * not smaller than its limit.
531 public abstract ByteBuffer putInt (int value);
534 * Absolute get method for reading an integer value.
536 * @exception IndexOutOfBoundsException If index is negative or not smaller
537 * than the buffer's limit, minus three.
539 public abstract int getInt (int index);
542 * Absolute put method for writing an integer value.
544 * @exception IndexOutOfBoundsException If index is negative or not smaller
545 * than the buffer's limit, minus three.
547 public abstract ByteBuffer putInt (int index, int value);
550 * Relative get method for reading a long value.
552 * @exception BufferUnderflowException If there are fewer than eight bytes
553 * remaining in this buffer.
555 public abstract long getLong ();
558 * Relative put method for writing a long value.
560 * @exception BufferOverflowException If this buffer's current position is
561 * not smaller than its limit.
563 public abstract ByteBuffer putLong (long value);
566 * Absolute get method for reading a long value.
568 * @exception IndexOutOfBoundsException If index is negative or not smaller
569 * than the buffer's limit, minus seven.
571 public abstract long getLong (int index);
574 * Absolute put method for writing a float value.
576 * @exception IndexOutOfBoundsException If index is negative or not smaller
577 * than the buffer's limit, minus seven.
579 public abstract ByteBuffer putLong (int index, long value);
582 * Relative get method for reading a float value.
584 * @exception BufferUnderflowException If there are fewer than four bytes
585 * remaining in this buffer.
587 public abstract float getFloat ();
590 * Relative put method for writing a float value.
592 * @exception BufferOverflowException If there are fewer than four bytes
593 * remaining in this buffer.
595 public abstract ByteBuffer putFloat (float value);
598 * Absolute get method for reading a float value.
600 * @exception IndexOutOfBoundsException If index is negative or not smaller
601 * than the buffer's limit, minus three.
603 public abstract float getFloat (int index);
606 * Relative put method for writing a float value.
608 * @exception IndexOutOfBoundsException If index is negative or not smaller
609 * than the buffer's limit, minus three.
611 public abstract ByteBuffer putFloat (int index, float value);
614 * Relative get method for reading a double value.
616 * @exception BufferUnderflowException If there are fewer than eight bytes
617 * remaining in this buffer.
619 public abstract double getDouble ();
622 * Relative put method for writing a double value.
624 * @exception BufferOverflowException If this buffer's current position is
625 * not smaller than its limit.
627 public abstract ByteBuffer putDouble (double value);
630 * Absolute get method for reading a double value.
632 * @exception IndexOutOfBoundsException If index is negative or not smaller
633 * than the buffer's limit, minus seven.
635 public abstract double getDouble (int index);
638 * Absolute put method for writing a double value.
640 * @exception IndexOutOfBoundsException If index is negative or not smaller
641 * than the buffer's limit, minus seven.
643 public abstract ByteBuffer putDouble (int index, double value);
646 * Returns a string summarizing the state of this buffer.
648 public String toString ()
650 return getClass ().getName () +
651 "[pos=" + position () +
652 " lim=" + limit () +
653 " cap=" + capacity () + "]";