ByteBuffer.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* ByteBuffer.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. import gnu.classpath.Pointer;
  35. /**
  36. * @since 1.4
  37. */
  38. public abstract class ByteBuffer extends Buffer
  39. implements Comparable<ByteBuffer>
  40. {
  41. ByteOrder endian = ByteOrder.BIG_ENDIAN;
  42. final byte[] backing_buffer;
  43. final int array_offset;
  44. ByteBuffer (int capacity, int limit, int position, int mark,
  45. RawData address, byte[] backing_buffer, int array_offset)
  46. {
  47. super (capacity, limit, position, mark, address);
  48. this.backing_buffer = backing_buffer;
  49. this.array_offset = array_offset;
  50. }
  51. /**
  52. * Allocates a new direct byte buffer.
  53. */
  54. public static ByteBuffer allocateDirect (int capacity)
  55. {
  56. return DirectByteBufferImpl.allocate (capacity);
  57. }
  58. /**
  59. * Allocates a new <code>ByteBuffer</code> object with a given capacity.
  60. */
  61. public static ByteBuffer allocate (int capacity)
  62. {
  63. return wrap(new byte[capacity], 0, capacity);
  64. }
  65. /**
  66. * Wraps a <code>byte</code> array into a <code>ByteBuffer</code>
  67. * object.
  68. *
  69. * @exception IndexOutOfBoundsException If the preconditions on the offset
  70. * and length parameters do not hold
  71. */
  72. public static final ByteBuffer wrap (byte[] array, int offset, int length)
  73. {
  74. // FIXME: In GCJ and other implementations where arrays may not
  75. // move we might consider, at least when offset==0:
  76. // return new DirectByteBufferImpl(array,
  77. // address_of_data(array) + offset,
  78. // length, length, 0, false);
  79. // This may be more efficient, mainly because we can then use the
  80. // same logic for all ByteBuffers.
  81. return new ByteBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
  82. }
  83. /**
  84. * Wraps a <code>byte</code> array into a <code>ByteBuffer</code>
  85. * object.
  86. */
  87. public static final ByteBuffer wrap (byte[] array)
  88. {
  89. return wrap (array, 0, array.length);
  90. }
  91. /**
  92. * This method transfers <code>byte</code>s from this buffer into the given
  93. * destination array. Before the transfer, it checks if there are fewer than
  94. * length <code>byte</code>s remaining in this buffer.
  95. *
  96. * @param dst The destination array
  97. * @param offset The offset within the array of the first <code>byte</code>
  98. * to be written; 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.
  101. *
  102. * @exception BufferUnderflowException If there are fewer than length
  103. * <code>byte</code>s remaining in this buffer.
  104. * @exception IndexOutOfBoundsException If the preconditions on the offset
  105. * and length parameters do not hold.
  106. */
  107. public ByteBuffer get (byte[] dst, int offset, int length)
  108. {
  109. checkArraySize(dst.length, offset, length);
  110. checkForUnderflow(length);
  111. for (int i = offset; i < offset + length; i++)
  112. {
  113. dst [i] = get ();
  114. }
  115. return this;
  116. }
  117. /**
  118. * This method transfers <code>byte</code>s from this buffer into the given
  119. * destination array.
  120. *
  121. * @param dst The byte array to write into.
  122. *
  123. * @exception BufferUnderflowException If there are fewer than dst.length
  124. * <code>byte</code>s remaining in this buffer.
  125. */
  126. public ByteBuffer get (byte[] dst)
  127. {
  128. return get (dst, 0, dst.length);
  129. }
  130. /**
  131. * Writes the content of the the <code>ByteBUFFER</code> src
  132. * into the buffer. Before the transfer, it checks if there is fewer than
  133. * <code>src.remaining()</code> space remaining in this buffer.
  134. *
  135. * @param src The source data.
  136. *
  137. * @exception BufferOverflowException If there is insufficient space in this
  138. * buffer for the remaining <code>byte</code>s in the source buffer.
  139. * @exception IllegalArgumentException If the source buffer is this buffer.
  140. * @exception ReadOnlyBufferException If this buffer is read-only.
  141. */
  142. public ByteBuffer put (ByteBuffer src)
  143. {
  144. if (src == this)
  145. throw new IllegalArgumentException ();
  146. checkForOverflow(src.remaining());
  147. if (src.remaining () > 0)
  148. {
  149. byte[] toPut = new byte [src.remaining ()];
  150. src.get (toPut);
  151. put (toPut);
  152. }
  153. return this;
  154. }
  155. /**
  156. * Writes the content of the the <code>byte array</code> src
  157. * into the buffer. Before the transfer, it checks if there is fewer than
  158. * length space remaining in this buffer.
  159. *
  160. * @param src The array to copy into the buffer.
  161. * @param offset The offset within the array of the first byte to be read;
  162. * must be non-negative and no larger than src.length.
  163. * @param length The number of bytes to be read from the given array;
  164. * must be non-negative and no larger than src.length - offset.
  165. *
  166. * @exception BufferOverflowException If there is insufficient space in this
  167. * buffer for the remaining <code>byte</code>s in the source array.
  168. * @exception IndexOutOfBoundsException If the preconditions on the offset
  169. * and length parameters do not hold
  170. * @exception ReadOnlyBufferException If this buffer is read-only.
  171. */
  172. public ByteBuffer put (byte[] src, int offset, int length)
  173. {
  174. checkArraySize(src.length, offset, length);
  175. checkForOverflow(length);
  176. for (int i = offset; i < offset + length; i++)
  177. put (src [i]);
  178. return this;
  179. }
  180. /**
  181. * Writes the content of the the <code>byte array</code> src
  182. * into the buffer.
  183. *
  184. * @param src The array to copy into the buffer.
  185. *
  186. * @exception BufferOverflowException If there is insufficient space in this
  187. * buffer for the remaining <code>byte</code>s in the source array.
  188. * @exception ReadOnlyBufferException If this buffer is read-only.
  189. */
  190. public final ByteBuffer put (byte[] src)
  191. {
  192. return put (src, 0, src.length);
  193. }
  194. /**
  195. * Tells whether ot not this buffer is backed by an accessible
  196. * <code>byte</code> array.
  197. */
  198. public final boolean hasArray ()
  199. {
  200. return (backing_buffer != null
  201. && !isReadOnly ());
  202. }
  203. /**
  204. * Returns the <code>byte</code> array that backs this buffer.
  205. *
  206. * @exception ReadOnlyBufferException If this buffer is read-only.
  207. * @exception UnsupportedOperationException If this buffer is not backed
  208. * by an accessible array.
  209. */
  210. public final byte[] array ()
  211. {
  212. if (backing_buffer == null)
  213. throw new UnsupportedOperationException ();
  214. checkIfReadOnly();
  215. return backing_buffer;
  216. }
  217. /**
  218. * Returns the offset within this buffer's backing array of the first element.
  219. *
  220. * @exception ReadOnlyBufferException If this buffer is read-only.
  221. * @exception UnsupportedOperationException If this buffer is not backed
  222. * by an accessible array.
  223. */
  224. public final int arrayOffset ()
  225. {
  226. if (backing_buffer == null)
  227. throw new UnsupportedOperationException ();
  228. checkIfReadOnly();
  229. return array_offset;
  230. }
  231. /**
  232. * Calculates a hash code for this buffer.
  233. *
  234. * This is done with <code>int</code> arithmetic,
  235. * where ** represents exponentiation, by this formula:<br>
  236. * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
  237. * (s[limit()-1]+30)*31**(limit()-1)</code>.
  238. * Where s is the buffer data. Note that the hashcode is dependent
  239. * on buffer content, and therefore is not useful if the buffer
  240. * content may change.
  241. *
  242. * @return the hash code
  243. */
  244. public int hashCode ()
  245. {
  246. int hashCode = get(position()) + 31;
  247. int multiplier = 1;
  248. for (int i = position() + 1; i < limit(); ++i)
  249. {
  250. multiplier *= 31;
  251. hashCode += (get(i) + 30)*multiplier;
  252. }
  253. return hashCode;
  254. }
  255. /**
  256. * Checks if this buffer is equal to obj.
  257. */
  258. public boolean equals (Object obj)
  259. {
  260. if (obj instanceof ByteBuffer)
  261. {
  262. return compareTo ((ByteBuffer) obj) == 0;
  263. }
  264. return false;
  265. }
  266. /**
  267. * Compares two <code>ByteBuffer</code> objects.
  268. *
  269. * @exception ClassCastException If obj is not an object derived from
  270. * <code>ByteBuffer</code>.
  271. */
  272. public int compareTo (ByteBuffer other)
  273. {
  274. int num = Math.min(remaining(), other.remaining());
  275. int pos_this = position();
  276. int pos_other = other.position();
  277. for (int count = 0; count < num; count++)
  278. {
  279. byte a = get(pos_this++);
  280. byte b = other.get(pos_other++);
  281. if (a == b)
  282. continue;
  283. if (a < b)
  284. return -1;
  285. return 1;
  286. }
  287. return remaining() - other.remaining();
  288. }
  289. /**
  290. * Returns the byte order of this buffer.
  291. */
  292. public final ByteOrder order ()
  293. {
  294. return endian;
  295. }
  296. /**
  297. * Modifies this buffer's byte order.
  298. */
  299. public final ByteBuffer order (ByteOrder endian)
  300. {
  301. this.endian = endian;
  302. return this;
  303. }
  304. /**
  305. * Reads the <code>byte</code> at this buffer's current position,
  306. * and then increments the position.
  307. *
  308. * @exception BufferUnderflowException If there are no remaining
  309. * <code>byte</code>s in this buffer.
  310. */
  311. public abstract byte get ();
  312. /**
  313. * Writes the <code>byte</code> at this buffer's current position,
  314. * and then increments the position.
  315. *
  316. * @exception BufferOverflowException If there no remaining
  317. * <code>byte</code>s in this buffer.
  318. * @exception ReadOnlyBufferException If this buffer is read-only.
  319. */
  320. public abstract ByteBuffer put (byte b);
  321. /**
  322. * Absolute get method.
  323. *
  324. * @exception IndexOutOfBoundsException If index is negative or not smaller
  325. * than the buffer's limit.
  326. */
  327. public abstract byte get (int index);
  328. /**
  329. * Absolute put method.
  330. *
  331. * @exception IndexOutOfBoundsException If index is negative or not smaller
  332. * than the buffer's limit.
  333. * @exception ReadOnlyBufferException If this buffer is read-only.
  334. */
  335. public abstract ByteBuffer put (int index, byte b);
  336. /**
  337. * Compacts this buffer.
  338. *
  339. * @exception ReadOnlyBufferException If this buffer is read-only.
  340. */
  341. public abstract ByteBuffer compact ();
  342. void shiftDown (int dst_offset, int src_offset, int count)
  343. {
  344. for (int i = 0; i < count; i++)
  345. put(dst_offset + i, get(src_offset + i));
  346. }
  347. /**
  348. * Tells whether or not this buffer is direct.
  349. */
  350. public abstract boolean isDirect ();
  351. /**
  352. * Creates a new <code>ByteBuffer</code> whose content is a shared
  353. * subsequence of this buffer's content.
  354. */
  355. public abstract ByteBuffer slice ();
  356. /**
  357. * Creates a new <code>ByteBuffer</code> that shares this buffer's
  358. * content.
  359. */
  360. public abstract ByteBuffer duplicate ();
  361. /**
  362. * Creates a new read-only <code>ByteBuffer</code> that shares this
  363. * buffer's content.
  364. */
  365. public abstract ByteBuffer asReadOnlyBuffer ();
  366. /**
  367. * Creates a view of this byte buffer as a short buffer.
  368. */
  369. public abstract ShortBuffer asShortBuffer ();
  370. /**
  371. * Creates a view of this byte buffer as a char buffer.
  372. */
  373. public abstract CharBuffer asCharBuffer ();
  374. /**
  375. * Creates a view of this byte buffer as an integer buffer.
  376. */
  377. public abstract IntBuffer asIntBuffer ();
  378. /**
  379. * Creates a view of this byte buffer as a long buffer.
  380. */
  381. public abstract LongBuffer asLongBuffer ();
  382. /**
  383. * Creates a view of this byte buffer as a float buffer.
  384. */
  385. public abstract FloatBuffer asFloatBuffer ();
  386. /**
  387. * Creates a view of this byte buffer as a double buffer.
  388. */
  389. public abstract DoubleBuffer asDoubleBuffer ();
  390. /**
  391. * Relative get method for reading a character value.
  392. *
  393. * @exception BufferUnderflowException If there are fewer than two bytes
  394. * remaining in this buffer.
  395. */
  396. public abstract char getChar ();
  397. /**
  398. * Relative put method for writing a character value.
  399. *
  400. * @exception BufferOverflowException If this buffer's current position is
  401. * not smaller than its limit.
  402. */
  403. public abstract ByteBuffer putChar (char value);
  404. /**
  405. * Absolute get method for reading a character value.
  406. *
  407. * @exception IndexOutOfBoundsException If there are fewer than two bytes
  408. * remaining in this buffer
  409. */
  410. public abstract char getChar (int index);
  411. /**
  412. * Absolute put method for writing a character value.
  413. *
  414. * @exception IndexOutOfBoundsException If index is negative or not smaller
  415. * than the buffer's limit, minus one.
  416. */
  417. public abstract ByteBuffer putChar (int index, char value);
  418. /**
  419. * Relative get method for reading a short value.
  420. *
  421. * @exception BufferUnderflowException If index is negative or not smaller
  422. * than the buffer's limit, minus one.
  423. */
  424. public abstract short getShort ();
  425. /**
  426. * Relative put method for writing a short value.
  427. *
  428. * @exception BufferOverflowException If this buffer's current position is
  429. * not smaller than its limit.
  430. */
  431. public abstract ByteBuffer putShort (short value);
  432. /**
  433. * Absolute get method for reading a short value.
  434. *
  435. * @exception IndexOutOfBoundsException If there are fewer than two bytes
  436. * remaining in this buffer
  437. */
  438. public abstract short getShort (int index);
  439. /**
  440. * Absolute put method for writing a short value.
  441. *
  442. * @exception IndexOutOfBoundsException If index is negative or not smaller
  443. * than the buffer's limit, minus one.
  444. */
  445. public abstract ByteBuffer putShort (int index, short value);
  446. /**
  447. * Relative get method for reading an integer value.
  448. *
  449. * @exception BufferUnderflowException If there are fewer than four bytes
  450. * remaining in this buffer.
  451. */
  452. public abstract int getInt ();
  453. /**
  454. * Relative put method for writing an integer value.
  455. *
  456. * @exception BufferOverflowException If this buffer's current position is
  457. * not smaller than its limit.
  458. */
  459. public abstract ByteBuffer putInt (int value);
  460. /**
  461. * Absolute get method for reading an integer value.
  462. *
  463. * @exception IndexOutOfBoundsException If index is negative or not smaller
  464. * than the buffer's limit, minus three.
  465. */
  466. public abstract int getInt (int index);
  467. /**
  468. * Absolute put method for writing an integer value.
  469. *
  470. * @exception IndexOutOfBoundsException If index is negative or not smaller
  471. * than the buffer's limit, minus three.
  472. */
  473. public abstract ByteBuffer putInt (int index, int value);
  474. /**
  475. * Relative get method for reading a long value.
  476. *
  477. * @exception BufferUnderflowException If there are fewer than eight bytes
  478. * remaining in this buffer.
  479. */
  480. public abstract long getLong ();
  481. /**
  482. * Relative put method for writing a long value.
  483. *
  484. * @exception BufferOverflowException If this buffer's current position is
  485. * not smaller than its limit.
  486. */
  487. public abstract ByteBuffer putLong (long value);
  488. /**
  489. * Absolute get method for reading a long value.
  490. *
  491. * @exception IndexOutOfBoundsException If index is negative or not smaller
  492. * than the buffer's limit, minus seven.
  493. */
  494. public abstract long getLong (int index);
  495. /**
  496. * Absolute put method for writing a float value.
  497. *
  498. * @exception IndexOutOfBoundsException If index is negative or not smaller
  499. * than the buffer's limit, minus seven.
  500. */
  501. public abstract ByteBuffer putLong (int index, long value);
  502. /**
  503. * Relative get method for reading a float value.
  504. *
  505. * @exception BufferUnderflowException If there are fewer than four bytes
  506. * remaining in this buffer.
  507. */
  508. public abstract float getFloat ();
  509. /**
  510. * Relative put method for writing a float value.
  511. *
  512. * @exception BufferOverflowException If there are fewer than four bytes
  513. * remaining in this buffer.
  514. */
  515. public abstract ByteBuffer putFloat (float value);
  516. /**
  517. * Absolute get method for reading a float value.
  518. *
  519. * @exception IndexOutOfBoundsException If index is negative or not smaller
  520. * than the buffer's limit, minus three.
  521. */
  522. public abstract float getFloat (int index);
  523. /**
  524. * Relative put method for writing a float value.
  525. *
  526. * @exception IndexOutOfBoundsException If index is negative or not smaller
  527. * than the buffer's limit, minus three.
  528. */
  529. public abstract ByteBuffer putFloat (int index, float value);
  530. /**
  531. * Relative get method for reading a double value.
  532. *
  533. * @exception BufferUnderflowException If there are fewer than eight bytes
  534. * remaining in this buffer.
  535. */
  536. public abstract double getDouble ();
  537. /**
  538. * Relative put method for writing a double value.
  539. *
  540. * @exception BufferOverflowException If this buffer's current position is
  541. * not smaller than its limit.
  542. */
  543. public abstract ByteBuffer putDouble (double value);
  544. /**
  545. * Absolute get method for reading a double value.
  546. *
  547. * @exception IndexOutOfBoundsException If index is negative or not smaller
  548. * than the buffer's limit, minus seven.
  549. */
  550. public abstract double getDouble (int index);
  551. /**
  552. * Absolute put method for writing a double value.
  553. *
  554. * @exception IndexOutOfBoundsException If index is negative or not smaller
  555. * than the buffer's limit, minus seven.
  556. */
  557. public abstract ByteBuffer putDouble (int index, double value);
  558. /**
  559. * Returns a string summarizing the state of this buffer.
  560. */
  561. public String toString ()
  562. {
  563. return getClass ().getName () +
  564. "[pos=" + position () +
  565. " lim=" + limit () +
  566. " cap=" + capacity () + "]";
  567. }
  568. }