ShortBuffer.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* ShortBuffer.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: Use RawData instead of gnu.classpath.Pointer
  33. import gnu.gcj.RawData;
  34. /**
  35. * @since 1.4
  36. */
  37. public abstract class ShortBuffer extends Buffer
  38. implements Comparable<ShortBuffer>
  39. {
  40. final int array_offset;
  41. final short[] backing_buffer;
  42. ShortBuffer (int capacity, int limit, int position,
  43. int mark, RawData address, short[] backing_buffer,
  44. int array_offset)
  45. {
  46. super (capacity, limit, position, mark, address);
  47. this.backing_buffer = backing_buffer;
  48. this.array_offset = array_offset;
  49. }
  50. /**
  51. * Allocates a new <code>ShortBuffer</code> object with a given capacity.
  52. */
  53. public static ShortBuffer allocate (int capacity)
  54. {
  55. return new ShortBufferImpl (capacity);
  56. }
  57. /**
  58. * Wraps a <code>short</code> array into a <code>ShortBuffer</code>
  59. * object.
  60. *
  61. * @exception IndexOutOfBoundsException If the preconditions on the offset
  62. * and length parameters do not hold
  63. */
  64. public static final ShortBuffer wrap (short[] array, int offset, int length)
  65. {
  66. return new ShortBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
  67. }
  68. /**
  69. * Wraps a <code>short</code> array into a <code>ShortBuffer</code>
  70. * object.
  71. */
  72. public static final ShortBuffer wrap (short[] array)
  73. {
  74. return wrap (array, 0, array.length);
  75. }
  76. /**
  77. * This method transfers <code>short</code>s from this buffer into the given
  78. * destination array. Before the transfer, it checks if there are fewer than
  79. * length <code>short</code>s remaining in this buffer.
  80. *
  81. * @param dst The destination array
  82. * @param offset The offset within the array of the first <code>short</code>
  83. * to be written; must be non-negative and no larger than dst.length.
  84. * @param length The maximum number of bytes to be written to the given array;
  85. * must be non-negative and no larger than dst.length - offset.
  86. *
  87. * @exception BufferUnderflowException If there are fewer than length
  88. * <code>short</code>s remaining in this buffer.
  89. * @exception IndexOutOfBoundsException If the preconditions on the offset
  90. * and length parameters do not hold.
  91. */
  92. public ShortBuffer get (short[] dst, int offset, int length)
  93. {
  94. checkArraySize(dst.length, offset, length);
  95. checkForUnderflow(length);
  96. for (int i = offset; i < offset + length; i++)
  97. {
  98. dst [i] = get ();
  99. }
  100. return this;
  101. }
  102. /**
  103. * This method transfers <code>short</code>s from this buffer into the given
  104. * destination array.
  105. *
  106. * @param dst The byte array to write into.
  107. *
  108. * @exception BufferUnderflowException If there are fewer than dst.length
  109. * <code>short</code>s remaining in this buffer.
  110. */
  111. public ShortBuffer get (short[] dst)
  112. {
  113. return get (dst, 0, dst.length);
  114. }
  115. /**
  116. * Writes the content of the the <code>ShortBUFFER</code> src
  117. * into the buffer. Before the transfer, it checks if there is fewer than
  118. * <code>src.remaining()</code> space remaining in this buffer.
  119. *
  120. * @param src The source data.
  121. *
  122. * @exception BufferOverflowException If there is insufficient space in this
  123. * buffer for the remaining <code>short</code>s in the source buffer.
  124. * @exception IllegalArgumentException If the source buffer is this buffer.
  125. * @exception ReadOnlyBufferException If this buffer is read-only.
  126. */
  127. public ShortBuffer put (ShortBuffer src)
  128. {
  129. if (src == this)
  130. throw new IllegalArgumentException ();
  131. checkForOverflow(src.remaining ());
  132. if (src.remaining () > 0)
  133. {
  134. short[] toPut = new short [src.remaining ()];
  135. src.get (toPut);
  136. put (toPut);
  137. }
  138. return this;
  139. }
  140. /**
  141. * Writes the content of the the <code>short array</code> src
  142. * into the buffer. Before the transfer, it checks if there is fewer than
  143. * length space remaining in this buffer.
  144. *
  145. * @param src The array to copy into the buffer.
  146. * @param offset The offset within the array of the first byte to be read;
  147. * must be non-negative and no larger than src.length.
  148. * @param length The number of bytes to be read from the given array;
  149. * must be non-negative and no larger than src.length - offset.
  150. *
  151. * @exception BufferOverflowException If there is insufficient space in this
  152. * buffer for the remaining <code>short</code>s in the source array.
  153. * @exception IndexOutOfBoundsException If the preconditions on the offset
  154. * and length parameters do not hold
  155. * @exception ReadOnlyBufferException If this buffer is read-only.
  156. */
  157. public ShortBuffer put (short[] src, int offset, int length)
  158. {
  159. checkArraySize(src.length, offset, length);
  160. checkForOverflow(length);
  161. for (int i = offset; i < offset + length; i++)
  162. put (src [i]);
  163. return this;
  164. }
  165. /**
  166. * Writes the content of the the <code>short array</code> src
  167. * into the buffer.
  168. *
  169. * @param src The array to copy into the buffer.
  170. *
  171. * @exception BufferOverflowException If there is insufficient space in this
  172. * buffer for the remaining <code>short</code>s in the source array.
  173. * @exception ReadOnlyBufferException If this buffer is read-only.
  174. */
  175. public final ShortBuffer put (short[] src)
  176. {
  177. return put (src, 0, src.length);
  178. }
  179. /**
  180. * Tells whether ot not this buffer is backed by an accessible
  181. * <code>short</code> array.
  182. */
  183. public final boolean hasArray ()
  184. {
  185. return (backing_buffer != null
  186. && !isReadOnly ());
  187. }
  188. /**
  189. * Returns the <code>short</code> array that backs this buffer.
  190. *
  191. * @exception ReadOnlyBufferException If this buffer is read-only.
  192. * @exception UnsupportedOperationException If this buffer is not backed
  193. * by an accessible array.
  194. */
  195. public final short[] array ()
  196. {
  197. if (backing_buffer == null)
  198. throw new UnsupportedOperationException ();
  199. checkIfReadOnly();
  200. return backing_buffer;
  201. }
  202. /**
  203. * Returns the offset within this buffer's backing array of the first element.
  204. *
  205. * @exception ReadOnlyBufferException If this buffer is read-only.
  206. * @exception UnsupportedOperationException If this buffer is not backed
  207. * by an accessible array.
  208. */
  209. public final int arrayOffset ()
  210. {
  211. if (backing_buffer == null)
  212. throw new UnsupportedOperationException ();
  213. checkIfReadOnly();
  214. return array_offset;
  215. }
  216. /**
  217. * Calculates a hash code for this buffer.
  218. *
  219. * This is done with <code>int</code> arithmetic,
  220. * where ** represents exponentiation, by this formula:<br>
  221. * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
  222. * (s[limit()-1]+30)*31**(limit()-1)</code>.
  223. * Where s is the buffer data. Note that the hashcode is dependent
  224. * on buffer content, and therefore is not useful if the buffer
  225. * content may change.
  226. *
  227. * @return the hash code
  228. */
  229. public int hashCode ()
  230. {
  231. int hashCode = get(position()) + 31;
  232. int multiplier = 1;
  233. for (int i = position() + 1; i < limit(); ++i)
  234. {
  235. multiplier *= 31;
  236. hashCode += (get(i) + 30)*multiplier;
  237. }
  238. return hashCode;
  239. }
  240. /**
  241. * Checks if this buffer is equal to obj.
  242. */
  243. public boolean equals (Object obj)
  244. {
  245. if (obj instanceof ShortBuffer)
  246. {
  247. return compareTo ((ShortBuffer) obj) == 0;
  248. }
  249. return false;
  250. }
  251. /**
  252. * Compares two <code>ShortBuffer</code> objects.
  253. *
  254. * @exception ClassCastException If obj is not an object derived from
  255. * <code>ShortBuffer</code>.
  256. */
  257. public int compareTo (ShortBuffer other)
  258. {
  259. int num = Math.min(remaining(), other.remaining());
  260. int pos_this = position();
  261. int pos_other = other.position();
  262. for (int count = 0; count < num; count++)
  263. {
  264. short a = get(pos_this++);
  265. short b = other.get(pos_other++);
  266. if (a == b)
  267. continue;
  268. if (a < b)
  269. return -1;
  270. return 1;
  271. }
  272. return remaining() - other.remaining();
  273. }
  274. /**
  275. * Returns the byte order of this buffer.
  276. */
  277. public abstract ByteOrder order ();
  278. /**
  279. * Reads the <code>short</code> at this buffer's current position,
  280. * and then increments the position.
  281. *
  282. * @exception BufferUnderflowException If there are no remaining
  283. * <code>short</code>s in this buffer.
  284. */
  285. public abstract short get ();
  286. /**
  287. * Writes the <code>short</code> at this buffer's current position,
  288. * and then increments the position.
  289. *
  290. * @exception BufferOverflowException If there no remaining
  291. * <code>short</code>s in this buffer.
  292. * @exception ReadOnlyBufferException If this buffer is read-only.
  293. */
  294. public abstract ShortBuffer put (short b);
  295. /**
  296. * Absolute get method.
  297. *
  298. * @exception IndexOutOfBoundsException If index is negative or not smaller
  299. * than the buffer's limit.
  300. */
  301. public abstract short get (int index);
  302. /**
  303. * Absolute put method.
  304. *
  305. * @exception IndexOutOfBoundsException If index is negative or not smaller
  306. * than the buffer's limit.
  307. * @exception ReadOnlyBufferException If this buffer is read-only.
  308. */
  309. public abstract ShortBuffer put (int index, short b);
  310. /**
  311. * Compacts this buffer.
  312. *
  313. * @exception ReadOnlyBufferException If this buffer is read-only.
  314. */
  315. public abstract ShortBuffer compact ();
  316. /**
  317. * Tells wether or not this buffer is direct.
  318. */
  319. public abstract boolean isDirect ();
  320. /**
  321. * Creates a new <code>ShortBuffer</code> whose content is a shared
  322. * subsequence of this buffer's content.
  323. */
  324. public abstract ShortBuffer slice ();
  325. /**
  326. * Creates a new <code>ShortBuffer</code> that shares this buffer's
  327. * content.
  328. */
  329. public abstract ShortBuffer duplicate ();
  330. /**
  331. * Creates a new read-only <code>ShortBuffer</code> that shares this
  332. * buffer's content.
  333. */
  334. public abstract ShortBuffer asReadOnlyBuffer ();
  335. }