FloatBuffer.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* FloatBuffer.java --
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.nio;
  32. // GCJ LOCAL: Change gnu.classpath.Pointer to RawData
  33. import gnu.gcj.RawData;
  34. /**
  35. * @since 1.4
  36. */
  37. public abstract class FloatBuffer extends Buffer
  38. implements Comparable<FloatBuffer>
  39. {
  40. final int array_offset;
  41. final float[] backing_buffer;
  42. FloatBuffer (int capacity, int limit, int position, int mark,
  43. RawData address, float[] backing_buffer, int array_offset)
  44. {
  45. super (capacity, limit, position, mark, address);
  46. this.backing_buffer = backing_buffer;
  47. this.array_offset = array_offset;
  48. }
  49. /**
  50. * Allocates a new <code>FloatBuffer</code> object with a given capacity.
  51. */
  52. public static FloatBuffer allocate (int capacity)
  53. {
  54. return new FloatBufferImpl (capacity);
  55. }
  56. /**
  57. * Wraps a <code>float</code> array into a <code>FloatBuffer</code>
  58. * object.
  59. *
  60. * @exception IndexOutOfBoundsException If the preconditions on the offset
  61. * and length parameters do not hold
  62. */
  63. public static final FloatBuffer wrap (float[] array, int offset, int length)
  64. {
  65. return new FloatBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
  66. }
  67. /**
  68. * Wraps a <code>float</code> array into a <code>FloatBuffer</code>
  69. * object.
  70. */
  71. public static final FloatBuffer wrap (float[] array)
  72. {
  73. return wrap (array, 0, array.length);
  74. }
  75. /**
  76. * This method transfers <code>float</code>s from this buffer into the given
  77. * destination array. Before the transfer, it checks if there are fewer than
  78. * length <code>float</code>s remaining in this buffer.
  79. *
  80. * @param dst The destination array
  81. * @param offset The offset within the array of the first <code>float</code>
  82. * to be written; must be non-negative and no larger than dst.length.
  83. * @param length The maximum number of bytes to be written to the given array;
  84. * must be non-negative and no larger than dst.length - offset.
  85. *
  86. * @exception BufferUnderflowException If there are fewer than length
  87. * <code>float</code>s remaining in this buffer.
  88. * @exception IndexOutOfBoundsException If the preconditions on the offset
  89. * and length parameters do not hold.
  90. */
  91. public FloatBuffer get (float[] dst, int offset, int length)
  92. {
  93. checkArraySize(dst.length, offset, length);
  94. checkForUnderflow(length);
  95. for (int i = offset; i < offset + length; i++)
  96. {
  97. dst [i] = get ();
  98. }
  99. return this;
  100. }
  101. /**
  102. * This method transfers <code>float</code>s from this buffer into the given
  103. * destination array.
  104. *
  105. * @param dst The byte array to write into.
  106. *
  107. * @exception BufferUnderflowException If there are fewer than dst.length
  108. * <code>float</code>s remaining in this buffer.
  109. */
  110. public FloatBuffer get (float[] dst)
  111. {
  112. return get (dst, 0, dst.length);
  113. }
  114. /**
  115. * Writes the content of the the <code>FloatBUFFER</code> src
  116. * into the buffer. Before the transfer, it checks if there is fewer than
  117. * <code>src.remaining()</code> space remaining in this buffer.
  118. *
  119. * @param src The source data.
  120. *
  121. * @exception BufferOverflowException If there is insufficient space in this
  122. * buffer for the remaining <code>float</code>s in the source buffer.
  123. * @exception IllegalArgumentException If the source buffer is this buffer.
  124. * @exception ReadOnlyBufferException If this buffer is read-only.
  125. */
  126. public FloatBuffer put (FloatBuffer src)
  127. {
  128. if (src == this)
  129. throw new IllegalArgumentException ();
  130. checkForOverflow(src.remaining());
  131. if (src.remaining () > 0)
  132. {
  133. float[] toPut = new float [src.remaining ()];
  134. src.get (toPut);
  135. put (toPut);
  136. }
  137. return this;
  138. }
  139. /**
  140. * Writes the content of the the <code>float array</code> src
  141. * into the buffer. Before the transfer, it checks if there is fewer than
  142. * length space remaining in this buffer.
  143. *
  144. * @param src The array to copy into the buffer.
  145. * @param offset The offset within the array of the first byte to be read;
  146. * must be non-negative and no larger than src.length.
  147. * @param length The number of bytes to be read from the given array;
  148. * must be non-negative and no larger than src.length - offset.
  149. *
  150. * @exception BufferOverflowException If there is insufficient space in this
  151. * buffer for the remaining <code>float</code>s in the source array.
  152. * @exception IndexOutOfBoundsException If the preconditions on the offset
  153. * and length parameters do not hold
  154. * @exception ReadOnlyBufferException If this buffer is read-only.
  155. */
  156. public FloatBuffer put (float[] src, int offset, int length)
  157. {
  158. checkArraySize(src.length, offset, length);
  159. checkForOverflow(length);
  160. for (int i = offset; i < offset + length; i++)
  161. put (src [i]);
  162. return this;
  163. }
  164. /**
  165. * Writes the content of the the <code>float array</code> src
  166. * into the buffer.
  167. *
  168. * @param src The array to copy into the buffer.
  169. *
  170. * @exception BufferOverflowException If there is insufficient space in this
  171. * buffer for the remaining <code>float</code>s in the source array.
  172. * @exception ReadOnlyBufferException If this buffer is read-only.
  173. */
  174. public final FloatBuffer put (float[] src)
  175. {
  176. return put (src, 0, src.length);
  177. }
  178. /**
  179. * Tells whether ot not this buffer is backed by an accessible
  180. * <code>float</code> array.
  181. */
  182. public final boolean hasArray ()
  183. {
  184. return (backing_buffer != null
  185. && !isReadOnly ());
  186. }
  187. /**
  188. * Returns the <code>float</code> array that backs this buffer.
  189. *
  190. * @exception ReadOnlyBufferException If this buffer is read-only.
  191. * @exception UnsupportedOperationException If this buffer is not backed
  192. * by an accessible array.
  193. */
  194. public final float[] array ()
  195. {
  196. if (backing_buffer == null)
  197. throw new UnsupportedOperationException ();
  198. checkIfReadOnly();
  199. return backing_buffer;
  200. }
  201. /**
  202. * Returns the offset within this buffer's backing array of the first element.
  203. *
  204. * @exception ReadOnlyBufferException If this buffer is read-only.
  205. * @exception UnsupportedOperationException If this buffer is not backed
  206. * by an accessible array.
  207. */
  208. public final int arrayOffset ()
  209. {
  210. if (backing_buffer == null)
  211. throw new UnsupportedOperationException ();
  212. checkIfReadOnly();
  213. return array_offset;
  214. }
  215. /**
  216. * Calculates a hash code for this buffer.
  217. *
  218. * This is done with <code>int</code> arithmetic,
  219. * where ** represents exponentiation, by this formula:<br>
  220. * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
  221. * (s[limit()-1]+30)*31**(limit()-1)</code>.
  222. * Where s is the buffer data, in Float.floatToIntBits() form
  223. * Note that the hashcode is dependent on buffer content,
  224. * and therefore is not useful if the buffer content may change.
  225. *
  226. * @return the hash code
  227. */
  228. public int hashCode ()
  229. {
  230. int hashCode = Float.floatToIntBits(get(position())) + 31;
  231. int multiplier = 1;
  232. for (int i = position() + 1; i < limit(); ++i)
  233. {
  234. multiplier *= 31;
  235. hashCode += (Float.floatToIntBits(get(i)) + 30)*multiplier;
  236. }
  237. return hashCode;
  238. }
  239. /**
  240. * Checks if this buffer is equal to obj.
  241. */
  242. public boolean equals (Object obj)
  243. {
  244. if (obj instanceof FloatBuffer)
  245. {
  246. return compareTo ((FloatBuffer) obj) == 0;
  247. }
  248. return false;
  249. }
  250. /**
  251. * Compares two <code>FloatBuffer</code> objects.
  252. *
  253. * @exception ClassCastException If obj is not an object derived from
  254. * <code>FloatBuffer</code>.
  255. */
  256. public int compareTo (FloatBuffer other)
  257. {
  258. int num = Math.min(remaining(), other.remaining());
  259. int pos_this = position();
  260. int pos_other = other.position();
  261. for (int count = 0; count < num; count++)
  262. {
  263. float a = get(pos_this++);
  264. float b = other.get(pos_other++);
  265. if (a == b)
  266. continue;
  267. if (a < b)
  268. return -1;
  269. return 1;
  270. }
  271. return remaining() - other.remaining();
  272. }
  273. /**
  274. * Returns the byte order of this buffer.
  275. */
  276. public abstract ByteOrder order ();
  277. /**
  278. * Reads the <code>float</code> at this buffer's current position,
  279. * and then increments the position.
  280. *
  281. * @exception BufferUnderflowException If there are no remaining
  282. * <code>float</code>s in this buffer.
  283. */
  284. public abstract float get ();
  285. /**
  286. * Writes the <code>float</code> at this buffer's current position,
  287. * and then increments the position.
  288. *
  289. * @exception BufferOverflowException If there no remaining
  290. * <code>float</code>s in this buffer.
  291. * @exception ReadOnlyBufferException If this buffer is read-only.
  292. */
  293. public abstract FloatBuffer put (float b);
  294. /**
  295. * Absolute get method.
  296. *
  297. * @exception IndexOutOfBoundsException If index is negative or not smaller
  298. * than the buffer's limit.
  299. */
  300. public abstract float get (int index);
  301. /**
  302. * Absolute put method.
  303. *
  304. * @exception IndexOutOfBoundsException If index is negative or not smaller
  305. * than the buffer's limit.
  306. * @exception ReadOnlyBufferException If this buffer is read-only.
  307. */
  308. public abstract FloatBuffer put (int index, float b);
  309. /**
  310. * Compacts this buffer.
  311. *
  312. * @exception ReadOnlyBufferException If this buffer is read-only.
  313. */
  314. public abstract FloatBuffer compact ();
  315. /**
  316. * Tells wether or not this buffer is direct.
  317. */
  318. public abstract boolean isDirect ();
  319. /**
  320. * Creates a new <code>FloatBuffer</code> whose content is a shared
  321. * subsequence of this buffer's content.
  322. */
  323. public abstract FloatBuffer slice ();
  324. /**
  325. * Creates a new <code>FloatBuffer</code> that shares this buffer's
  326. * content.
  327. */
  328. public abstract FloatBuffer duplicate ();
  329. /**
  330. * Creates a new read-only <code>FloatBuffer</code> that shares this
  331. * buffer's content.
  332. */
  333. public abstract FloatBuffer asReadOnlyBuffer ();
  334. }