1 /* FloatBuffer.java -- 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 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. 10 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. 15 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. 20 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. 25 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. */ 37 38 39 package java.nio; 40 41 // GCJ LOCAL: Change gnu.classpath.Pointer to RawData 42 import gnu.gcj.RawData; 43 44 /** 45 * @since 1.4 46 */ 47 public abstract class FloatBuffer extends Buffer 48 implements Comparable<FloatBuffer> 49 { 50 final int array_offset; 51 final float[] backing_buffer; 52 FloatBuffer(int capacity, int limit, int position, int mark, RawData address, float[] backing_buffer, int array_offset)53 FloatBuffer (int capacity, int limit, int position, int mark, 54 RawData address, float[] backing_buffer, int array_offset) 55 { 56 super (capacity, limit, position, mark, address); 57 this.backing_buffer = backing_buffer; 58 this.array_offset = array_offset; 59 } 60 61 /** 62 * Allocates a new <code>FloatBuffer</code> object with a given capacity. 63 */ allocate(int capacity)64 public static FloatBuffer allocate (int capacity) 65 { 66 return new FloatBufferImpl (capacity); 67 } 68 69 /** 70 * Wraps a <code>float</code> array into a <code>FloatBuffer</code> 71 * object. 72 * 73 * @exception IndexOutOfBoundsException If the preconditions on the offset 74 * and length parameters do not hold 75 */ wrap(float[] array, int offset, int length)76 public static final FloatBuffer wrap (float[] array, int offset, int length) 77 { 78 return new FloatBufferImpl (array, 0, array.length, offset + length, offset, -1, false); 79 } 80 81 /** 82 * Wraps a <code>float</code> array into a <code>FloatBuffer</code> 83 * object. 84 */ wrap(float[] array)85 public static final FloatBuffer wrap (float[] array) 86 { 87 return wrap (array, 0, array.length); 88 } 89 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. 94 * 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. 100 * 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. 105 */ get(float[] dst, int offset, int length)106 public FloatBuffer get (float[] dst, int offset, int length) 107 { 108 checkArraySize(dst.length, offset, length); 109 checkForUnderflow(length); 110 111 for (int i = offset; i < offset + length; i++) 112 { 113 dst [i] = get (); 114 } 115 116 return this; 117 } 118 119 /** 120 * This method transfers <code>float</code>s from this buffer into the given 121 * destination array. 122 * 123 * @param dst The byte array to write into. 124 * 125 * @exception BufferUnderflowException If there are fewer than dst.length 126 * <code>float</code>s remaining in this buffer. 127 */ get(float[] dst)128 public FloatBuffer get (float[] dst) 129 { 130 return get (dst, 0, dst.length); 131 } 132 133 /** 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. 137 * 138 * @param src The source data. 139 * 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. 144 */ put(FloatBuffer src)145 public FloatBuffer put (FloatBuffer src) 146 { 147 if (src == this) 148 throw new IllegalArgumentException (); 149 150 checkForOverflow(src.remaining()); 151 152 if (src.remaining () > 0) 153 { 154 float[] toPut = new float [src.remaining ()]; 155 src.get (toPut); 156 put (toPut); 157 } 158 159 return this; 160 } 161 162 /** 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. 166 * 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. 172 * 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. 178 */ put(float[] src, int offset, int length)179 public FloatBuffer put (float[] src, int offset, int length) 180 { 181 checkArraySize(src.length, offset, length); 182 checkForOverflow(length); 183 184 for (int i = offset; i < offset + length; i++) 185 put (src [i]); 186 187 return this; 188 } 189 190 /** 191 * Writes the content of the the <code>float array</code> src 192 * into the buffer. 193 * 194 * @param src The array to copy into the buffer. 195 * 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. 199 */ put(float[] src)200 public final FloatBuffer put (float[] src) 201 { 202 return put (src, 0, src.length); 203 } 204 205 /** 206 * Tells whether ot not this buffer is backed by an accessible 207 * <code>float</code> array. 208 */ hasArray()209 public final boolean hasArray () 210 { 211 return (backing_buffer != null 212 && !isReadOnly ()); 213 } 214 215 /** 216 * Returns the <code>float</code> array that backs this buffer. 217 * 218 * @exception ReadOnlyBufferException If this buffer is read-only. 219 * @exception UnsupportedOperationException If this buffer is not backed 220 * by an accessible array. 221 */ array()222 public final float[] array () 223 { 224 if (backing_buffer == null) 225 throw new UnsupportedOperationException (); 226 227 checkIfReadOnly(); 228 229 return backing_buffer; 230 } 231 232 /** 233 * Returns the offset within this buffer's backing array of the first element. 234 * 235 * @exception ReadOnlyBufferException If this buffer is read-only. 236 * @exception UnsupportedOperationException If this buffer is not backed 237 * by an accessible array. 238 */ arrayOffset()239 public final int arrayOffset () 240 { 241 if (backing_buffer == null) 242 throw new UnsupportedOperationException (); 243 244 checkIfReadOnly(); 245 246 return array_offset; 247 } 248 249 /** 250 * Calculates a hash code for this buffer. 251 * 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. 259 * 260 * @return the hash code 261 */ hashCode()262 public int hashCode () 263 { 264 int hashCode = Float.floatToIntBits(get(position())) + 31; 265 int multiplier = 1; 266 for (int i = position() + 1; i < limit(); ++i) 267 { 268 multiplier *= 31; 269 hashCode += (Float.floatToIntBits(get(i)) + 30)*multiplier; 270 } 271 return hashCode; 272 } 273 274 /** 275 * Checks if this buffer is equal to obj. 276 */ equals(Object obj)277 public boolean equals (Object obj) 278 { 279 if (obj instanceof FloatBuffer) 280 { 281 return compareTo ((FloatBuffer) obj) == 0; 282 } 283 284 return false; 285 } 286 287 /** 288 * Compares two <code>FloatBuffer</code> objects. 289 * 290 * @exception ClassCastException If obj is not an object derived from 291 * <code>FloatBuffer</code>. 292 */ compareTo(FloatBuffer other)293 public int compareTo (FloatBuffer other) 294 { 295 int num = Math.min(remaining(), other.remaining()); 296 int pos_this = position(); 297 int pos_other = other.position(); 298 299 for (int count = 0; count < num; count++) 300 { 301 float a = get(pos_this++); 302 float b = other.get(pos_other++); 303 304 if (a == b) 305 continue; 306 307 if (a < b) 308 return -1; 309 310 return 1; 311 } 312 313 return remaining() - other.remaining(); 314 } 315 316 /** 317 * Returns the byte order of this buffer. 318 */ order()319 public abstract ByteOrder order (); 320 321 /** 322 * Reads the <code>float</code> at this buffer's current position, 323 * and then increments the position. 324 * 325 * @exception BufferUnderflowException If there are no remaining 326 * <code>float</code>s in this buffer. 327 */ get()328 public abstract float get (); 329 330 /** 331 * Writes the <code>float</code> at this buffer's current position, 332 * and then increments the position. 333 * 334 * @exception BufferOverflowException If there no remaining 335 * <code>float</code>s in this buffer. 336 * @exception ReadOnlyBufferException If this buffer is read-only. 337 */ put(float b)338 public abstract FloatBuffer put (float b); 339 340 /** 341 * Absolute get method. 342 * 343 * @exception IndexOutOfBoundsException If index is negative or not smaller 344 * than the buffer's limit. 345 */ get(int index)346 public abstract float get (int index); 347 348 /** 349 * Absolute put method. 350 * 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. 354 */ put(int index, float b)355 public abstract FloatBuffer put (int index, float b); 356 357 /** 358 * Compacts this buffer. 359 * 360 * @exception ReadOnlyBufferException If this buffer is read-only. 361 */ compact()362 public abstract FloatBuffer compact (); 363 364 /** 365 * Tells wether or not this buffer is direct. 366 */ isDirect()367 public abstract boolean isDirect (); 368 369 /** 370 * Creates a new <code>FloatBuffer</code> whose content is a shared 371 * subsequence of this buffer's content. 372 */ slice()373 public abstract FloatBuffer slice (); 374 375 /** 376 * Creates a new <code>FloatBuffer</code> that shares this buffer's 377 * content. 378 */ duplicate()379 public abstract FloatBuffer duplicate (); 380 381 /** 382 * Creates a new read-only <code>FloatBuffer</code> that shares this 383 * buffer's content. 384 */ asReadOnlyBuffer()385 public abstract FloatBuffer asReadOnlyBuffer (); 386 } 387