libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / nio / FloatBuffer.java
blobce40a09e37bf6c48943731be706cf7647abc40e2
1 /* FloatBuffer.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;
44 /**
45 * @since 1.4
47 public abstract class FloatBuffer extends Buffer
48 implements Comparable<FloatBuffer>
50 final int array_offset;
51 final float[] backing_buffer;
53 FloatBuffer (int capacity, int limit, int position, int mark,
54 RawData address, float[] backing_buffer, int array_offset)
56 super (capacity, limit, position, mark, address);
57 this.backing_buffer = backing_buffer;
58 this.array_offset = array_offset;
61 /**
62 * Allocates a new <code>FloatBuffer</code> object with a given capacity.
64 public static FloatBuffer allocate (int capacity)
66 return new FloatBufferImpl (capacity);
69 /**
70 * Wraps a <code>float</code> array into a <code>FloatBuffer</code>
71 * object.
73 * @exception IndexOutOfBoundsException If the preconditions on the offset
74 * and length parameters do not hold
76 public static final FloatBuffer wrap (float[] array, int offset, int length)
78 return new FloatBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
81 /**
82 * Wraps a <code>float</code> array into a <code>FloatBuffer</code>
83 * object.
85 public static final FloatBuffer wrap (float[] array)
87 return wrap (array, 0, array.length);
90 /**
91 * This method transfers <code>float</code>s from this buffer into the given
92 * destination array. Before the transfer, it checks if there are fewer than
93 * length <code>float</code>s remaining in this buffer.
95 * @param dst The destination array
96 * @param offset The offset within the array of the first <code>float</code>
97 * to be written; must be non-negative and no larger than dst.length.
98 * @param length The maximum number of bytes to be written to the given array;
99 * must be non-negative and no larger than dst.length - offset.
101 * @exception BufferUnderflowException If there are fewer than length
102 * <code>float</code>s remaining in this buffer.
103 * @exception IndexOutOfBoundsException If the preconditions on the offset
104 * and length parameters do not hold.
106 public FloatBuffer get (float[] dst, int offset, int length)
108 checkArraySize(dst.length, offset, length);
109 checkForUnderflow(length);
111 for (int i = offset; i < offset + length; i++)
113 dst [i] = get ();
116 return this;
120 * This method transfers <code>float</code>s from this buffer into the given
121 * destination array.
123 * @param dst The byte array to write into.
125 * @exception BufferUnderflowException If there are fewer than dst.length
126 * <code>float</code>s remaining in this buffer.
128 public FloatBuffer get (float[] dst)
130 return get (dst, 0, dst.length);
134 * Writes the content of the the <code>FloatBUFFER</code> src
135 * into the buffer. Before the transfer, it checks if there is fewer than
136 * <code>src.remaining()</code> space remaining in this buffer.
138 * @param src The source data.
140 * @exception BufferOverflowException If there is insufficient space in this
141 * buffer for the remaining <code>float</code>s in the source buffer.
142 * @exception IllegalArgumentException If the source buffer is this buffer.
143 * @exception ReadOnlyBufferException If this buffer is read-only.
145 public FloatBuffer put (FloatBuffer src)
147 if (src == this)
148 throw new IllegalArgumentException ();
150 checkForOverflow(src.remaining());
152 if (src.remaining () > 0)
154 float[] toPut = new float [src.remaining ()];
155 src.get (toPut);
156 put (toPut);
159 return this;
163 * Writes the content of the the <code>float array</code> src
164 * into the buffer. Before the transfer, it checks if there is fewer than
165 * length space remaining in this buffer.
167 * @param src The array to copy into the buffer.
168 * @param offset The offset within the array of the first byte to be read;
169 * must be non-negative and no larger than src.length.
170 * @param length The number of bytes to be read from the given array;
171 * must be non-negative and no larger than src.length - offset.
173 * @exception BufferOverflowException If there is insufficient space in this
174 * buffer for the remaining <code>float</code>s in the source array.
175 * @exception IndexOutOfBoundsException If the preconditions on the offset
176 * and length parameters do not hold
177 * @exception ReadOnlyBufferException If this buffer is read-only.
179 public FloatBuffer put (float[] src, int offset, int length)
181 checkArraySize(src.length, offset, length);
182 checkForOverflow(length);
184 for (int i = offset; i < offset + length; i++)
185 put (src [i]);
187 return this;
191 * Writes the content of the the <code>float array</code> src
192 * into the buffer.
194 * @param src The array to copy into the buffer.
196 * @exception BufferOverflowException If there is insufficient space in this
197 * buffer for the remaining <code>float</code>s in the source array.
198 * @exception ReadOnlyBufferException If this buffer is read-only.
200 public final FloatBuffer put (float[] src)
202 return put (src, 0, src.length);
206 * Tells whether ot not this buffer is backed by an accessible
207 * <code>float</code> array.
209 public final boolean hasArray ()
211 return (backing_buffer != null
212 && !isReadOnly ());
216 * Returns the <code>float</code> array that backs this buffer.
218 * @exception ReadOnlyBufferException If this buffer is read-only.
219 * @exception UnsupportedOperationException If this buffer is not backed
220 * by an accessible array.
222 public final float[] array ()
224 if (backing_buffer == null)
225 throw new UnsupportedOperationException ();
227 checkIfReadOnly();
229 return backing_buffer;
233 * Returns the offset within this buffer's backing array of the first element.
235 * @exception ReadOnlyBufferException If this buffer is read-only.
236 * @exception UnsupportedOperationException If this buffer is not backed
237 * by an accessible array.
239 public final int arrayOffset ()
241 if (backing_buffer == null)
242 throw new UnsupportedOperationException ();
244 checkIfReadOnly();
246 return array_offset;
250 * Calculates a hash code for this buffer.
252 * This is done with <code>int</code> arithmetic,
253 * where ** represents exponentiation, by this formula:<br>
254 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
255 * (s[limit()-1]+30)*31**(limit()-1)</code>.
256 * Where s is the buffer data, in Float.floatToIntBits() form
257 * Note that the hashcode is dependent on buffer content,
258 * and therefore is not useful if the buffer content may change.
260 * @return the hash code
262 public int hashCode ()
264 int hashCode = Float.floatToIntBits(get(position())) + 31;
265 int multiplier = 1;
266 for (int i = position() + 1; i < limit(); ++i)
268 multiplier *= 31;
269 hashCode += (Float.floatToIntBits(get(i)) + 30)*multiplier;
271 return hashCode;
275 * Checks if this buffer is equal to obj.
277 public boolean equals (Object obj)
279 if (obj instanceof FloatBuffer)
281 return compareTo ((FloatBuffer) obj) == 0;
284 return false;
288 * Compares two <code>FloatBuffer</code> objects.
290 * @exception ClassCastException If obj is not an object derived from
291 * <code>FloatBuffer</code>.
293 public int compareTo (FloatBuffer other)
295 int num = Math.min(remaining(), other.remaining());
296 int pos_this = position();
297 int pos_other = other.position();
299 for (int count = 0; count < num; count++)
301 float a = get(pos_this++);
302 float b = other.get(pos_other++);
304 if (a == b)
305 continue;
307 if (a < b)
308 return -1;
310 return 1;
313 return remaining() - other.remaining();
317 * Returns the byte order of this buffer.
319 public abstract ByteOrder order ();
322 * Reads the <code>float</code> at this buffer's current position,
323 * and then increments the position.
325 * @exception BufferUnderflowException If there are no remaining
326 * <code>float</code>s in this buffer.
328 public abstract float get ();
331 * Writes the <code>float</code> at this buffer's current position,
332 * and then increments the position.
334 * @exception BufferOverflowException If there no remaining
335 * <code>float</code>s in this buffer.
336 * @exception ReadOnlyBufferException If this buffer is read-only.
338 public abstract FloatBuffer put (float b);
341 * Absolute get method.
343 * @exception IndexOutOfBoundsException If index is negative or not smaller
344 * than the buffer's limit.
346 public abstract float get (int index);
349 * Absolute put method.
351 * @exception IndexOutOfBoundsException If index is negative or not smaller
352 * than the buffer's limit.
353 * @exception ReadOnlyBufferException If this buffer is read-only.
355 public abstract FloatBuffer put (int index, float b);
358 * Compacts this buffer.
360 * @exception ReadOnlyBufferException If this buffer is read-only.
362 public abstract FloatBuffer compact ();
365 * Tells wether or not this buffer is direct.
367 public abstract boolean isDirect ();
370 * Creates a new <code>FloatBuffer</code> whose content is a shared
371 * subsequence of this buffer's content.
373 public abstract FloatBuffer slice ();
376 * Creates a new <code>FloatBuffer</code> that shares this buffer's
377 * content.
379 public abstract FloatBuffer duplicate ();
382 * Creates a new read-only <code>FloatBuffer</code> that shares this
383 * buffer's content.
385 public abstract FloatBuffer asReadOnlyBuffer ();