FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / nio / ByteBuffer.java
blob8b7c60130192afb691a7e37afa5fb6977bae84b3
1 /* ByteBuffer.java --
2 Copyright (C) 2002 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 import gnu.java.nio.ByteBufferImpl;
42 /**
43 * @since 1.4
45 public abstract class ByteBuffer extends Buffer implements Comparable
47 private ByteOrder endian = ByteOrder.BIG_ENDIAN;
49 int offset;
50 byte[] backing_buffer;
52 /**
53 * Allocates a new direct byte buffer.
54 */
55 public static ByteBuffer allocateDirect (int capacity)
57 throw new Error ("direct buffers are not implemented");
60 /**
61 * Allocates a new byte buffer.
63 public static ByteBuffer allocate (int capacity)
65 return new ByteBufferImpl (capacity, 0, capacity);
68 /**
69 * Wraps a byte array into a buffer.
71 * @exception IndexOutOfBoundsException If the preconditions on the offset
72 * and length parameters do not hold
74 final public static ByteBuffer wrap (byte[] array, int offset, int length)
76 return new ByteBufferImpl (array, offset, length);
79 /**
80 * Wraps a byte array into a buffer.
82 final public static ByteBuffer wrap (byte[] array)
84 return wrap (array, 0, array.length);
87 ByteBuffer (int capacity, int limit, int position, int mark)
89 super (capacity, limit, position, mark);
92 /**
93 * This method transfers bytes from this buffer into
94 * the given destination array.
96 * @param dst The destination array
97 * @param offset The offset within the array of the first byte to be written;
98 * must be non-negative and no larger than dst.length.
99 * @param length The maximum number of bytes to be written to the given array;
100 * must be non-negative and no larger than dst.length - offset.
102 * @exception BufferUnderflowException If there are fewer than length bytes
103 * remaining in this buffer.
104 * @exception IndexOutOfBoundsException - If the preconditions on the offset
105 * and length parameters do not hold.
107 public ByteBuffer get (byte[] dst, int offset, int length)
109 if ((offset < 0)
110 || (offset > dst.length)
111 || (length < 0)
112 || (length > (dst.length - offset)))
113 throw new IndexOutOfBoundsException ();
115 for (int i = offset; i < offset + length; i++)
117 dst [i] = get();
120 return this;
124 * This method transfers bytes from this buffer into the given
125 * destination array.
127 * @param dst The byte array to write into.
129 * @exception BufferUnderflowException If there are fewer than dst.length
130 * bytes remaining in this buffer.
132 public ByteBuffer get (byte[] dst)
134 return get (dst, 0, dst.length);
138 * Writes the content of src into the buffer.
140 * @param src The source data.
142 * @exception BufferOverflowException If there is insufficient space in this
143 * buffer for the remaining bytes in the source buffer.
144 * @exception IllegalArgumentException If the source buffer is this buffer.
145 * @exception ReadOnlyBufferException If this buffer is read only.
147 public ByteBuffer put (ByteBuffer src)
149 if (src == this)
150 throw new IllegalArgumentException ();
152 while (src.hasRemaining ())
153 put (src.get ());
155 return this;
159 * Writes the content of the the array src into the buffer.
161 * @param src The array to copy into the buffer.
162 * @param offset The offset within the array of the first byte to be read;
163 * must be non-negative and no larger than src.length.
164 * @param length The number of bytes to be read from the given array;
165 * must be non-negative and no larger than src.length - offset.
167 * @exception BufferOverflowException If there is insufficient space in this
168 * buffer for the remaining bytes in the source buffer.
169 * @exception IndexOutOfBoundsException If the preconditions on the offset
170 * and length parameters do not hold.
171 * @exception ReadOnlyBufferException If this buffer is read only.
173 public ByteBuffer put (byte[] src, int offset, int length)
175 if ((offset < 0) ||
176 (offset > src.length) ||
177 (length < 0) ||
178 (length > src.length - offset))
179 throw new IndexOutOfBoundsException ();
181 for (int i = offset; i < offset + length; i++)
182 put (src [i]);
184 return this;
188 * Writes the content of the the array src into the buffer.
190 * @param src The array to copy into the buffer.
192 * @exception BufferOverflowException If there is insufficient space in this
193 * buffer for the remaining bytes in the source buffer.
194 * @exception ReadOnlyBufferException If this buffer is read only.
196 public final ByteBuffer put (byte[] src)
198 return put (src, 0, src.length);
202 * Tells whether or not this buffer is backed by an accessible byte array.
204 public final boolean hasArray ()
206 return (backing_buffer != null
207 && !isReadOnly ());
211 * Returns the byte array that backs this buffer.
213 * @exception ReadOnlyBufferException If this buffer is backed by an array
214 * but is read-only.
215 * @exception UnsupportedOperationException If this buffer is not backed
216 * by an accessible array.
218 public final byte[] array ()
220 if (backing_buffer == null)
221 throw new UnsupportedOperationException ();
223 if (isReadOnly ())
224 throw new ReadOnlyBufferException ();
226 return backing_buffer;
230 * Returns the offset within this buffer's backing array of the first element
231 * of the buffer
233 * @exception ReadOnlyBufferException If this buffer is backed by an array
234 * but is read-only.
235 * @exception UnsupportedOperationException If this buffer is not backed
236 * by an accessible array.
238 public final int arrayOffset ()
240 if (backing_buffer == null)
241 throw new UnsupportedOperationException ();
243 if (isReadOnly ())
244 throw new ReadOnlyBufferException ();
246 return offset;
250 * Tells whether or not this buffer is equal to another object.
252 public boolean equals (Object obj)
254 if (obj != null &&
255 obj instanceof ByteBuffer)
257 return compareTo (obj) == 0;
260 return false;
264 * Compares this buffer to another object.
266 * @exception ClassCastException If the argument is not a byte buffer
268 public int compareTo (Object obj)
270 ByteBuffer a = (ByteBuffer) obj;
272 if (a.remaining() != remaining())
274 return 1;
277 if (! hasArray() ||
278 ! a.hasArray())
280 return 1;
283 int r = remaining();
284 int i1 = position ();
285 int i2 = a.position ();
287 for (int i = 0; i < r; i++)
289 int t = (int) (get (i1) - a.get (i2));
291 if (t != 0)
293 return (int) t;
297 return 0;
301 * Retrieves this buffer's byte order.
303 public final ByteOrder order()
305 return endian;
309 * Modifies this buffer's byte order.
311 public final ByteBuffer order (ByteOrder endian)
313 this.endian = endian;
314 return this;
318 * Reads the byte at this buffer's current position,
319 * and then increments the position.
321 * @exception BufferUnderflowException If the buffer's current position
322 * is not smaller than its limit.
324 public abstract byte get ();
327 * Relative put method.
329 * @exception BufferOverflowException If this buffer's current position is
330 * not smaller than its limit.
331 * @exception ReadOnlyBufferException If this buffer is read-only.
333 public abstract ByteBuffer put (byte b);
336 * Absolute get method.
338 * @exception IndexOutOfBoundsException FIXME
340 public abstract byte get (int index);
343 * Absolute put method.
345 * @exception ReadOnlyBufferException If this buffer is read-only
346 * @exception IndexOutOfBoundsException FIXME
348 public abstract ByteBuffer put (int index, byte b);
351 * Compacts this buffer.
353 * @exception ReadOnlyBufferException If this buffer is read-only
355 public abstract ByteBuffer compact();
358 * Tells whether or not this buffer is direct.
360 public abstract boolean isDirect();
363 * Creates a new byte buffer whose content is a shared subsequence of this
364 * buffer's content.
366 public abstract ByteBuffer slice();
369 * Creates a new byte buffer that shares this buffer's content.
371 public abstract ByteBuffer duplicate();
374 * Creates a new, read-only byte buffer that shares this buffer's content.
376 public abstract ByteBuffer asReadOnlyBuffer();
379 * Creates a view of this byte buffer as a short buffer.
381 public abstract ShortBuffer asShortBuffer();
384 * Creates a view of this byte buffer as a char buffer.
386 public abstract CharBuffer asCharBuffer();
389 * Creates a view of this byte buffer as an integer buffer.
391 public abstract IntBuffer asIntBuffer();
394 * Creates a view of this byte buffer as a long buffer.
396 public abstract LongBuffer asLongBuffer();
399 * Creates a view of this byte buffer as a float buffer.
401 public abstract FloatBuffer asFloatBuffer();
404 * Creates a view of this byte buffer as a double buffer.
406 public abstract DoubleBuffer asDoubleBuffer();
409 * Relative get method for reading a character value.
411 * @exception BufferUnderflowException If there are fewer than two bytes
412 * remaining in this buffer.
414 public abstract char getChar();
417 * Relative put method for writing a character value.
419 * @exception BufferOverflowException If this buffer's current position is
420 * not smaller than its limit.
422 public abstract ByteBuffer putChar(char value);
425 * Absolute get method for reading a character value.
427 * @exception IndexOutOfBoundsException If there are fewer than two bytes
428 * remaining in this buffer
430 public abstract char getChar(int index);
433 * Absolute put method for writing a character value.
435 * @exception IndexOutOfBoundsException If index is negative or not smaller
436 * than the buffer's limit, minus one.
438 public abstract ByteBuffer putChar(int index, char value);
441 * Relative get method for reading a short value.
443 * @exception BufferUnderflowException If index is negative or not smaller
444 * than the buffer's limit, minus one.
446 public abstract short getShort();
449 * Relative put method for writing a short value.
451 * @exception BufferOverflowException If this buffer's current position is
452 * not smaller than its limit.
454 public abstract ByteBuffer putShort(short value);
457 * Absolute get method for reading a short value.
459 * @exception IndexOutOfBoundsException If there are fewer than two bytes
460 * remaining in this buffer
462 public abstract short getShort(int index);
465 * Absolute put method for writing a short value.
467 * @exception IndexOutOfBoundsException If index is negative or not smaller
468 * than the buffer's limit, minus one.
470 public abstract ByteBuffer putShort(int index, short value);
473 * Relative get method for reading an integer value.
475 * @exception BufferUnderflowException If there are fewer than four bytes
476 * remaining in this buffer.
478 public abstract int getInt();
481 * Relative put method for writing an integer value.
483 * @exception BufferOverflowException If this buffer's current position is
484 * not smaller than its limit.
486 public abstract ByteBuffer putInt(int value);
489 * Absolute get method for reading an integer value.
491 * @exception IndexOutOfBoundsException If index is negative or not smaller
492 * than the buffer's limit, minus three.
494 public abstract int getInt(int index);
497 * Absolute put method for writing an integer value.
499 * @exception IndexOutOfBoundsException If index is negative or not smaller
500 * than the buffer's limit, minus three.
502 public abstract ByteBuffer putInt(int index, int value);
505 * Relative get method for reading a long value.
507 * @exception BufferUnderflowException If there are fewer than eight bytes
508 * remaining in this buffer.
510 public abstract long getLong();
513 * Relative put method for writing a long value.
515 * @exception BufferOverflowException If this buffer's current position is
516 * not smaller than its limit.
518 public abstract ByteBuffer putLong(long value);
521 * Absolute get method for reading a long value.
523 * @exception IndexOutOfBoundsException If index is negative or not smaller
524 * than the buffer's limit, minus seven.
526 public abstract long getLong(int index);
529 * Absolute put method for writing a float value.
531 * @exception IndexOutOfBoundsException If index is negative or not smaller
532 * than the buffer's limit, minus seven.
534 public abstract ByteBuffer putLong(int index, long value);
537 * Relative get method for reading a float value.
539 * @exception BufferUnderflowException If there are fewer than four bytes
540 * remaining in this buffer.
542 public abstract float getFloat();
545 * Relative put method for writing a float value.
547 * @exception BufferOverflowException If there are fewer than four bytes
548 * remaining in this buffer.
550 public abstract ByteBuffer putFloat(float value);
553 * Absolute get method for reading a float value.
555 * @exception IndexOutOfBoundsException If index is negative or not smaller
556 * than the buffer's limit, minus three.
558 public abstract float getFloat(int index);
561 * Relative put method for writing a float value.
563 * @exception IndexOutOfBoundsException If index is negative or not smaller
564 * than the buffer's limit, minus three.
566 public abstract ByteBuffer putFloat(int index, float value);
569 * Relative get method for reading a double value.
571 * @exception BufferUnderflowException If there are fewer than eight bytes
572 * remaining in this buffer.
574 public abstract double getDouble();
577 * Relative put method for writing a double value.
579 * @exception BufferOverflowException If this buffer's current position is
580 * not smaller than its limit.
582 public abstract ByteBuffer putDouble(double value);
585 * Absolute get method for reading a double value.
587 * @exception IndexOutOfBoundsException If index is negative or not smaller
588 * than the buffer's limit, minus seven.
590 public abstract double getDouble(int index);
593 * Absolute put method for writing a double value.
595 * @exception IndexOutOfBoundsException If index is negative or not smaller
596 * than the buffer's limit, minus seven.
598 public abstract ByteBuffer putDouble(int index, double value);
601 * Returns a string summarizing the state of this buffer.
603 public String toString ()
605 return getClass ().getName () +
606 "[pos=" + position () +
607 " lim=" + limit () +
608 " cap=" + capacity () + "]";