1. /*
  2. * @(#)X-Buffer.java 1.48 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. // -- This file was mechanically generated: Do not edit! -- //
  8. package java.nio;
  9. /**
  10. * A byte buffer.
  11. *
  12. * <p> This class defines six categories of operations upon
  13. * byte buffers:
  14. *
  15. * <ul>
  16. *
  17. * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
  18. * {@link #put(byte) </code><i>put</i><code>} methods that read and write
  19. * single bytes; </p></li>
  20. *
  21. * <li><p> Relative {@link #get(byte[]) </code><i>bulk get</i><code>}
  22. * methods that transfer contiguous sequences of bytes from this buffer
  23. * into an array; </p></li>
  24. *
  25. * <li><p> Relative {@link #put(byte[]) </code><i>bulk put</i><code>}
  26. * methods that transfer contiguous sequences of bytes from a
  27. * byte array or some other byte
  28. * buffer into this buffer; </p></li>
  29. *
  30. *
  31. * <li><p> Absolute and relative {@link #getChar() </code><i>get</i><code>}
  32. * and {@link #putChar(char) </code><i>put</i><code>} methods that read and
  33. * write values of other primitive types, translating them to and from
  34. * sequences of bytes in a particular byte order; </p></li>
  35. *
  36. * <li><p> Methods for creating <i><a href="#views">view buffers</a></i>,
  37. * which allow a byte buffer to be viewed as a buffer containing values of
  38. * some other primitive type; and </p></li>
  39. *
  40. *
  41. * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
  42. * #duplicate </code>duplicating<code>}, and {@link #slice
  43. * </code>slicing<code>} a byte buffer. </p></li>
  44. *
  45. * </ul>
  46. *
  47. * <p> Byte buffers can be created either by {@link #allocate
  48. * </code><i>allocation</i><code>}, which allocates space for the buffer's
  49. *
  50. *
  51. * content, or by {@link #wrap(byte[]) </code><i>wrapping</i><code>} an
  52. * existing byte array into a buffer.
  53. *
  54. *
  55. *
  56. * <a name="direct">
  57. * <h4> Direct <i>vs.</i> non-direct buffers </h4>
  58. *
  59. * <p> A byte buffer is either <i>direct</i> or <i>non-direct</i>. Given a
  60. * direct byte buffer, the Java virtual machine will make a best effort to
  61. * perform native I/O operations directly upon it. That is, it will attempt to
  62. * avoid copying the buffer's content to (or from) an intermediate buffer
  63. * before (or after) each invocation of one of the underlying operating
  64. * system's native I/O operations.
  65. *
  66. * <p> A direct byte buffer may be created by invoking the {@link
  67. * #allocateDirect(int) allocateDirect} factory method of this class. The
  68. * buffers returned by this method typically have somewhat higher allocation
  69. * and deallocation costs than non-direct buffers. The contents of direct
  70. * buffers may reside outside of the normal garbage-collected heap, and so
  71. * their impact upon the memory footprint of an application might not be
  72. * obvious. It is therefore recommended that direct buffers be allocated
  73. * primarily for large, long-lived buffers that are subject to the underlying
  74. * system's native I/O operations. In general it is best to allocate direct
  75. * buffers only when they yield a measureable gain in program performance.
  76. *
  77. * <p> A direct byte buffer may also be created by {@link
  78. * java.nio.channels.FileChannel#map </code>mapping<code>} a region of a file
  79. * directly into memory. An implementation of the Java platform may optionally
  80. * support the creation of direct byte buffers from native code via JNI. If an
  81. * instance of one of these kinds of buffers refers to an inaccessible region
  82. * of memory then an attempt to access that region will not change the buffer's
  83. * content and will cause an unspecified exception to be thrown either at the
  84. * time of the access or at some later time.
  85. *
  86. * <p> Whether a byte buffer is direct or non-direct may be determined by
  87. * invoking its {@link #isDirect isDirect} method. This method is provided so
  88. * that explicit buffer management can be done in performance-critical code.
  89. *
  90. *
  91. * <a name="bin">
  92. * <h4> Access to binary data </h4>
  93. *
  94. * <p> This class defines methods for reading and writing values of all other
  95. * primitive types, except <tt>boolean</tt>. Primitive values are translated
  96. * to (or from) sequences of bytes according to the buffer's current byte
  97. * order, which may be retrieved and modified via the {@link #order order}
  98. * methods. Specific byte orders are represented by instances of the {@link
  99. * ByteOrder} class. The initial order of a byte buffer is always {@link
  100. * ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
  101. *
  102. * <p> For access to heterogenous binary data, that is, sequences of values of
  103. * different types, this class defines a family of absolute and relative
  104. * <i>get</i> and <i>put</i> methods for each type. For 32-bit floating-point
  105. * values, for example, this class defines:
  106. *
  107. * <blockquote><pre>
  108. * float {@link #getFloat()}
  109. * float {@link #getFloat(int) getFloat(int index)}
  110. * void {@link #putFloat(float) putFloat(float f)}
  111. * void {@link #putFloat(int,float) putFloat(int index, float f)}</pre></blockquote>
  112. *
  113. * <p> Corresponding methods are defined for the types <tt>char</tt>,
  114. * <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and <tt>double</tt>. The index
  115. * parameters of the absolute <i>get</i> and <i>put</i> methods are in terms of
  116. * bytes rather than of the type being read or written.
  117. *
  118. * <a name="views">
  119. *
  120. * <p> For access to homogeneous binary data, that is, sequences of values of
  121. * the same type, this class defines methods that can create <i>views</i> of a
  122. * given byte buffer. A <i>view buffer</i> is simply another buffer whose
  123. * content is backed by the byte buffer. Changes to the byte buffer's content
  124. * will be visible in the view buffer, and vice versa; the two buffers'
  125. * position, limit, and mark values are independent. The {@link
  126. * #asFloatBuffer() asFloatBuffer} method, for example, creates an instance of
  127. * the {@link FloatBuffer} class that is backed by the byte buffer upon which
  128. * the method is invoked. Corresponding view-creation methods are defined for
  129. * the types <tt>char</tt>, <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and
  130. * <tt>double</tt>.
  131. *
  132. * <p> View buffers have three important advantages over the families of
  133. * type-specific <i>get</i> and <i>put</i> methods described above:
  134. *
  135. * <ul>
  136. *
  137. * <li><p> A view buffer is indexed not in terms of bytes but rather in terms
  138. * of the type-specific size of its values; </p></li>
  139. *
  140. * <li><p> A view buffer provides relative bulk <i>get</i> and <i>put</i>
  141. * methods that can transfer contiguous sequences of values between a buffer
  142. * and an array or some other buffer of the same type; and </p></li>
  143. *
  144. * <li><p> A view buffer is potentially much more efficient because it will
  145. * be direct if, and only if, its backing byte buffer is direct. </p></li>
  146. *
  147. * </ul>
  148. *
  149. * <p> The byte order of a view buffer is fixed to be that of its byte buffer
  150. * at the time that the view is created. </p>
  151. *
  152. *
  153. *
  154. *
  155. * <h4> Invocation chaining </h4>
  156. *
  157. * <p> Methods in this class that do not otherwise have a value to return are
  158. * specified to return the buffer upon which they are invoked. This allows
  159. * method invocations to be chained.
  160. *
  161. *
  162. * The sequence of statements
  163. *
  164. * <blockquote><pre>
  165. * bb.putInt(0xCAFEBABE);
  166. * bb.putShort(3);
  167. * bb.putShort(45);</pre></blockquote>
  168. *
  169. * can, for example, be replaced by the single statement
  170. *
  171. * <blockquote><pre>
  172. * bb.putInt(0xCAFEBABE).putShort(3).putShort(45);</pre></blockquote>
  173. *
  174. *
  175. *
  176. * @author Mark Reinhold
  177. * @author JSR-51 Expert Group
  178. * @version 1.48, 03/01/23
  179. * @since 1.4
  180. */
  181. public abstract class ByteBuffer
  182. extends Buffer
  183. implements Comparable
  184. {
  185. // These fields are declared here rather than in Heap-X-Buffer in order to
  186. // reduce the number of virtual method invocations needed to access these
  187. // values, which is especially costly when coding small buffers.
  188. //
  189. final byte[] hb; // Non-null only for heap buffers
  190. final int offset;
  191. boolean isReadOnly; // Valid only for heap buffers
  192. // Creates a new buffer with the given mark, position, limit, capacity,
  193. // backing array, and array offset
  194. //
  195. ByteBuffer(int mark, int pos, int lim, int cap, // package-private
  196. byte[] hb, int offset)
  197. {
  198. super(mark, pos, lim, cap);
  199. this.hb = hb;
  200. this.offset = offset;
  201. }
  202. // Creates a new buffer with the given mark, position, limit, and capacity
  203. //
  204. ByteBuffer(int mark, int pos, int lim, int cap) { // package-private
  205. this(mark, pos, lim, cap, null, 0);
  206. }
  207. /**
  208. * Allocates a new direct byte buffer.
  209. *
  210. * <p> The new buffer's position will be zero, its limit will be its
  211. * capacity, and its mark will be undefined. Whether or not it has a
  212. * {@link #hasArray </code>backing array<code>} is unspecified. </p>
  213. *
  214. * @param capacity
  215. * The new buffer's capacity, in bytes
  216. *
  217. * @return The new byte buffer
  218. *
  219. * @throws IllegalArgumentException
  220. * If the <tt>capacity</tt> is a negative integer
  221. */
  222. public static ByteBuffer allocateDirect(int capacity) {
  223. return new DirectByteBuffer(capacity);
  224. }
  225. /**
  226. * Allocates a new byte buffer.
  227. *
  228. * <p> The new buffer's position will be zero, its limit will be its
  229. * capacity, and its mark will be undefined. It will have a {@link #array
  230. * </code>backing array<code>}, and its {@link #arrayOffset </code>array
  231. * offset<code>} will be zero.
  232. *
  233. * @param capacity
  234. * The new buffer's capacity, in bytes
  235. *
  236. * @return The new byte buffer
  237. *
  238. * @throws IllegalArgumentException
  239. * If the <tt>capacity</tt> is a negative integer
  240. */
  241. public static ByteBuffer allocate(int capacity) {
  242. if (capacity < 0)
  243. throw new IllegalArgumentException();
  244. return new HeapByteBuffer(capacity, capacity);
  245. }
  246. /**
  247. * Wraps a byte array into a buffer.
  248. *
  249. * <p> The new buffer will be backed by the the given byte array;
  250. * that is, modifications to the buffer will cause the array to be modified
  251. * and vice versa. The new buffer's capacity will be
  252. * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
  253. * will be <tt>offset + length</tt>, and its mark will be undefined. Its
  254. * {@link #array </code>backing array<code>} will be the given array, and
  255. * its {@link #arrayOffset </code>array offset<code>} will be zero. </p>
  256. *
  257. * @param array
  258. * The array that will back the new buffer
  259. *
  260. * @param offset
  261. * The offset of the subarray to be used; must be non-negative and
  262. * no larger than <tt>array.length</tt>. The new buffer's position
  263. * will be set to this value.
  264. *
  265. * @param length
  266. * The length of the subarray to be used;
  267. * must be non-negative and no larger than
  268. * <tt>array.length - offset</tt>.
  269. * The new buffer's limit will be set to <tt>offset + length</tt>.
  270. *
  271. * @return The new byte buffer
  272. *
  273. * @throws IndexOutOfBoundsException
  274. * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
  275. * parameters do not hold
  276. */
  277. public static ByteBuffer wrap(byte[] array,
  278. int offset, int length)
  279. {
  280. try {
  281. return new HeapByteBuffer(array, offset, length);
  282. } catch (IllegalArgumentException x) {
  283. throw new IndexOutOfBoundsException();
  284. }
  285. }
  286. /**
  287. * Wraps a byte array into a buffer.
  288. *
  289. * <p> The new buffer will be backed by the the given byte array;
  290. * that is, modifications to the buffer will cause the array to be modified
  291. * and vice versa. The new buffer's capacity and limit will be
  292. * <tt>array.length</tt>, its position will be zero, and its mark will be
  293. * undefined. Its {@link #array </code>backing array<code>} will be the
  294. * given array, and its {@link #arrayOffset </code>array offset<code>} will
  295. * be zero. </p>
  296. *
  297. * @param array
  298. * The array that will back this buffer
  299. *
  300. * @return The new byte buffer
  301. */
  302. public static ByteBuffer wrap(byte[] array) {
  303. return wrap(array, 0, array.length);
  304. }
  305. /**
  306. * Creates a new byte buffer whose content is a shared subsequence of
  307. * this buffer's content.
  308. *
  309. * <p> The content of the new buffer will start at this buffer's current
  310. * position. Changes to this buffer's content will be visible in the new
  311. * buffer, and vice versa; the two buffers' position, limit, and mark
  312. * values will be independent.
  313. *
  314. * <p> The new buffer's position will be zero, its capacity and its limit
  315. * will be the number of bytes remaining in this buffer, and its mark
  316. * will be undefined. The new buffer will be direct if, and only if, this
  317. * buffer is direct, and it will be read-only if, and only if, this buffer
  318. * is read-only. </p>
  319. *
  320. * @return The new byte buffer
  321. */
  322. public abstract ByteBuffer slice();
  323. /**
  324. * Creates a new byte buffer that shares this buffer's content.
  325. *
  326. * <p> The content of the new buffer will be that of this buffer. Changes
  327. * to this buffer's content will be visible in the new buffer, and vice
  328. * versa; the two buffers' position, limit, and mark values will be
  329. * independent.
  330. *
  331. * <p> The new buffer's capacity, limit, position, and mark values will be
  332. * identical to those of this buffer. The new buffer will be direct if,
  333. * and only if, this buffer is direct, and it will be read-only if, and
  334. * only if, this buffer is read-only. </p>
  335. *
  336. * @return The new byte buffer
  337. */
  338. public abstract ByteBuffer duplicate();
  339. /**
  340. * Creates a new, read-only byte buffer that shares this buffer's
  341. * content.
  342. *
  343. * <p> The content of the new buffer will be that of this buffer. Changes
  344. * to this buffer's content will be visible in the new buffer; the new
  345. * buffer itself, however, will be read-only and will not allow the shared
  346. * content to be modified. The two buffers' position, limit, and mark
  347. * values will be independent.
  348. *
  349. * <p> The new buffer's capacity, limit, position, and mark values will be
  350. * identical to those of this buffer.
  351. *
  352. * <p> If this buffer is itself read-only then this method behaves in
  353. * exactly the same way as the {@link #duplicate duplicate} method. </p>
  354. *
  355. * @return The new, read-only byte buffer
  356. */
  357. public abstract ByteBuffer asReadOnlyBuffer();
  358. // -- Singleton get/put methods --
  359. /**
  360. * Relative <i>get</i> method. Reads the byte at this buffer's
  361. * current position, and then increments the position. </p>
  362. *
  363. * @return The byte at the buffer's current position
  364. *
  365. * @throws BufferUnderflowException
  366. * If the buffer's current position is not smaller than its limit
  367. */
  368. public abstract byte get();
  369. /**
  370. * Relative <i>put</i> method  <i>(optional operation)</i>.
  371. *
  372. * <p> Writes the given byte into this buffer at the current
  373. * position, and then increments the position. </p>
  374. *
  375. * @param b
  376. * The byte to be written
  377. *
  378. * @return This buffer
  379. *
  380. * @throws BufferOverflowException
  381. * If this buffer's current position is not smaller than its limit
  382. *
  383. * @throws ReadOnlyBufferException
  384. * If this buffer is read-only
  385. */
  386. public abstract ByteBuffer put(byte b);
  387. /**
  388. * Absolute <i>get</i> method. Reads the byte at the given
  389. * index. </p>
  390. *
  391. * @param index
  392. * The index from which the byte will be read
  393. *
  394. * @return The byte at the given index
  395. *
  396. * @throws IndexOutOfBoundsException
  397. * If <tt>index</tt> is negative
  398. * or not smaller than the buffer's limit
  399. */
  400. public abstract byte get(int index);
  401. /**
  402. * Absolute <i>put</i> method  <i>(optional operation)</i>.
  403. *
  404. * <p> Writes the given byte into this buffer at the given
  405. * index. </p>
  406. *
  407. * @param index
  408. * The index at which the byte will be written
  409. *
  410. * @param b
  411. * The byte value to be written
  412. *
  413. * @return This buffer
  414. *
  415. * @throws IndexOutOfBoundsException
  416. * If <tt>index</tt> is negative
  417. * or not smaller than the buffer's limit
  418. *
  419. * @throws ReadOnlyBufferException
  420. * If this buffer is read-only
  421. */
  422. public abstract ByteBuffer put(int index, byte b);
  423. // -- Bulk get operations --
  424. /**
  425. * Relative bulk <i>get</i> method.
  426. *
  427. * <p> This method transfers bytes from this buffer into the given
  428. * destination array. If there are fewer bytes remaining in the
  429. * buffer than are required to satisfy the request, that is, if
  430. * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no
  431. * bytes are transferred and a {@link BufferUnderflowException} is
  432. * thrown.
  433. *
  434. * <p> Otherwise, this method copies <tt>length</tt> bytes from this
  435. * buffer into the given array, starting at the current position of this
  436. * buffer and at the given offset in the array. The position of this
  437. * buffer is then incremented by <tt>length</tt>.
  438. *
  439. * <p> In other words, an invocation of this method of the form
  440. * <tt>src.get(dst, off, len)</tt> has exactly the same effect as
  441. * the loop
  442. *
  443. * <pre>
  444. * for (int i = off; i < off + len; i++)
  445. * dst[i] = src.get(); </pre>
  446. *
  447. * except that it first checks that there are sufficient bytes in
  448. * this buffer and it is potentially much more efficient. </p>
  449. *
  450. * @param dst
  451. * The array into which bytes are to be written
  452. *
  453. * @param offset
  454. * The offset within the array of the first byte to be
  455. * written; must be non-negative and no larger than
  456. * <tt>dst.length</tt>
  457. *
  458. * @param length
  459. * The maximum number of bytes to be written to the given
  460. * array; must be non-negative and no larger than
  461. * <tt>dst.length - offset</tt>
  462. *
  463. * @return This buffer
  464. *
  465. * @throws BufferUnderflowException
  466. * If there are fewer than <tt>length</tt> bytes
  467. * remaining in this buffer
  468. *
  469. * @throws IndexOutOfBoundsException
  470. * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
  471. * parameters do not hold
  472. */
  473. public ByteBuffer get(byte[] dst, int offset, int length) {
  474. checkBounds(offset, length, dst.length);
  475. if (length > remaining())
  476. throw new BufferUnderflowException();
  477. int end = offset + length;
  478. for (int i = offset; i < end; i++)
  479. dst[i] = get();
  480. return this;
  481. }
  482. /**
  483. * Relative bulk <i>get</i> method.
  484. *
  485. * <p> This method transfers bytes from this buffer into the given
  486. * destination array. An invocation of this method of the form
  487. * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
  488. *
  489. * <pre>
  490. * src.get(a, 0, a.length) </pre>
  491. *
  492. * @return This buffer
  493. *
  494. * @throws BufferUnderflowException
  495. * If there are fewer than <tt>length</tt> bytes
  496. * remaining in this buffer
  497. */
  498. public ByteBuffer get(byte[] dst) {
  499. return get(dst, 0, dst.length);
  500. }
  501. // -- Bulk put operations --
  502. /**
  503. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  504. *
  505. * <p> This method transfers the bytes remaining in the given source
  506. * buffer into this buffer. If there are more bytes remaining in the
  507. * source buffer than in this buffer, that is, if
  508. * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>,
  509. * then no bytes are transferred and a {@link
  510. * BufferOverflowException} is thrown.
  511. *
  512. * <p> Otherwise, this method copies
  513. * <i>n</i> = <tt>src.remaining()</tt> bytes from the given
  514. * buffer into this buffer, starting at each buffer's current position.
  515. * The positions of both buffers are then incremented by <i>n</i>.
  516. *
  517. * <p> In other words, an invocation of this method of the form
  518. * <tt>dst.put(src)</tt> has exactly the same effect as the loop
  519. *
  520. * <pre>
  521. * while (src.hasRemaining())
  522. * dst.put(src.get()); </pre>
  523. *
  524. * except that it first checks that there is sufficient space in this
  525. * buffer and it is potentially much more efficient. </p>
  526. *
  527. * @param src
  528. * The source buffer from which bytes are to be read;
  529. * must not be this buffer
  530. *
  531. * @return This buffer
  532. *
  533. * @throws BufferOverflowException
  534. * If there is insufficient space in this buffer
  535. * for the remaining bytes in the source buffer
  536. *
  537. * @throws IllegalArgumentException
  538. * If the source buffer is this buffer
  539. *
  540. * @throws ReadOnlyBufferException
  541. * If this buffer is read-only
  542. */
  543. public ByteBuffer put(ByteBuffer src) {
  544. if (src == this)
  545. throw new IllegalArgumentException();
  546. int n = src.remaining();
  547. if (n > remaining())
  548. throw new BufferOverflowException();
  549. for (int i = 0; i < n; i++)
  550. put(src.get());
  551. return this;
  552. }
  553. /**
  554. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  555. *
  556. * <p> This method transfers bytes into this buffer from the given
  557. * source array. If there are more bytes to be copied from the array
  558. * than remain in this buffer, that is, if
  559. * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no
  560. * bytes are transferred and a {@link BufferOverflowException} is
  561. * thrown.
  562. *
  563. * <p> Otherwise, this method copies <tt>length</tt> bytes from the
  564. * given array into this buffer, starting at the given offset in the array
  565. * and at the current position of this buffer. The position of this buffer
  566. * is then incremented by <tt>length</tt>.
  567. *
  568. * <p> In other words, an invocation of this method of the form
  569. * <tt>dst.put(src, off, len)</tt> has exactly the same effect as
  570. * the loop
  571. *
  572. * <pre>
  573. * for (int i = off; i < off + len; i++)
  574. * dst.put(a[i]); </pre>
  575. *
  576. * except that it first checks that there is sufficient space in this
  577. * buffer and it is potentially much more efficient. </p>
  578. *
  579. * @param src
  580. * The array from which bytes are to be read
  581. *
  582. * @param offset
  583. * The offset within the array of the first byte to be read;
  584. * must be non-negative and no larger than <tt>array.length</tt>
  585. *
  586. * @param length
  587. * The number of bytes to be read from the given array;
  588. * must be non-negative and no larger than
  589. * <tt>array.length - offset</tt>
  590. *
  591. * @return This buffer
  592. *
  593. * @throws BufferOverflowException
  594. * If there is insufficient space in this buffer
  595. *
  596. * @throws IndexOutOfBoundsException
  597. * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
  598. * parameters do not hold
  599. *
  600. * @throws ReadOnlyBufferException
  601. * If this buffer is read-only
  602. */
  603. public ByteBuffer put(byte[] src, int offset, int length) {
  604. checkBounds(offset, length, src.length);
  605. if (length > remaining())
  606. throw new BufferOverflowException();
  607. int end = offset + length;
  608. for (int i = offset; i < end; i++)
  609. this.put(src[i]);
  610. return this;
  611. }
  612. /**
  613. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  614. *
  615. * <p> This method transfers the entire content of the given source
  616. * byte array into this buffer. An invocation of this method of the
  617. * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
  618. * invocation
  619. *
  620. * <pre>
  621. * dst.put(a, 0, a.length) </pre>
  622. *
  623. * @return This buffer
  624. *
  625. * @throws BufferOverflowException
  626. * If there is insufficient space in this buffer
  627. *
  628. * @throws ReadOnlyBufferException
  629. * If this buffer is read-only
  630. */
  631. public final ByteBuffer put(byte[] src) {
  632. return put(src, 0, src.length);
  633. }
  634. // -- Other stuff --
  635. /**
  636. * Tells whether or not this buffer is backed by an accessible byte
  637. * array.
  638. *
  639. * <p> If this method returns <tt>true</tt> then the {@link #array() array}
  640. * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
  641. * </p>
  642. *
  643. * @return <tt>true</tt> if, and only if, this buffer
  644. * is backed by an array and is not read-only
  645. */
  646. public final boolean hasArray() {
  647. return (hb != null) && !isReadOnly;
  648. }
  649. /**
  650. * Returns the byte array that backs this
  651. * buffer  <i>(optional operation)</i>.
  652. *
  653. * <p> Modifications to this buffer's content will cause the returned
  654. * array's content to be modified, and vice versa.
  655. *
  656. * <p> Invoke the {@link #hasArray hasArray} method before invoking this
  657. * method in order to ensure that this buffer has an accessible backing
  658. * array. </p>
  659. *
  660. * @return The array that backs this buffer
  661. *
  662. * @throws ReadOnlyBufferException
  663. * If this buffer is backed by an array but is read-only
  664. *
  665. * @throws UnsupportedOperationException
  666. * If this buffer is not backed by an accessible array
  667. */
  668. public final byte[] array() {
  669. if (hb == null)
  670. throw new UnsupportedOperationException();
  671. if (isReadOnly)
  672. throw new ReadOnlyBufferException();
  673. return hb;
  674. }
  675. /**
  676. * Returns the offset within this buffer's backing array of the first
  677. * element of the buffer  <i>(optional operation)</i>.
  678. *
  679. * <p> If this buffer is backed by an array then buffer position <i>p</i>
  680. * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>.
  681. *
  682. * <p> Invoke the {@link #hasArray hasArray} method before invoking this
  683. * method in order to ensure that this buffer has an accessible backing
  684. * array. </p>
  685. *
  686. * @return The offset within this buffer's array
  687. * of the first element of the buffer
  688. *
  689. * @throws ReadOnlyBufferException
  690. * If this buffer is backed by an array but is read-only
  691. *
  692. * @throws UnsupportedOperationException
  693. * If this buffer is not backed by an accessible array
  694. */
  695. public final int arrayOffset() {
  696. if (hb == null)
  697. throw new UnsupportedOperationException();
  698. if (isReadOnly)
  699. throw new ReadOnlyBufferException();
  700. return offset;
  701. }
  702. /**
  703. * Compacts this buffer  <i>(optional operation)</i>.
  704. *
  705. * <p> The bytes between the buffer's current position and its limit,
  706. * if any, are copied to the beginning of the buffer. That is, the
  707. * byte at index <i>p</i> = <tt>position()</tt> is copied
  708. * to index zero, the byte at index <i>p</i> + 1 is copied
  709. * to index one, and so forth until the byte at index
  710. * <tt>limit()</tt> - 1 is copied to index
  711. * <i>n</i> = <tt>limit()</tt> - <tt>1</tt> - <i>p</i>.
  712. * The buffer's position is then set to <i>n+1</i> and its limit is set to
  713. * its capacity. The mark, if defined, is discarded.
  714. *
  715. * <p> The buffer's position is set to the number of bytes copied,
  716. * rather than to zero, so that an invocation of this method can be
  717. * followed immediately by an invocation of another relative <i>put</i>
  718. * method. </p>
  719. *
  720. *
  721. * <p> Invoke this method after writing data from a buffer in case the
  722. * write was incomplete. The following loop, for example, copies bytes
  723. * from one channel to another via the buffer <tt>buf</tt>:
  724. *
  725. * <blockquote><pre>
  726. * buf.clear(); // Prepare buffer for use
  727. * for (;;) {
  728. * if (in.read(buf) < 0 && !buf.hasRemaining())
  729. * break; // No more bytes to transfer
  730. * buf.flip();
  731. * out.write(buf);
  732. * buf.compact(); // In case of partial write
  733. * }</pre></blockquote>
  734. *
  735. *
  736. * @return This buffer
  737. *
  738. * @throws ReadOnlyBufferException
  739. * If this buffer is read-only
  740. */
  741. public abstract ByteBuffer compact();
  742. /**
  743. * Tells whether or not this byte buffer is direct. </p>
  744. *
  745. * @return <tt>true</tt> if, and only if, this buffer is direct
  746. */
  747. public abstract boolean isDirect();
  748. /**
  749. * Returns a string summarizing the state of this buffer. </p>
  750. *
  751. * @return A summary string
  752. */
  753. public String toString() {
  754. StringBuffer sb = new StringBuffer();
  755. sb.append(getClass().getName());
  756. sb.append("[pos=");
  757. sb.append(position());
  758. sb.append(" lim=");
  759. sb.append(limit());
  760. sb.append(" cap=");
  761. sb.append(capacity());
  762. sb.append("]");
  763. return sb.toString();
  764. }
  765. /**
  766. * Returns the current hash code of this buffer.
  767. *
  768. * <p> The hash code of a byte buffer depends only upon its remaining
  769. * elements; that is, upon the elements from <tt>position()</tt> up to, and
  770. * including, the element at <tt>limit()</tt> - <tt>1</tt>.
  771. *
  772. * <p> Because buffer hash codes are content-dependent, it is inadvisable
  773. * to use buffers as keys in hash maps or similar data structures unless it
  774. * is known that their contents will not change. </p>
  775. *
  776. * @return The current hash code of this buffer
  777. */
  778. public int hashCode() {
  779. int h = 1;
  780. int p = position();
  781. for (int i = limit() - 1; i >= p; i--)
  782. h = 31 * h + (int)get(i);
  783. return h;
  784. }
  785. /**
  786. * Tells whether or not this buffer is equal to another object.
  787. *
  788. * <p> Two byte buffers are equal if, and only if,
  789. *
  790. * <p><ol>
  791. *
  792. * <li><p> They have the same element type, </p></li>
  793. *
  794. * <li><p> They have the same number of remaining elements, and
  795. * </p></li>
  796. *
  797. * <li><p> The two sequences of remaining elements, considered
  798. * independently of their starting positions, are pointwise equal.
  799. * </p></li>
  800. *
  801. * </ol>
  802. *
  803. * <p> A byte buffer is not equal to any other type of object. </p>
  804. *
  805. * @param ob The object to which this buffer is to be compared
  806. *
  807. * @return <tt>true</tt> if, and only if, this buffer is equal to the
  808. * given object
  809. */
  810. public boolean equals(Object ob) {
  811. if (!(ob instanceof ByteBuffer))
  812. return false;
  813. ByteBuffer that = (ByteBuffer)ob;
  814. if (this.remaining() != that.remaining())
  815. return false;
  816. int p = this.position();
  817. for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
  818. byte v1 = this.get(i);
  819. byte v2 = that.get(j);
  820. if (v1 != v2) {
  821. if ((v1 != v1) && (v2 != v2)) // For float and double
  822. continue;
  823. return false;
  824. }
  825. }
  826. return true;
  827. }
  828. /**
  829. * Compares this buffer to another object.
  830. *
  831. * <p> Two byte buffers are compared by comparing their sequences of
  832. * remaining elements lexicographically, without regard to the starting
  833. * position of each sequence within its corresponding buffer.
  834. *
  835. * <p> A byte buffer is not comparable to any other type of object. </p>
  836. *
  837. * @return A negative integer, zero, or a positive integer as this buffer
  838. * is less than, equal to, or greater than the given buffer
  839. *
  840. * @throws ClassCastException
  841. * If the argument is not a byte buffer
  842. */
  843. public int compareTo(Object ob) {
  844. ByteBuffer that = (ByteBuffer)ob;
  845. int n = this.position() + Math.min(this.remaining(), that.remaining());
  846. for (int i = this.position(), j = that.position(); i < n; i++, j++) {
  847. byte v1 = this.get(i);
  848. byte v2 = that.get(j);
  849. if (v1 == v2)
  850. continue;
  851. if ((v1 != v1) && (v2 != v2)) // For float and double
  852. continue;
  853. if (v1 < v2)
  854. return -1;
  855. return +1;
  856. }
  857. return this.remaining() - that.remaining();
  858. }
  859. // -- Other char stuff --
  860. // -- Other byte stuff: Access to binary data --
  861. boolean bigEndian // package-private
  862. = true;
  863. boolean nativeByteOrder // package-private
  864. = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN);
  865. /**
  866. * Retrieves this buffer's byte order.
  867. *
  868. * <p> The byte order is used when reading or writing multibyte values, and
  869. * when creating buffers that are views of this byte buffer. The order of
  870. * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN
  871. * BIG_ENDIAN}. </p>
  872. *
  873. * @return This buffer's byte order
  874. */
  875. public final ByteOrder order() {
  876. return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
  877. }
  878. /**
  879. * Modifies this buffer's byte order. </p>
  880. *
  881. * @param bo
  882. * The new byte order,
  883. * either {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
  884. * or {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}
  885. *
  886. * @return This buffer
  887. */
  888. public final ByteBuffer order(ByteOrder bo) {
  889. bigEndian = (bo == ByteOrder.BIG_ENDIAN);
  890. nativeByteOrder =
  891. (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
  892. return this;
  893. }
  894. // Unchecked accessors, for use by ByteBufferAs-X-Buffer classes
  895. //
  896. abstract byte _get(int i); // package-private
  897. abstract void _put(int i, byte b); // package-private
  898. /**
  899. * Relative <i>get</i> method for reading a char value.
  900. *
  901. * <p> Reads the next two bytes at this buffer's current position,
  902. * composing them into a char value according to the current byte order,
  903. * and then increments the position by two. </p>
  904. *
  905. * @return The char value at the buffer's current position
  906. *
  907. * @throws BufferUnderflowException
  908. * If there are fewer than two bytes
  909. * remaining in this buffer
  910. */
  911. public abstract char getChar();
  912. /**
  913. * Relative <i>put</i> method for writing a char
  914. * value  <i>(optional operation)</i>.
  915. *
  916. * <p> Writes two bytes containing the given char value, in the
  917. * current byte order, into this buffer at the current position, and then
  918. * increments the position by two. </p>
  919. *
  920. * @param value
  921. * The char value to be written
  922. *
  923. * @return This buffer
  924. *
  925. * @throws BufferOverflowException
  926. * If there are fewer than two bytes
  927. * remaining in this buffer
  928. *
  929. * @throws ReadOnlyBufferException
  930. * If this buffer is read-only
  931. */
  932. public abstract ByteBuffer putChar(char value);
  933. /**
  934. * Absolute <i>get</i> method for reading a char value.
  935. *
  936. * <p> Reads two bytes at the given index, composing them into a
  937. * char value according to the current byte order. </p>
  938. *
  939. * @param index
  940. * The index from which the bytes will be read
  941. *
  942. * @return The char value at the given index
  943. *
  944. * @throws IndexOutOfBoundsException
  945. * If <tt>index</tt> is negative
  946. * or not smaller than the buffer's limit,
  947. * minus one
  948. */
  949. public abstract char getChar(int index);
  950. /**
  951. * Absolute <i>put</i> method for writing a char
  952. * value  <i>(optional operation)</i>.
  953. *
  954. * <p> Writes two bytes containing the given char value, in the
  955. * current byte order, into this buffer at the given index. </p>
  956. *
  957. * @param index
  958. * The index at which the bytes will be written
  959. *
  960. * @param value
  961. * The char value to be written
  962. *
  963. * @return This buffer
  964. *
  965. * @throws IndexOutOfBoundsException
  966. * If <tt>index</tt> is negative
  967. * or not smaller than the buffer's limit,
  968. * minus one
  969. *
  970. * @throws ReadOnlyBufferException
  971. * If this buffer is read-only
  972. */
  973. public abstract ByteBuffer putChar(int index, char value);
  974. /**
  975. * Creates a view of this byte buffer as a char buffer.
  976. *
  977. * <p> The content of the new buffer will start at this buffer's current
  978. * position. Changes to this buffer's content will be visible in the new
  979. * buffer, and vice versa; the two buffers' position, limit, and mark
  980. * values will be independent.
  981. *
  982. * <p> The new buffer's position will be zero, its capacity and its limit
  983. * will be the number of bytes remaining in this buffer divided by
  984. * two, and its mark will be undefined. The new buffer will be direct
  985. * if, and only if, this buffer is direct, and it will be read-only if, and
  986. * only if, this buffer is read-only. </p>
  987. *
  988. * @return A new char buffer
  989. */
  990. public abstract CharBuffer asCharBuffer();
  991. /**
  992. * Relative <i>get</i> method for reading a short value.
  993. *
  994. * <p> Reads the next two bytes at this buffer's current position,
  995. * composing them into a short value according to the current byte order,
  996. * and then increments the position by two. </p>
  997. *
  998. * @return The short value at the buffer's current position
  999. *
  1000. * @throws BufferUnderflowException
  1001. * If there are fewer than two bytes
  1002. * remaining in this buffer
  1003. */
  1004. public abstract short getShort();
  1005. /**
  1006. * Relative <i>put</i> method for writing a short
  1007. * value  <i>(optional operation)</i>.
  1008. *
  1009. * <p> Writes two bytes containing the given short value, in the
  1010. * current byte order, into this buffer at the current position, and then
  1011. * increments the position by two. </p>
  1012. *
  1013. * @param value
  1014. * The short value to be written
  1015. *
  1016. * @return This buffer
  1017. *
  1018. * @throws BufferOverflowException
  1019. * If there are fewer than two bytes
  1020. * remaining in this buffer
  1021. *
  1022. * @throws ReadOnlyBufferException
  1023. * If this buffer is read-only
  1024. */
  1025. public abstract ByteBuffer putShort(short value);
  1026. /**
  1027. * Absolute <i>get</i> method for reading a short value.
  1028. *
  1029. * <p> Reads two bytes at the given index, composing them into a
  1030. * short value according to the current byte order. </p>
  1031. *
  1032. * @param index
  1033. * The index from which the bytes will be read
  1034. *
  1035. * @return The short value at the given index
  1036. *
  1037. * @throws IndexOutOfBoundsException
  1038. * If <tt>index</tt> is negative
  1039. * or not smaller than the buffer's limit,
  1040. * minus one
  1041. */
  1042. public abstract short getShort(int index);
  1043. /**
  1044. * Absolute <i>put</i> method for writing a short
  1045. * value  <i>(optional operation)</i>.
  1046. *
  1047. * <p> Writes two bytes containing the given short value, in the
  1048. * current byte order, into this buffer at the given index. </p>
  1049. *
  1050. * @param index
  1051. * The index at which the bytes will be written
  1052. *
  1053. * @param value
  1054. * The short value to be written
  1055. *
  1056. * @return This buffer
  1057. *
  1058. * @throws IndexOutOfBoundsException
  1059. * If <tt>index</tt> is negative
  1060. * or not smaller than the buffer's limit,
  1061. * minus one
  1062. *
  1063. * @throws ReadOnlyBufferException
  1064. * If this buffer is read-only
  1065. */
  1066. public abstract ByteBuffer putShort(int index, short value);
  1067. /**
  1068. * Creates a view of this byte buffer as a short buffer.
  1069. *
  1070. * <p> The content of the new buffer will start at this buffer's current
  1071. * position. Changes to this buffer's content will be visible in the new
  1072. * buffer, and vice versa; the two buffers' position, limit, and mark
  1073. * values will be independent.
  1074. *
  1075. * <p> The new buffer's position will be zero, its capacity and its limit
  1076. * will be the number of bytes remaining in this buffer divided by
  1077. * two, and its mark will be undefined. The new buffer will be direct
  1078. * if, and only if, this buffer is direct, and it will be read-only if, and
  1079. * only if, this buffer is read-only. </p>
  1080. *
  1081. * @return A new short buffer
  1082. */
  1083. public abstract ShortBuffer asShortBuffer();
  1084. /**
  1085. * Relative <i>get</i> method for reading an int value.
  1086. *
  1087. * <p> Reads the next four bytes at this buffer's current position,
  1088. * composing them into an int value according to the current byte order,
  1089. * and then increments the position by four. </p>
  1090. *
  1091. * @return The int value at the buffer's current position
  1092. *
  1093. * @throws BufferUnderflowException
  1094. * If there are fewer than four bytes
  1095. * remaining in this buffer
  1096. */
  1097. public abstract int getInt();
  1098. /**
  1099. * Relative <i>put</i> method for writing an int
  1100. * value  <i>(optional operation)</i>.
  1101. *
  1102. * <p> Writes four bytes containing the given int value, in the
  1103. * current byte order, into this buffer at the current position, and then
  1104. * increments the position by four. </p>
  1105. *
  1106. * @param value
  1107. * The int value to be written
  1108. *
  1109. * @return This buffer
  1110. *
  1111. * @throws BufferOverflowException
  1112. * If there are fewer than four bytes
  1113. * remaining in this buffer
  1114. *
  1115. * @throws ReadOnlyBufferException
  1116. * If this buffer is read-only
  1117. */
  1118. public abstract ByteBuffer putInt(int value);
  1119. /**
  1120. * Absolute <i>get</i> method for reading an int value.
  1121. *
  1122. * <p> Reads four bytes at the given index, composing them into a
  1123. * int value according to the current byte order. </p>
  1124. *
  1125. * @param index
  1126. * The index from which the bytes will be read
  1127. *
  1128. * @return The int value at the given index
  1129. *
  1130. * @throws IndexOutOfBoundsException
  1131. * If <tt>index</tt> is negative
  1132. * or not smaller than the buffer's limit,
  1133. * minus three
  1134. */
  1135. public abstract int getInt(int index);
  1136. /**
  1137. * Absolute <i>put</i> method for writing an int
  1138. * value  <i>(optional operation)</i>.
  1139. *
  1140. * <p> Writes four bytes containing the given int value, in the
  1141. * current byte order, into this buffer at the given index. </p>
  1142. *
  1143. * @param index
  1144. * The index at which the bytes will be written
  1145. *
  1146. * @param value
  1147. * The int value to be written
  1148. *
  1149. * @return This buffer
  1150. *
  1151. * @throws IndexOutOfBoundsException
  1152. * If <tt>index</tt> is negative
  1153. * or not smaller than the buffer's limit,
  1154. * minus three
  1155. *
  1156. * @throws ReadOnlyBufferException
  1157. * If this buffer is read-only
  1158. */
  1159. public abstract ByteBuffer putInt(int index, int value);
  1160. /**
  1161. * Creates a view of this byte buffer as an int buffer.
  1162. *
  1163. * <p> The content of the new buffer will start at this buffer's current
  1164. * position. Changes to this buffer's content will be visible in the new
  1165. * buffer, and vice versa; the two buffers' position, limit, and mark
  1166. * values will be independent.
  1167. *
  1168. * <p> The new buffer's position will be zero, its capacity and its limit
  1169. * will be the number of bytes remaining in this buffer divided by
  1170. * four, and its mark will be undefined. The new buffer will be direct
  1171. * if, and only if, this buffer is direct, and it will be read-only if, and
  1172. * only if, this buffer is read-only. </p>
  1173. *
  1174. * @return A new int buffer
  1175. */
  1176. public abstract IntBuffer asIntBuffer();
  1177. /**
  1178. * Relative <i>get</i> method for reading a long value.
  1179. *
  1180. * <p> Reads the next eight bytes at this buffer's current position,
  1181. * composing them into a long value according to the current byte order,
  1182. * and then increments the position by eight. </p>
  1183. *
  1184. * @return The long value at the buffer's current position
  1185. *
  1186. * @throws BufferUnderflowException
  1187. * If there are fewer than eight bytes
  1188. * remaining in this buffer
  1189. */
  1190. public abstract long getLong();
  1191. /**
  1192. * Relative <i>put</i> method for writing a long
  1193. * value  <i>(optional operation)</i>.
  1194. *
  1195. * <p> Writes eight bytes containing the given long value, in the
  1196. * current byte order, into this buffer at the current position, and then
  1197. * increments the position by eight. </p>
  1198. *
  1199. * @param value
  1200. * The long value to be written
  1201. *
  1202. * @return This buffer
  1203. *
  1204. * @throws BufferOverflowException
  1205. * If there are fewer than eight bytes
  1206. * remaining in this buffer
  1207. *
  1208. * @throws ReadOnlyBufferException
  1209. * If this buffer is read-only
  1210. */
  1211. public abstract ByteBuffer putLong(long value);
  1212. /**
  1213. * Absolute <i>get</i> method for reading a long value.
  1214. *
  1215. * <p> Reads eight bytes at the given index, composing them into a
  1216. * long value according to the current byte order. </p>
  1217. *
  1218. * @param index
  1219. * The index from which the bytes will be read
  1220. *
  1221. * @return The long value at the given index
  1222. *
  1223. * @throws IndexOutOfBoundsException
  1224. * If <tt>index</tt> is negative
  1225. * or not smaller than the buffer's limit,
  1226. * minus seven
  1227. */
  1228. public abstract long getLong(int index);
  1229. /**
  1230. * Absolute <i>put</i> method for writing a long
  1231. * value  <i>(optional operation)</i>.
  1232. *
  1233. * <p> Writes eight bytes containing the given long value, in the
  1234. * current byte order, into this buffer at the given index. </p>
  1235. *
  1236. * @param index
  1237. * The index at which the bytes will be written
  1238. *
  1239. * @param value
  1240. * The long value to be written
  1241. *
  1242. * @return This buffer
  1243. *
  1244. * @throws IndexOutOfBoundsException
  1245. * If <tt>index</tt> is negative
  1246. * or not smaller than the buffer's limit,
  1247. * minus seven
  1248. *
  1249. * @throws ReadOnlyBufferException
  1250. * If this buffer is read-only
  1251. */
  1252. public abstract ByteBuffer putLong(int index, long value);
  1253. /**
  1254. * Creates a view of this byte buffer as a long buffer.
  1255. *
  1256. * <p> The content of the new buffer will start at this buffer's current
  1257. * position. Changes to this buffer's content will be visible in the new
  1258. * buffer, and vice versa; the two buffers' position, limit, and mark
  1259. * values will be independent.
  1260. *
  1261. * <p> The new buffer's position will be zero, its capacity and its limit
  1262. * will be the number of bytes remaining in this buffer divided by
  1263. * eight, and its mark will be undefined. The new buffer will be direct
  1264. * if, and only if, this buffer is direct, and it will be read-only if, and
  1265. * only if, this buffer is read-only. </p>
  1266. *
  1267. * @return A new long buffer
  1268. */
  1269. public abstract LongBuffer asLongBuffer();
  1270. /**
  1271. * Relative <i>get</i> method for reading a float value.
  1272. *
  1273. * <p> Reads the next four bytes at this buffer's current position,
  1274. * composing them into a float value according to the current byte order,
  1275. * and then increments the position by four. </p>
  1276. *
  1277. * @return The float value at the buffer's current position
  1278. *
  1279. * @throws BufferUnderflowException
  1280. * If there are fewer than four bytes
  1281. * remaining in this buffer
  1282. */
  1283. public abstract float getFloat();
  1284. /**
  1285. * Relative <i>put</i> method for writing a float
  1286. * value  <i>(optional operation)</i>.
  1287. *
  1288. * <p> Writes four bytes containing the given float value, in the
  1289. * current byte order, into this buffer at the current position, and then
  1290. * increments the position by four. </p>
  1291. *
  1292. * @param value
  1293. * The float value to be written
  1294. *
  1295. * @return This buffer
  1296. *
  1297. * @throws BufferOverflowException
  1298. * If there are fewer than four bytes
  1299. * remaining in this buffer
  1300. *
  1301. * @throws ReadOnlyBufferException
  1302. * If this buffer is read-only
  1303. */
  1304. public abstract ByteBuffer putFloat(float value);
  1305. /**
  1306. * Absolute <i>get</i> method for reading a float value.
  1307. *
  1308. * <p> Reads four bytes at the given index, composing them into a
  1309. * float value according to the current byte order. </p>
  1310. *
  1311. * @param index
  1312. * The index from which the bytes will be read
  1313. *
  1314. * @return The float value at the given index
  1315. *
  1316. * @throws IndexOutOfBoundsException
  1317. * If <tt>index</tt> is negative
  1318. * or not smaller than the buffer's limit,
  1319. * minus three
  1320. */
  1321. public abstract float getFloat(int index);
  1322. /**
  1323. * Absolute <i>put</i> method for writing a float
  1324. * value  <i>(optional operation)</i>.
  1325. *
  1326. * <p> Writes four bytes containing the given float value, in the
  1327. * current byte order, into this buffer at the given index. </p>
  1328. *
  1329. * @param index
  1330. * The index at which the bytes will be written
  1331. *
  1332. * @param value
  1333. * The float value to be written
  1334. *
  1335. * @return This buffer
  1336. *
  1337. * @throws IndexOutOfBoundsException
  1338. * If <tt>index</tt> is negative
  1339. * or not smaller than the buffer's limit,
  1340. * minus three
  1341. *
  1342. * @throws ReadOnlyBufferException
  1343. * If this buffer is read-only
  1344. */
  1345. public abstract ByteBuffer putFloat(int index, float value);
  1346. /**
  1347. * Creates a view of this byte buffer as a float buffer.
  1348. *
  1349. * <p> The content of the new buffer will start at this buffer's current
  1350. * position. Changes to this buffer's content will be visible in the new
  1351. * buffer, and vice versa; the two buffers' position, limit, and mark
  1352. * values will be independent.
  1353. *
  1354. * <p> The new buffer's position will be zero, its capacity and its limit
  1355. * will be the number of bytes remaining in this buffer divided by
  1356. * four, and its mark will be undefined. The new buffer will be direct
  1357. * if, and only if, this buffer is direct, and it will be read-only if, and
  1358. * only if, this buffer is read-only. </p>
  1359. *
  1360. * @return A new float buffer
  1361. */
  1362. public abstract FloatBuffer asFloatBuffer();
  1363. /**
  1364. * Relative <i>get</i> method for reading a double value.
  1365. *
  1366. * <p> Reads the next eight bytes at this buffer's current position,
  1367. * composing them into a double value according to the current byte order,
  1368. * and then increments the position by eight. </p>
  1369. *
  1370. * @return The double value at the buffer's current position
  1371. *
  1372. * @throws BufferUnderflowException
  1373. * If there are fewer than eight bytes
  1374. * remaining in this buffer
  1375. */
  1376. public abstract double getDouble();
  1377. /**
  1378. * Relative <i>put</i> method for writing a double
  1379. * value  <i>(optional operation)</i>.
  1380. *
  1381. * <p> Writes eight bytes containing the given double value, in the
  1382. * current byte order, into this buffer at the current position, and then
  1383. * increments the position by eight. </p>
  1384. *
  1385. * @param value
  1386. * The double value to be written
  1387. *
  1388. * @return This buffer
  1389. *
  1390. * @throws BufferOverflowException
  1391. * If there are fewer than eight bytes
  1392. * remaining in this buffer
  1393. *
  1394. * @throws ReadOnlyBufferException
  1395. * If this buffer is read-only
  1396. */
  1397. public abstract ByteBuffer putDouble(double value);
  1398. /**
  1399. * Absolute <i>get</i> method for reading a double value.
  1400. *
  1401. * <p> Reads eight bytes at the given index, composing them into a
  1402. * double value according to the current byte order. </p>
  1403. *
  1404. * @param index
  1405. * The index from which the bytes will be read
  1406. *
  1407. * @return The double value at the given index
  1408. *
  1409. * @throws IndexOutOfBoundsException
  1410. * If <tt>index</tt> is negative
  1411. * or not smaller than the buffer's limit,
  1412. * minus seven
  1413. */
  1414. public abstract double getDouble(int index);
  1415. /**
  1416. * Absolute <i>put</i> method for writing a double
  1417. * value  <i>(optional operation)</i>.
  1418. *
  1419. * <p> Writes eight bytes containing the given double value, in the
  1420. * current byte order, into this buffer at the given index. </p>
  1421. *
  1422. * @param index
  1423. * The index at which the bytes will be written
  1424. *
  1425. * @param value
  1426. * The double value to be written
  1427. *
  1428. * @return This buffer
  1429. *
  1430. * @throws IndexOutOfBoundsException
  1431. * If <tt>index</tt> is negative
  1432. * or not smaller than the buffer's limit,
  1433. * minus seven
  1434. *
  1435. * @throws ReadOnlyBufferException
  1436. * If this buffer is read-only
  1437. */
  1438. public abstract ByteBuffer putDouble(int index, double value);
  1439. /**
  1440. * Creates a view of this byte buffer as a double buffer.
  1441. *
  1442. * <p> The content of the new buffer will start at this buffer's current
  1443. * position. Changes to this buffer's content will be visible in the new
  1444. * buffer, and vice versa; the two buffers' position, limit, and mark
  1445. * values will be independent.
  1446. *
  1447. * <p> The new buffer's position will be zero, its capacity and its limit
  1448. * will be the number of bytes remaining in this buffer divided by
  1449. * eight, and its mark will be undefined. The new buffer will be direct
  1450. * if, and only if, this buffer is direct, and it will be read-only if, and
  1451. * only if, this buffer is read-only. </p>
  1452. *
  1453. * @return A new double buffer
  1454. */
  1455. public abstract DoubleBuffer asDoubleBuffer();
  1456. }