1. /*
  2. * @(#)X-Buffer.java 1.56 04/07/16
  3. *
  4. * Copyright 2004 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 heterogeneous 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.56, 04/07/16
  179. * @since 1.4
  180. */
  181. public abstract class ByteBuffer
  182. extends Buffer
  183. implements Comparable<ByteBuffer>
  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 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 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.
  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.
  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. public int compareTo(ByteBuffer that) {
  841. int n = this.position() + Math.min(this.remaining(), that.remaining());
  842. for (int i = this.position(), j = that.position(); i < n; i++, j++) {
  843. byte v1 = this.get(i);
  844. byte v2 = that.get(j);
  845. if (v1 == v2)
  846. continue;
  847. if ((v1 != v1) && (v2 != v2)) // For float and double
  848. continue;
  849. if (v1 < v2)
  850. return -1;
  851. return +1;
  852. }
  853. return this.remaining() - that.remaining();
  854. }
  855. // -- Other char stuff --
  856. // -- Other byte stuff: Access to binary data --
  857. boolean bigEndian // package-private
  858. = true;
  859. boolean nativeByteOrder // package-private
  860. = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN);
  861. /**
  862. * Retrieves this buffer's byte order.
  863. *
  864. * <p> The byte order is used when reading or writing multibyte values, and
  865. * when creating buffers that are views of this byte buffer. The order of
  866. * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN
  867. * BIG_ENDIAN}. </p>
  868. *
  869. * @return This buffer's byte order
  870. */
  871. public final ByteOrder order() {
  872. return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
  873. }
  874. /**
  875. * Modifies this buffer's byte order. </p>
  876. *
  877. * @param bo
  878. * The new byte order,
  879. * either {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
  880. * or {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}
  881. *
  882. * @return This buffer
  883. */
  884. public final ByteBuffer order(ByteOrder bo) {
  885. bigEndian = (bo == ByteOrder.BIG_ENDIAN);
  886. nativeByteOrder =
  887. (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
  888. return this;
  889. }
  890. // Unchecked accessors, for use by ByteBufferAs-X-Buffer classes
  891. //
  892. abstract byte _get(int i); // package-private
  893. abstract void _put(int i, byte b); // package-private
  894. /**
  895. * Relative <i>get</i> method for reading a char value.
  896. *
  897. * <p> Reads the next two bytes at this buffer's current position,
  898. * composing them into a char value according to the current byte order,
  899. * and then increments the position by two. </p>
  900. *
  901. * @return The char value at the buffer's current position
  902. *
  903. * @throws BufferUnderflowException
  904. * If there are fewer than two bytes
  905. * remaining in this buffer
  906. */
  907. public abstract char getChar();
  908. /**
  909. * Relative <i>put</i> method for writing a char
  910. * value  <i>(optional operation)</i>.
  911. *
  912. * <p> Writes two bytes containing the given char value, in the
  913. * current byte order, into this buffer at the current position, and then
  914. * increments the position by two. </p>
  915. *
  916. * @param value
  917. * The char value to be written
  918. *
  919. * @return This buffer
  920. *
  921. * @throws BufferOverflowException
  922. * If there are fewer than two bytes
  923. * remaining in this buffer
  924. *
  925. * @throws ReadOnlyBufferException
  926. * If this buffer is read-only
  927. */
  928. public abstract ByteBuffer putChar(char value);
  929. /**
  930. * Absolute <i>get</i> method for reading a char value.
  931. *
  932. * <p> Reads two bytes at the given index, composing them into a
  933. * char value according to the current byte order. </p>
  934. *
  935. * @param index
  936. * The index from which the bytes will be read
  937. *
  938. * @return The char value at the given index
  939. *
  940. * @throws IndexOutOfBoundsException
  941. * If <tt>index</tt> is negative
  942. * or not smaller than the buffer's limit,
  943. * minus one
  944. */
  945. public abstract char getChar(int index);
  946. /**
  947. * Absolute <i>put</i> method for writing a char
  948. * value  <i>(optional operation)</i>.
  949. *
  950. * <p> Writes two bytes containing the given char value, in the
  951. * current byte order, into this buffer at the given index. </p>
  952. *
  953. * @param index
  954. * The index at which the bytes will be written
  955. *
  956. * @param value
  957. * The char value to be written
  958. *
  959. * @return This buffer
  960. *
  961. * @throws IndexOutOfBoundsException
  962. * If <tt>index</tt> is negative
  963. * or not smaller than the buffer's limit,
  964. * minus one
  965. *
  966. * @throws ReadOnlyBufferException
  967. * If this buffer is read-only
  968. */
  969. public abstract ByteBuffer putChar(int index, char value);
  970. /**
  971. * Creates a view of this byte buffer as a char buffer.
  972. *
  973. * <p> The content of the new buffer will start at this buffer's current
  974. * position. Changes to this buffer's content will be visible in the new
  975. * buffer, and vice versa; the two buffers' position, limit, and mark
  976. * values will be independent.
  977. *
  978. * <p> The new buffer's position will be zero, its capacity and its limit
  979. * will be the number of bytes remaining in this buffer divided by
  980. * two, and its mark will be undefined. The new buffer will be direct
  981. * if, and only if, this buffer is direct, and it will be read-only if, and
  982. * only if, this buffer is read-only. </p>
  983. *
  984. * @return A new char buffer
  985. */
  986. public abstract CharBuffer asCharBuffer();
  987. /**
  988. * Relative <i>get</i> method for reading a short value.
  989. *
  990. * <p> Reads the next two bytes at this buffer's current position,
  991. * composing them into a short value according to the current byte order,
  992. * and then increments the position by two. </p>
  993. *
  994. * @return The short value at the buffer's current position
  995. *
  996. * @throws BufferUnderflowException
  997. * If there are fewer than two bytes
  998. * remaining in this buffer
  999. */
  1000. public abstract short getShort();
  1001. /**
  1002. * Relative <i>put</i> method for writing a short
  1003. * value  <i>(optional operation)</i>.
  1004. *
  1005. * <p> Writes two bytes containing the given short value, in the
  1006. * current byte order, into this buffer at the current position, and then
  1007. * increments the position by two. </p>
  1008. *
  1009. * @param value
  1010. * The short value to be written
  1011. *
  1012. * @return This buffer
  1013. *
  1014. * @throws BufferOverflowException
  1015. * If there are fewer than two bytes
  1016. * remaining in this buffer
  1017. *
  1018. * @throws ReadOnlyBufferException
  1019. * If this buffer is read-only
  1020. */
  1021. public abstract ByteBuffer putShort(short value);
  1022. /**
  1023. * Absolute <i>get</i> method for reading a short value.
  1024. *
  1025. * <p> Reads two bytes at the given index, composing them into a
  1026. * short value according to the current byte order. </p>
  1027. *
  1028. * @param index
  1029. * The index from which the bytes will be read
  1030. *
  1031. * @return The short value at the given index
  1032. *
  1033. * @throws IndexOutOfBoundsException
  1034. * If <tt>index</tt> is negative
  1035. * or not smaller than the buffer's limit,
  1036. * minus one
  1037. */
  1038. public abstract short getShort(int index);
  1039. /**
  1040. * Absolute <i>put</i> method for writing a short
  1041. * value  <i>(optional operation)</i>.
  1042. *
  1043. * <p> Writes two bytes containing the given short value, in the
  1044. * current byte order, into this buffer at the given index. </p>
  1045. *
  1046. * @param index
  1047. * The index at which the bytes will be written
  1048. *
  1049. * @param value
  1050. * The short value to be written
  1051. *
  1052. * @return This buffer
  1053. *
  1054. * @throws IndexOutOfBoundsException
  1055. * If <tt>index</tt> is negative
  1056. * or not smaller than the buffer's limit,
  1057. * minus one
  1058. *
  1059. * @throws ReadOnlyBufferException
  1060. * If this buffer is read-only
  1061. */
  1062. public abstract ByteBuffer putShort(int index, short value);
  1063. /**
  1064. * Creates a view of this byte buffer as a short buffer.
  1065. *
  1066. * <p> The content of the new buffer will start at this buffer's current
  1067. * position. Changes to this buffer's content will be visible in the new
  1068. * buffer, and vice versa; the two buffers' position, limit, and mark
  1069. * values will be independent.
  1070. *
  1071. * <p> The new buffer's position will be zero, its capacity and its limit
  1072. * will be the number of bytes remaining in this buffer divided by
  1073. * two, and its mark will be undefined. The new buffer will be direct
  1074. * if, and only if, this buffer is direct, and it will be read-only if, and
  1075. * only if, this buffer is read-only. </p>
  1076. *
  1077. * @return A new short buffer
  1078. */
  1079. public abstract ShortBuffer asShortBuffer();
  1080. /**
  1081. * Relative <i>get</i> method for reading an int value.
  1082. *
  1083. * <p> Reads the next four bytes at this buffer's current position,
  1084. * composing them into an int value according to the current byte order,
  1085. * and then increments the position by four. </p>
  1086. *
  1087. * @return The int value at the buffer's current position
  1088. *
  1089. * @throws BufferUnderflowException
  1090. * If there are fewer than four bytes
  1091. * remaining in this buffer
  1092. */
  1093. public abstract int getInt();
  1094. /**
  1095. * Relative <i>put</i> method for writing an int
  1096. * value  <i>(optional operation)</i>.
  1097. *
  1098. * <p> Writes four bytes containing the given int value, in the
  1099. * current byte order, into this buffer at the current position, and then
  1100. * increments the position by four. </p>
  1101. *
  1102. * @param value
  1103. * The int value to be written
  1104. *
  1105. * @return This buffer
  1106. *
  1107. * @throws BufferOverflowException
  1108. * If there are fewer than four bytes
  1109. * remaining in this buffer
  1110. *
  1111. * @throws ReadOnlyBufferException
  1112. * If this buffer is read-only
  1113. */
  1114. public abstract ByteBuffer putInt(int value);
  1115. /**
  1116. * Absolute <i>get</i> method for reading an int value.
  1117. *
  1118. * <p> Reads four bytes at the given index, composing them into a
  1119. * int value according to the current byte order. </p>
  1120. *
  1121. * @param index
  1122. * The index from which the bytes will be read
  1123. *
  1124. * @return The int value at the given index
  1125. *
  1126. * @throws IndexOutOfBoundsException
  1127. * If <tt>index</tt> is negative
  1128. * or not smaller than the buffer's limit,
  1129. * minus three
  1130. */
  1131. public abstract int getInt(int index);
  1132. /**
  1133. * Absolute <i>put</i> method for writing an int
  1134. * value  <i>(optional operation)</i>.
  1135. *
  1136. * <p> Writes four bytes containing the given int value, in the
  1137. * current byte order, into this buffer at the given index. </p>
  1138. *
  1139. * @param index
  1140. * The index at which the bytes will be written
  1141. *
  1142. * @param value
  1143. * The int value to be written
  1144. *
  1145. * @return This buffer
  1146. *
  1147. * @throws IndexOutOfBoundsException
  1148. * If <tt>index</tt> is negative
  1149. * or not smaller than the buffer's limit,
  1150. * minus three
  1151. *
  1152. * @throws ReadOnlyBufferException
  1153. * If this buffer is read-only
  1154. */
  1155. public abstract ByteBuffer putInt(int index, int value);
  1156. /**
  1157. * Creates a view of this byte buffer as an int buffer.
  1158. *
  1159. * <p> The content of the new buffer will start at this buffer's current
  1160. * position. Changes to this buffer's content will be visible in the new
  1161. * buffer, and vice versa; the two buffers' position, limit, and mark
  1162. * values will be independent.
  1163. *
  1164. * <p> The new buffer's position will be zero, its capacity and its limit
  1165. * will be the number of bytes remaining in this buffer divided by
  1166. * four, and its mark will be undefined. The new buffer will be direct
  1167. * if, and only if, this buffer is direct, and it will be read-only if, and
  1168. * only if, this buffer is read-only. </p>
  1169. *
  1170. * @return A new int buffer
  1171. */
  1172. public abstract IntBuffer asIntBuffer();
  1173. /**
  1174. * Relative <i>get</i> method for reading a long value.
  1175. *
  1176. * <p> Reads the next eight bytes at this buffer's current position,
  1177. * composing them into a long value according to the current byte order,
  1178. * and then increments the position by eight. </p>
  1179. *
  1180. * @return The long value at the buffer's current position
  1181. *
  1182. * @throws BufferUnderflowException
  1183. * If there are fewer than eight bytes
  1184. * remaining in this buffer
  1185. */
  1186. public abstract long getLong();
  1187. /**
  1188. * Relative <i>put</i> method for writing a long
  1189. * value  <i>(optional operation)</i>.
  1190. *
  1191. * <p> Writes eight bytes containing the given long value, in the
  1192. * current byte order, into this buffer at the current position, and then
  1193. * increments the position by eight. </p>
  1194. *
  1195. * @param value
  1196. * The long value to be written
  1197. *
  1198. * @return This buffer
  1199. *
  1200. * @throws BufferOverflowException
  1201. * If there are fewer than eight bytes
  1202. * remaining in this buffer
  1203. *
  1204. * @throws ReadOnlyBufferException
  1205. * If this buffer is read-only
  1206. */
  1207. public abstract ByteBuffer putLong(long value);
  1208. /**
  1209. * Absolute <i>get</i> method for reading a long value.
  1210. *
  1211. * <p> Reads eight bytes at the given index, composing them into a
  1212. * long value according to the current byte order. </p>
  1213. *
  1214. * @param index
  1215. * The index from which the bytes will be read
  1216. *
  1217. * @return The long value at the given index
  1218. *
  1219. * @throws IndexOutOfBoundsException
  1220. * If <tt>index</tt> is negative
  1221. * or not smaller than the buffer's limit,
  1222. * minus seven
  1223. */
  1224. public abstract long getLong(int index);
  1225. /**
  1226. * Absolute <i>put</i> method for writing a long
  1227. * value  <i>(optional operation)</i>.
  1228. *
  1229. * <p> Writes eight bytes containing the given long value, in the
  1230. * current byte order, into this buffer at the given index. </p>
  1231. *
  1232. * @param index
  1233. * The index at which the bytes will be written
  1234. *
  1235. * @param value
  1236. * The long value to be written
  1237. *
  1238. * @return This buffer
  1239. *
  1240. * @throws IndexOutOfBoundsException
  1241. * If <tt>index</tt> is negative
  1242. * or not smaller than the buffer's limit,
  1243. * minus seven
  1244. *
  1245. * @throws ReadOnlyBufferException
  1246. * If this buffer is read-only
  1247. */
  1248. public abstract ByteBuffer putLong(int index, long value);
  1249. /**
  1250. * Creates a view of this byte buffer as a long buffer.
  1251. *
  1252. * <p> The content of the new buffer will start at this buffer's current
  1253. * position. Changes to this buffer's content will be visible in the new
  1254. * buffer, and vice versa; the two buffers' position, limit, and mark
  1255. * values will be independent.
  1256. *
  1257. * <p> The new buffer's position will be zero, its capacity and its limit
  1258. * will be the number of bytes remaining in this buffer divided by
  1259. * eight, and its mark will be undefined. The new buffer will be direct
  1260. * if, and only if, this buffer is direct, and it will be read-only if, and
  1261. * only if, this buffer is read-only. </p>
  1262. *
  1263. * @return A new long buffer
  1264. */
  1265. public abstract LongBuffer asLongBuffer();
  1266. /**
  1267. * Relative <i>get</i> method for reading a float value.
  1268. *
  1269. * <p> Reads the next four bytes at this buffer's current position,
  1270. * composing them into a float value according to the current byte order,
  1271. * and then increments the position by four. </p>
  1272. *
  1273. * @return The float value at the buffer's current position
  1274. *
  1275. * @throws BufferUnderflowException
  1276. * If there are fewer than four bytes
  1277. * remaining in this buffer
  1278. */
  1279. public abstract float getFloat();
  1280. /**
  1281. * Relative <i>put</i> method for writing a float
  1282. * value  <i>(optional operation)</i>.
  1283. *
  1284. * <p> Writes four bytes containing the given float value, in the
  1285. * current byte order, into this buffer at the current position, and then
  1286. * increments the position by four. </p>
  1287. *
  1288. * @param value
  1289. * The float value to be written
  1290. *
  1291. * @return This buffer
  1292. *
  1293. * @throws BufferOverflowException
  1294. * If there are fewer than four bytes
  1295. * remaining in this buffer
  1296. *
  1297. * @throws ReadOnlyBufferException
  1298. * If this buffer is read-only
  1299. */
  1300. public abstract ByteBuffer putFloat(float value);
  1301. /**
  1302. * Absolute <i>get</i> method for reading a float value.
  1303. *
  1304. * <p> Reads four bytes at the given index, composing them into a
  1305. * float value according to the current byte order. </p>
  1306. *
  1307. * @param index
  1308. * The index from which the bytes will be read
  1309. *
  1310. * @return The float value at the given index
  1311. *
  1312. * @throws IndexOutOfBoundsException
  1313. * If <tt>index</tt> is negative
  1314. * or not smaller than the buffer's limit,
  1315. * minus three
  1316. */
  1317. public abstract float getFloat(int index);
  1318. /**
  1319. * Absolute <i>put</i> method for writing a float
  1320. * value  <i>(optional operation)</i>.
  1321. *
  1322. * <p> Writes four bytes containing the given float value, in the
  1323. * current byte order, into this buffer at the given index. </p>
  1324. *
  1325. * @param index
  1326. * The index at which the bytes will be written
  1327. *
  1328. * @param value
  1329. * The float value to be written
  1330. *
  1331. * @return This buffer
  1332. *
  1333. * @throws IndexOutOfBoundsException
  1334. * If <tt>index</tt> is negative
  1335. * or not smaller than the buffer's limit,
  1336. * minus three
  1337. *
  1338. * @throws ReadOnlyBufferException
  1339. * If this buffer is read-only
  1340. */
  1341. public abstract ByteBuffer putFloat(int index, float value);
  1342. /**
  1343. * Creates a view of this byte buffer as a float buffer.
  1344. *
  1345. * <p> The content of the new buffer will start at this buffer's current
  1346. * position. Changes to this buffer's content will be visible in the new
  1347. * buffer, and vice versa; the two buffers' position, limit, and mark
  1348. * values will be independent.
  1349. *
  1350. * <p> The new buffer's position will be zero, its capacity and its limit
  1351. * will be the number of bytes remaining in this buffer divided by
  1352. * four, and its mark will be undefined. The new buffer will be direct
  1353. * if, and only if, this buffer is direct, and it will be read-only if, and
  1354. * only if, this buffer is read-only. </p>
  1355. *
  1356. * @return A new float buffer
  1357. */
  1358. public abstract FloatBuffer asFloatBuffer();
  1359. /**
  1360. * Relative <i>get</i> method for reading a double value.
  1361. *
  1362. * <p> Reads the next eight bytes at this buffer's current position,
  1363. * composing them into a double value according to the current byte order,
  1364. * and then increments the position by eight. </p>
  1365. *
  1366. * @return The double value at the buffer's current position
  1367. *
  1368. * @throws BufferUnderflowException
  1369. * If there are fewer than eight bytes
  1370. * remaining in this buffer
  1371. */
  1372. public abstract double getDouble();
  1373. /**
  1374. * Relative <i>put</i> method for writing a double
  1375. * value  <i>(optional operation)</i>.
  1376. *
  1377. * <p> Writes eight bytes containing the given double value, in the
  1378. * current byte order, into this buffer at the current position, and then
  1379. * increments the position by eight. </p>
  1380. *
  1381. * @param value
  1382. * The double value to be written
  1383. *
  1384. * @return This buffer
  1385. *
  1386. * @throws BufferOverflowException
  1387. * If there are fewer than eight bytes
  1388. * remaining in this buffer
  1389. *
  1390. * @throws ReadOnlyBufferException
  1391. * If this buffer is read-only
  1392. */
  1393. public abstract ByteBuffer putDouble(double value);
  1394. /**
  1395. * Absolute <i>get</i> method for reading a double value.
  1396. *
  1397. * <p> Reads eight bytes at the given index, composing them into a
  1398. * double value according to the current byte order. </p>
  1399. *
  1400. * @param index
  1401. * The index from which the bytes will be read
  1402. *
  1403. * @return The double value at the given index
  1404. *
  1405. * @throws IndexOutOfBoundsException
  1406. * If <tt>index</tt> is negative
  1407. * or not smaller than the buffer's limit,
  1408. * minus seven
  1409. */
  1410. public abstract double getDouble(int index);
  1411. /**
  1412. * Absolute <i>put</i> method for writing a double
  1413. * value  <i>(optional operation)</i>.
  1414. *
  1415. * <p> Writes eight bytes containing the given double value, in the
  1416. * current byte order, into this buffer at the given index. </p>
  1417. *
  1418. * @param index
  1419. * The index at which the bytes will be written
  1420. *
  1421. * @param value
  1422. * The double value to be written
  1423. *
  1424. * @return This buffer
  1425. *
  1426. * @throws IndexOutOfBoundsException
  1427. * If <tt>index</tt> is negative
  1428. * or not smaller than the buffer's limit,
  1429. * minus seven
  1430. *
  1431. * @throws ReadOnlyBufferException
  1432. * If this buffer is read-only
  1433. */
  1434. public abstract ByteBuffer putDouble(int index, double value);
  1435. /**
  1436. * Creates a view of this byte buffer as a double buffer.
  1437. *
  1438. * <p> The content of the new buffer will start at this buffer's current
  1439. * position. Changes to this buffer's content will be visible in the new
  1440. * buffer, and vice versa; the two buffers' position, limit, and mark
  1441. * values will be independent.
  1442. *
  1443. * <p> The new buffer's position will be zero, its capacity and its limit
  1444. * will be the number of bytes remaining in this buffer divided by
  1445. * eight, and its mark will be undefined. The new buffer will be direct
  1446. * if, and only if, this buffer is direct, and it will be read-only if, and
  1447. * only if, this buffer is read-only. </p>
  1448. *
  1449. * @return A new double buffer
  1450. */
  1451. public abstract DoubleBuffer asDoubleBuffer();
  1452. }