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 character buffer.
  11. *
  12. * <p> This class defines four categories of operations upon
  13. * character buffers:
  14. *
  15. * <ul>
  16. *
  17. * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
  18. * {@link #put(char) </code><i>put</i><code>} methods that read and write
  19. * single characters; </p></li>
  20. *
  21. * <li><p> Relative {@link #get(char[]) </code><i>bulk get</i><code>}
  22. * methods that transfer contiguous sequences of characters from this buffer
  23. * into an array; and</p></li>
  24. *
  25. * <li><p> Relative {@link #put(char[]) </code><i>bulk put</i><code>}
  26. * methods that transfer contiguous sequences of characters from a
  27. * character array, a string, or some other character
  28. * buffer into this buffer; and </p></li>
  29. *
  30. *
  31. * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
  32. * #duplicate </code>duplicating<code>}, and {@link #slice
  33. * </code>slicing<code>} a character buffer. </p></li>
  34. *
  35. * </ul>
  36. *
  37. * <p> Character buffers can be created either by {@link #allocate
  38. * </code><i>allocation</i><code>}, which allocates space for the buffer's
  39. *
  40. *
  41. * content, by {@link #wrap(char[]) </code><i>wrapping</i><code>} an existing
  42. * character array or string into a buffer, or by creating a
  43. * <a href="ByteBuffer.html#view"><i>view</i></a> of an existing byte buffer
  44. *
  45. *
  46. *
  47. *
  48. * <p> Like a byte buffer, a character buffer is either <a
  49. * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A
  50. * character buffer created via the <tt>wrap</tt> methods of this class will
  51. * be non-direct. A character buffer created as a view of a byte buffer will
  52. * be direct if, and only if, the byte buffer itself is direct. Whether or not
  53. * a character buffer is direct may be determined by invoking the {@link
  54. * #isDirect isDirect} method. </p>
  55. *
  56. *
  57. *
  58. * <p> This class implements the {@link CharSequence} interface so that
  59. * character buffers may be used wherever character sequences are accepted, for
  60. * example in the regular-expression package <tt>{@link java.util.regex}</tt>.
  61. * </p>
  62. *
  63. *
  64. *
  65. * <p> Methods in this class that do not otherwise have a value to return are
  66. * specified to return the buffer upon which they are invoked. This allows
  67. * method invocations to be chained.
  68. *
  69. *
  70. * The sequence of statements
  71. *
  72. * <blockquote><pre>
  73. * cb.put("text/");
  74. * cb.put(subtype);
  75. * cb.put("; charset=");
  76. * cb.put(enc);</pre></blockquote>
  77. *
  78. * can, for example, be replaced by the single statement
  79. *
  80. * <blockquote><pre>
  81. * cb.put("text/").put(subtype).put("; charset=").put(enc);</pre></blockquote>
  82. *
  83. *
  84. *
  85. * @author Mark Reinhold
  86. * @author JSR-51 Expert Group
  87. * @version 1.48, 03/01/23
  88. * @since 1.4
  89. */
  90. public abstract class CharBuffer
  91. extends Buffer
  92. implements Comparable, CharSequence
  93. {
  94. // These fields are declared here rather than in Heap-X-Buffer in order to
  95. // reduce the number of virtual method invocations needed to access these
  96. // values, which is especially costly when coding small buffers.
  97. //
  98. final char[] hb; // Non-null only for heap buffers
  99. final int offset;
  100. boolean isReadOnly; // Valid only for heap buffers
  101. // Creates a new buffer with the given mark, position, limit, capacity,
  102. // backing array, and array offset
  103. //
  104. CharBuffer(int mark, int pos, int lim, int cap, // package-private
  105. char[] hb, int offset)
  106. {
  107. super(mark, pos, lim, cap);
  108. this.hb = hb;
  109. this.offset = offset;
  110. }
  111. // Creates a new buffer with the given mark, position, limit, and capacity
  112. //
  113. CharBuffer(int mark, int pos, int lim, int cap) { // package-private
  114. this(mark, pos, lim, cap, null, 0);
  115. }
  116. /**
  117. * Allocates a new character buffer.
  118. *
  119. * <p> The new buffer's position will be zero, its limit will be its
  120. * capacity, and its mark will be undefined. It will have a {@link #array
  121. * </code>backing array<code>}, and its {@link #arrayOffset </code>array
  122. * offset<code>} will be zero.
  123. *
  124. * @param capacity
  125. * The new buffer's capacity, in characters
  126. *
  127. * @return The new character buffer
  128. *
  129. * @throws IllegalArgumentException
  130. * If the <tt>capacity</tt> is a negative integer
  131. */
  132. public static CharBuffer allocate(int capacity) {
  133. if (capacity < 0)
  134. throw new IllegalArgumentException();
  135. return new HeapCharBuffer(capacity, capacity);
  136. }
  137. /**
  138. * Wraps a character array into a buffer.
  139. *
  140. * <p> The new buffer will be backed by the the given character array;
  141. * that is, modifications to the buffer will cause the array to be modified
  142. * and vice versa. The new buffer's capacity will be
  143. * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
  144. * will be <tt>offset + length</tt>, and its mark will be undefined. Its
  145. * {@link #array </code>backing array<code>} will be the given array, and
  146. * its {@link #arrayOffset </code>array offset<code>} will be zero. </p>
  147. *
  148. * @param array
  149. * The array that will back the new buffer
  150. *
  151. * @param offset
  152. * The offset of the subarray to be used; must be non-negative and
  153. * no larger than <tt>array.length</tt>. The new buffer's position
  154. * will be set to this value.
  155. *
  156. * @param length
  157. * The length of the subarray to be used;
  158. * must be non-negative and no larger than
  159. * <tt>array.length - offset</tt>.
  160. * The new buffer's limit will be set to <tt>offset + length</tt>.
  161. *
  162. * @return The new character buffer
  163. *
  164. * @throws IndexOutOfBoundsException
  165. * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
  166. * parameters do not hold
  167. */
  168. public static CharBuffer wrap(char[] array,
  169. int offset, int length)
  170. {
  171. try {
  172. return new HeapCharBuffer(array, offset, length);
  173. } catch (IllegalArgumentException x) {
  174. throw new IndexOutOfBoundsException();
  175. }
  176. }
  177. /**
  178. * Wraps a character array into a buffer.
  179. *
  180. * <p> The new buffer will be backed by the the given character array;
  181. * that is, modifications to the buffer will cause the array to be modified
  182. * and vice versa. The new buffer's capacity and limit will be
  183. * <tt>array.length</tt>, its position will be zero, and its mark will be
  184. * undefined. Its {@link #array </code>backing array<code>} will be the
  185. * given array, and its {@link #arrayOffset </code>array offset<code>} will
  186. * be zero. </p>
  187. *
  188. * @param array
  189. * The array that will back this buffer
  190. *
  191. * @return The new character buffer
  192. */
  193. public static CharBuffer wrap(char[] array) {
  194. return wrap(array, 0, array.length);
  195. }
  196. /**
  197. * Wraps a character sequence into a buffer.
  198. *
  199. * <p> The content of the new, read-only buffer will be the content of the
  200. * given character sequence. The buffer's capacity will be
  201. * <tt>csq.length()</tt>, its position will be <tt>start</tt>, its limit
  202. * will be <tt>end</tt>, and its mark will be undefined. </p>
  203. *
  204. * @param csq
  205. * The character sequence from which the new character buffer is to
  206. * be created
  207. *
  208. * @param start
  209. * The index of the first character to be used;
  210. * must be non-negative and no larger than <tt>csq.length()</tt>.
  211. * The new buffer's position will be set to this value.
  212. *
  213. * @param end
  214. * The index of the character following the last character to be
  215. * used; must be no smaller than <tt>start</tt> and no larger
  216. * than <tt>csq.length()</tt>.
  217. * The new buffer's limit will be set to this value.
  218. *
  219. * @return The new character buffer
  220. *
  221. * @throws IndexOutOfBoundsException
  222. * If the preconditions on the <tt>start</tt> and <tt>end</tt>
  223. * parameters do not hold
  224. */
  225. public static CharBuffer wrap(CharSequence csq, int start, int end) {
  226. try {
  227. return new StringCharBuffer(csq, start, end);
  228. } catch (IllegalArgumentException x) {
  229. throw new IndexOutOfBoundsException();
  230. }
  231. }
  232. /**
  233. * Wraps a string into a buffer.
  234. *
  235. * <p> The content of the new, read-only buffer will be the content of the
  236. * given string. The new buffer's capacity and limit will be
  237. * <tt>csq.length()</tt>, its position will be zero, and its mark will be
  238. * undefined. </p>
  239. *
  240. * @param csq
  241. * The character sequence from which the new character buffer is to
  242. * be created
  243. *
  244. * @return The new character buffer
  245. */
  246. public static CharBuffer wrap(CharSequence csq) {
  247. return wrap(csq, 0, csq.length());
  248. }
  249. /**
  250. * Creates a new character buffer whose content is a shared subsequence of
  251. * this buffer's content.
  252. *
  253. * <p> The content of the new buffer will start at this buffer's current
  254. * position. Changes to this buffer's content will be visible in the new
  255. * buffer, and vice versa; the two buffers' position, limit, and mark
  256. * values will be independent.
  257. *
  258. * <p> The new buffer's position will be zero, its capacity and its limit
  259. * will be the number of characters remaining in this buffer, and its mark
  260. * will be undefined. The new buffer will be direct if, and only if, this
  261. * buffer is direct, and it will be read-only if, and only if, this buffer
  262. * is read-only. </p>
  263. *
  264. * @return The new character buffer
  265. */
  266. public abstract CharBuffer slice();
  267. /**
  268. * Creates a new character buffer that shares this buffer's content.
  269. *
  270. * <p> The content of the new buffer will be that of this buffer. Changes
  271. * to this buffer's content will be visible in the new buffer, and vice
  272. * versa; the two buffers' position, limit, and mark values will be
  273. * independent.
  274. *
  275. * <p> The new buffer's capacity, limit, position, and mark values will be
  276. * identical to those of this buffer. The new buffer will be direct if,
  277. * and only if, this buffer is direct, and it will be read-only if, and
  278. * only if, this buffer is read-only. </p>
  279. *
  280. * @return The new character buffer
  281. */
  282. public abstract CharBuffer duplicate();
  283. /**
  284. * Creates a new, read-only character buffer that shares this buffer's
  285. * content.
  286. *
  287. * <p> The content of the new buffer will be that of this buffer. Changes
  288. * to this buffer's content will be visible in the new buffer; the new
  289. * buffer itself, however, will be read-only and will not allow the shared
  290. * content to be modified. The two buffers' position, limit, and mark
  291. * values will be independent.
  292. *
  293. * <p> The new buffer's capacity, limit, position, and mark values will be
  294. * identical to those of this buffer.
  295. *
  296. * <p> If this buffer is itself read-only then this method behaves in
  297. * exactly the same way as the {@link #duplicate duplicate} method. </p>
  298. *
  299. * @return The new, read-only character buffer
  300. */
  301. public abstract CharBuffer asReadOnlyBuffer();
  302. // -- Singleton get/put methods --
  303. /**
  304. * Relative <i>get</i> method. Reads the character at this buffer's
  305. * current position, and then increments the position. </p>
  306. *
  307. * @return The character at the buffer's current position
  308. *
  309. * @throws BufferUnderflowException
  310. * If the buffer's current position is not smaller than its limit
  311. */
  312. public abstract char get();
  313. /**
  314. * Relative <i>put</i> method  <i>(optional operation)</i>.
  315. *
  316. * <p> Writes the given character into this buffer at the current
  317. * position, and then increments the position. </p>
  318. *
  319. * @param c
  320. * The character to be written
  321. *
  322. * @return This buffer
  323. *
  324. * @throws BufferOverflowException
  325. * If this buffer's current position is not smaller than its limit
  326. *
  327. * @throws ReadOnlyBufferException
  328. * If this buffer is read-only
  329. */
  330. public abstract CharBuffer put(char c);
  331. /**
  332. * Absolute <i>get</i> method. Reads the character at the given
  333. * index. </p>
  334. *
  335. * @param index
  336. * The index from which the character will be read
  337. *
  338. * @return The character at the given index
  339. *
  340. * @throws IndexOutOfBoundsException
  341. * If <tt>index</tt> is negative
  342. * or not smaller than the buffer's limit
  343. */
  344. public abstract char get(int index);
  345. /**
  346. * Absolute <i>put</i> method  <i>(optional operation)</i>.
  347. *
  348. * <p> Writes the given character into this buffer at the given
  349. * index. </p>
  350. *
  351. * @param index
  352. * The index at which the character will be written
  353. *
  354. * @param c
  355. * The character value to be written
  356. *
  357. * @return This buffer
  358. *
  359. * @throws IndexOutOfBoundsException
  360. * If <tt>index</tt> is negative
  361. * or not smaller than the buffer's limit
  362. *
  363. * @throws ReadOnlyBufferException
  364. * If this buffer is read-only
  365. */
  366. public abstract CharBuffer put(int index, char c);
  367. // -- Bulk get operations --
  368. /**
  369. * Relative bulk <i>get</i> method.
  370. *
  371. * <p> This method transfers characters from this buffer into the given
  372. * destination array. If there are fewer characters remaining in the
  373. * buffer than are required to satisfy the request, that is, if
  374. * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no
  375. * characters are transferred and a {@link BufferUnderflowException} is
  376. * thrown.
  377. *
  378. * <p> Otherwise, this method copies <tt>length</tt> characters from this
  379. * buffer into the given array, starting at the current position of this
  380. * buffer and at the given offset in the array. The position of this
  381. * buffer is then incremented by <tt>length</tt>.
  382. *
  383. * <p> In other words, an invocation of this method of the form
  384. * <tt>src.get(dst, off, len)</tt> has exactly the same effect as
  385. * the loop
  386. *
  387. * <pre>
  388. * for (int i = off; i < off + len; i++)
  389. * dst[i] = src.get(); </pre>
  390. *
  391. * except that it first checks that there are sufficient characters in
  392. * this buffer and it is potentially much more efficient. </p>
  393. *
  394. * @param dst
  395. * The array into which characters are to be written
  396. *
  397. * @param offset
  398. * The offset within the array of the first character to be
  399. * written; must be non-negative and no larger than
  400. * <tt>dst.length</tt>
  401. *
  402. * @param length
  403. * The maximum number of characters to be written to the given
  404. * array; must be non-negative and no larger than
  405. * <tt>dst.length - offset</tt>
  406. *
  407. * @return This buffer
  408. *
  409. * @throws BufferUnderflowException
  410. * If there are fewer than <tt>length</tt> characters
  411. * remaining in this buffer
  412. *
  413. * @throws IndexOutOfBoundsException
  414. * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
  415. * parameters do not hold
  416. */
  417. public CharBuffer get(char[] dst, int offset, int length) {
  418. checkBounds(offset, length, dst.length);
  419. if (length > remaining())
  420. throw new BufferUnderflowException();
  421. int end = offset + length;
  422. for (int i = offset; i < end; i++)
  423. dst[i] = get();
  424. return this;
  425. }
  426. /**
  427. * Relative bulk <i>get</i> method.
  428. *
  429. * <p> This method transfers characters from this buffer into the given
  430. * destination array. An invocation of this method of the form
  431. * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
  432. *
  433. * <pre>
  434. * src.get(a, 0, a.length) </pre>
  435. *
  436. * @return This buffer
  437. *
  438. * @throws BufferUnderflowException
  439. * If there are fewer than <tt>length</tt> characters
  440. * remaining in this buffer
  441. */
  442. public CharBuffer get(char[] dst) {
  443. return get(dst, 0, dst.length);
  444. }
  445. // -- Bulk put operations --
  446. /**
  447. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  448. *
  449. * <p> This method transfers the characters remaining in the given source
  450. * buffer into this buffer. If there are more characters remaining in the
  451. * source buffer than in this buffer, that is, if
  452. * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>,
  453. * then no characters are transferred and a {@link
  454. * BufferOverflowException} is thrown.
  455. *
  456. * <p> Otherwise, this method copies
  457. * <i>n</i> = <tt>src.remaining()</tt> characters from the given
  458. * buffer into this buffer, starting at each buffer's current position.
  459. * The positions of both buffers are then incremented by <i>n</i>.
  460. *
  461. * <p> In other words, an invocation of this method of the form
  462. * <tt>dst.put(src)</tt> has exactly the same effect as the loop
  463. *
  464. * <pre>
  465. * while (src.hasRemaining())
  466. * dst.put(src.get()); </pre>
  467. *
  468. * except that it first checks that there is sufficient space in this
  469. * buffer and it is potentially much more efficient. </p>
  470. *
  471. * @param src
  472. * The source buffer from which characters are to be read;
  473. * must not be this buffer
  474. *
  475. * @return This buffer
  476. *
  477. * @throws BufferOverflowException
  478. * If there is insufficient space in this buffer
  479. * for the remaining characters in the source buffer
  480. *
  481. * @throws IllegalArgumentException
  482. * If the source buffer is this buffer
  483. *
  484. * @throws ReadOnlyBufferException
  485. * If this buffer is read-only
  486. */
  487. public CharBuffer put(CharBuffer src) {
  488. if (src == this)
  489. throw new IllegalArgumentException();
  490. int n = src.remaining();
  491. if (n > remaining())
  492. throw new BufferOverflowException();
  493. for (int i = 0; i < n; i++)
  494. put(src.get());
  495. return this;
  496. }
  497. /**
  498. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  499. *
  500. * <p> This method transfers characters into this buffer from the given
  501. * source array. If there are more characters to be copied from the array
  502. * than remain in this buffer, that is, if
  503. * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no
  504. * characters are transferred and a {@link BufferOverflowException} is
  505. * thrown.
  506. *
  507. * <p> Otherwise, this method copies <tt>length</tt> characters from the
  508. * given array into this buffer, starting at the given offset in the array
  509. * and at the current position of this buffer. The position of this buffer
  510. * is then incremented by <tt>length</tt>.
  511. *
  512. * <p> In other words, an invocation of this method of the form
  513. * <tt>dst.put(src, off, len)</tt> has exactly the same effect as
  514. * the loop
  515. *
  516. * <pre>
  517. * for (int i = off; i < off + len; i++)
  518. * dst.put(a[i]); </pre>
  519. *
  520. * except that it first checks that there is sufficient space in this
  521. * buffer and it is potentially much more efficient. </p>
  522. *
  523. * @param src
  524. * The array from which characters are to be read
  525. *
  526. * @param offset
  527. * The offset within the array of the first character to be read;
  528. * must be non-negative and no larger than <tt>array.length</tt>
  529. *
  530. * @param length
  531. * The number of characters to be read from the given array;
  532. * must be non-negative and no larger than
  533. * <tt>array.length - offset</tt>
  534. *
  535. * @return This buffer
  536. *
  537. * @throws BufferOverflowException
  538. * If there is insufficient space in this buffer
  539. *
  540. * @throws IndexOutOfBoundsException
  541. * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
  542. * parameters do not hold
  543. *
  544. * @throws ReadOnlyBufferException
  545. * If this buffer is read-only
  546. */
  547. public CharBuffer put(char[] src, int offset, int length) {
  548. checkBounds(offset, length, src.length);
  549. if (length > remaining())
  550. throw new BufferOverflowException();
  551. int end = offset + length;
  552. for (int i = offset; i < end; i++)
  553. this.put(src[i]);
  554. return this;
  555. }
  556. /**
  557. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  558. *
  559. * <p> This method transfers the entire content of the given source
  560. * character array into this buffer. An invocation of this method of the
  561. * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
  562. * invocation
  563. *
  564. * <pre>
  565. * dst.put(a, 0, a.length) </pre>
  566. *
  567. * @return This buffer
  568. *
  569. * @throws BufferOverflowException
  570. * If there is insufficient space in this buffer
  571. *
  572. * @throws ReadOnlyBufferException
  573. * If this buffer is read-only
  574. */
  575. public final CharBuffer put(char[] src) {
  576. return put(src, 0, src.length);
  577. }
  578. /**
  579. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  580. *
  581. * <p> This method transfers characters from the given string into this
  582. * buffer. If there are more characters to be copied from the string than
  583. * remain in this buffer, that is, if
  584. * <tt>end - start</tt> <tt>></tt> <tt>remaining()</tt>,
  585. * then no characters are transferred and a {@link
  586. * BufferOverflowException} is thrown.
  587. *
  588. * <p> Otherwise, this method copies
  589. * <i>n</i> = <tt>end</tt> - <tt>start</tt> characters
  590. * from the given string into this buffer, starting at the given
  591. * <tt>start</tt> index and at the current position of this buffer. The
  592. * position of this buffer is then incremented by <i>n</i>.
  593. *
  594. * <p> In other words, an invocation of this method of the form
  595. * <tt>dst.put(src, start, end)</tt> has exactly the same effect
  596. * as the loop
  597. *
  598. * <pre>
  599. * for (int i = start; i < end; i++)
  600. * dst.put(src.charAt(i)); </pre>
  601. *
  602. * except that it first checks that there is sufficient space in this
  603. * buffer and it is potentially much more efficient. </p>
  604. *
  605. * @param src
  606. * The string from which characters are to be read
  607. *
  608. * @param start
  609. * The offset within the string of the first character to be read;
  610. * must be non-negative and no larger than
  611. * <tt>string.length()</tt>
  612. *
  613. * @param end
  614. * The offset within the string of the last character to be read,
  615. * plus one; must be non-negative and no larger than
  616. * <tt>string.length()</tt>
  617. *
  618. * @return This buffer
  619. *
  620. * @throws BufferOverflowException
  621. * If there is insufficient space in this buffer
  622. *
  623. * @throws IndexOutOfBoundsException
  624. * If the preconditions on the <tt>start</tt> and <tt>end</tt>
  625. * parameters do not hold
  626. *
  627. * @throws ReadOnlyBufferException
  628. * If this buffer is read-only
  629. */
  630. public CharBuffer put(String src, int start, int end) {
  631. checkBounds(start, end - start, src.length());
  632. for (int i = start; i < end; i++)
  633. this.put(src.charAt(i));
  634. return this;
  635. }
  636. /**
  637. * Relative bulk <i>put</i> method  <i>(optional operation)</i>.
  638. *
  639. * <p> This method transfers the entire content of the given source string
  640. * into this buffer. An invocation of this method of the form
  641. * <tt>dst.put(s)</tt> behaves in exactly the same way as the invocation
  642. *
  643. * <pre>
  644. * dst.put(s, 0, s.length()) </pre>
  645. *
  646. * @return This buffer
  647. *
  648. * @throws BufferOverflowException
  649. * If there is insufficient space in this buffer
  650. *
  651. * @throws ReadOnlyBufferException
  652. * If this buffer is read-only
  653. */
  654. public final CharBuffer put(String src) {
  655. return put(src, 0, src.length());
  656. }
  657. // -- Other stuff --
  658. /**
  659. * Tells whether or not this buffer is backed by an accessible character
  660. * array.
  661. *
  662. * <p> If this method returns <tt>true</tt> then the {@link #array() array}
  663. * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
  664. * </p>
  665. *
  666. * @return <tt>true</tt> if, and only if, this buffer
  667. * is backed by an array and is not read-only
  668. */
  669. public final boolean hasArray() {
  670. return (hb != null) && !isReadOnly;
  671. }
  672. /**
  673. * Returns the character array that backs this
  674. * buffer  <i>(optional operation)</i>.
  675. *
  676. * <p> Modifications to this buffer's content will cause the returned
  677. * array's content to be modified, and vice versa.
  678. *
  679. * <p> Invoke the {@link #hasArray hasArray} method before invoking this
  680. * method in order to ensure that this buffer has an accessible backing
  681. * array. </p>
  682. *
  683. * @return The array that backs this buffer
  684. *
  685. * @throws ReadOnlyBufferException
  686. * If this buffer is backed by an array but is read-only
  687. *
  688. * @throws UnsupportedOperationException
  689. * If this buffer is not backed by an accessible array
  690. */
  691. public final char[] array() {
  692. if (hb == null)
  693. throw new UnsupportedOperationException();
  694. if (isReadOnly)
  695. throw new ReadOnlyBufferException();
  696. return hb;
  697. }
  698. /**
  699. * Returns the offset within this buffer's backing array of the first
  700. * element of the buffer  <i>(optional operation)</i>.
  701. *
  702. * <p> If this buffer is backed by an array then buffer position <i>p</i>
  703. * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>.
  704. *
  705. * <p> Invoke the {@link #hasArray hasArray} method before invoking this
  706. * method in order to ensure that this buffer has an accessible backing
  707. * array. </p>
  708. *
  709. * @return The offset within this buffer's array
  710. * of the first element of the buffer
  711. *
  712. * @throws ReadOnlyBufferException
  713. * If this buffer is backed by an array but is read-only
  714. *
  715. * @throws UnsupportedOperationException
  716. * If this buffer is not backed by an accessible array
  717. */
  718. public final int arrayOffset() {
  719. if (hb == null)
  720. throw new UnsupportedOperationException();
  721. if (isReadOnly)
  722. throw new ReadOnlyBufferException();
  723. return offset;
  724. }
  725. /**
  726. * Compacts this buffer  <i>(optional operation)</i>.
  727. *
  728. * <p> The characters between the buffer's current position and its limit,
  729. * if any, are copied to the beginning of the buffer. That is, the
  730. * character at index <i>p</i> = <tt>position()</tt> is copied
  731. * to index zero, the character at index <i>p</i> + 1 is copied
  732. * to index one, and so forth until the character at index
  733. * <tt>limit()</tt> - 1 is copied to index
  734. * <i>n</i> = <tt>limit()</tt> - <tt>1</tt> - <i>p</i>.
  735. * The buffer's position is then set to <i>n+1</i> and its limit is set to
  736. * its capacity. The mark, if defined, is discarded.
  737. *
  738. * <p> The buffer's position is set to the number of characters copied,
  739. * rather than to zero, so that an invocation of this method can be
  740. * followed immediately by an invocation of another relative <i>put</i>
  741. * method. </p>
  742. *
  743. *
  744. * @return This buffer
  745. *
  746. * @throws ReadOnlyBufferException
  747. * If this buffer is read-only
  748. */
  749. public abstract CharBuffer compact();
  750. /**
  751. * Tells whether or not this character buffer is direct. </p>
  752. *
  753. * @return <tt>true</tt> if, and only if, this buffer is direct
  754. */
  755. public abstract boolean isDirect();
  756. /**
  757. * Returns the current hash code of this buffer.
  758. *
  759. * <p> The hash code of a char buffer depends only upon its remaining
  760. * elements; that is, upon the elements from <tt>position()</tt> up to, and
  761. * including, the element at <tt>limit()</tt> - <tt>1</tt>.
  762. *
  763. * <p> Because buffer hash codes are content-dependent, it is inadvisable
  764. * to use buffers as keys in hash maps or similar data structures unless it
  765. * is known that their contents will not change. </p>
  766. *
  767. * @return The current hash code of this buffer
  768. */
  769. public int hashCode() {
  770. int h = 1;
  771. int p = position();
  772. for (int i = limit() - 1; i >= p; i--)
  773. h = 31 * h + (int)get(i);
  774. return h;
  775. }
  776. /**
  777. * Tells whether or not this buffer is equal to another object.
  778. *
  779. * <p> Two char buffers are equal if, and only if,
  780. *
  781. * <p><ol>
  782. *
  783. * <li><p> They have the same element type, </p></li>
  784. *
  785. * <li><p> They have the same number of remaining elements, and
  786. * </p></li>
  787. *
  788. * <li><p> The two sequences of remaining elements, considered
  789. * independently of their starting positions, are pointwise equal.
  790. * </p></li>
  791. *
  792. * </ol>
  793. *
  794. * <p> A char buffer is not equal to any other type of object. </p>
  795. *
  796. * @param ob The object to which this buffer is to be compared
  797. *
  798. * @return <tt>true</tt> if, and only if, this buffer is equal to the
  799. * given object
  800. */
  801. public boolean equals(Object ob) {
  802. if (!(ob instanceof CharBuffer))
  803. return false;
  804. CharBuffer that = (CharBuffer)ob;
  805. if (this.remaining() != that.remaining())
  806. return false;
  807. int p = this.position();
  808. for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
  809. char v1 = this.get(i);
  810. char v2 = that.get(j);
  811. if (v1 != v2) {
  812. if ((v1 != v1) && (v2 != v2)) // For float and double
  813. continue;
  814. return false;
  815. }
  816. }
  817. return true;
  818. }
  819. /**
  820. * Compares this buffer to another object.
  821. *
  822. * <p> Two char buffers are compared by comparing their sequences of
  823. * remaining elements lexicographically, without regard to the starting
  824. * position of each sequence within its corresponding buffer.
  825. *
  826. * <p> A char buffer is not comparable to any other type of object. </p>
  827. *
  828. * @return A negative integer, zero, or a positive integer as this buffer
  829. * is less than, equal to, or greater than the given buffer
  830. *
  831. * @throws ClassCastException
  832. * If the argument is not a char buffer
  833. */
  834. public int compareTo(Object ob) {
  835. CharBuffer that = (CharBuffer)ob;
  836. int n = this.position() + Math.min(this.remaining(), that.remaining());
  837. for (int i = this.position(), j = that.position(); i < n; i++, j++) {
  838. char v1 = this.get(i);
  839. char v2 = that.get(j);
  840. if (v1 == v2)
  841. continue;
  842. if ((v1 != v1) && (v2 != v2)) // For float and double
  843. continue;
  844. if (v1 < v2)
  845. return -1;
  846. return +1;
  847. }
  848. return this.remaining() - that.remaining();
  849. }
  850. // -- Other char stuff --
  851. /**
  852. * Returns a string containing the characters in this buffer.
  853. *
  854. * <p> The first character of the resulting string will be the character at
  855. * this buffer's position, while the last character will be the character
  856. * at index <tt>limit()</tt> - 1. Invoking this method does not
  857. * change the buffer's position. </p>
  858. *
  859. * @return The specified string
  860. */
  861. public String toString() {
  862. return toString(position(), limit());
  863. }
  864. abstract String toString(int start, int end); // package-private
  865. // --- Methods to support CharSequence ---
  866. /**
  867. * Returns the length of this character buffer.
  868. *
  869. * <p> When viewed as a character sequence, the length of a character
  870. * buffer is simply the number of characters between the position
  871. * (inclusive) and the limit (exclusive); that is, it is equivalent to
  872. * <tt>remaining()</tt>. </p>
  873. *
  874. * @return The length of this character buffer
  875. */
  876. public final int length() {
  877. return remaining();
  878. }
  879. /**
  880. * Reads the character at the given index relative to the current
  881. * position. </p>
  882. *
  883. * @param index
  884. * The index of the character to be read, relative to the position;
  885. * must be non-negative and smaller than <tt>remaining()</tt>
  886. *
  887. * @return The character at index
  888. * <tt>position()</tt> + index</tt>
  889. *
  890. * @throws IndexOutOfBoundsException
  891. * If the preconditions on <tt>index</tt> do not hold
  892. */
  893. public final char charAt(int index) {
  894. return get(position() + checkIndex(index, 1));
  895. }
  896. /**
  897. * Creates a new character buffer that represents the specified subsequence
  898. * of this buffer, relative to the current position.
  899. *
  900. * <p> The new buffer will share this buffer's content; that is, if the
  901. * content of this buffer is mutable then modifications to one buffer will
  902. * cause the other to be modified. The new buffer's capacity will be that
  903. * of this buffer, its position will be
  904. * <tt>position()</tt> + <tt>start</tt>, and its limit will be
  905. * <tt>position()</tt> + <tt>end</tt>. The new buffer will be
  906. * direct if, and only if, this buffer is direct, and it will be read-only
  907. * if, and only if, this buffer is read-only. </p>
  908. *
  909. * @param start
  910. * The index, relative to the current position, of the first
  911. * character in the subsequence; must be non-negative and no larger
  912. * than <tt>remaining()</tt>
  913. *
  914. * @param end
  915. * The index, relative to the current position, of the character
  916. * following the last character in the subsequence; must be no
  917. * smaller than <tt>start</tt> and no larger than
  918. * <tt>remaining()</tt>
  919. *
  920. * @return The new character buffer
  921. *
  922. * @throws IndexOutOfBoundsException
  923. * If the preconditions on <tt>start</tt> and <tt>end</tt>
  924. * do not hold
  925. */
  926. public abstract CharSequence subSequence(int start, int end);
  927. // -- Other byte stuff: Access to binary data --
  928. /**
  929. * Retrieves this buffer's byte order.
  930. *
  931. * <p> The byte order of a character buffer created by allocation or by
  932. * wrapping an existing <tt>char</tt> array is the {@link
  933. * ByteOrder#nativeOrder </code>native order<code>} of the underlying
  934. * hardware. The byte order of a character buffer created as a <a
  935. * href="ByteBuffer.html#view">view</a> of a byte buffer is that of the
  936. * byte buffer at the moment that the view is created. </p>
  937. *
  938. * @return This buffer's byte order
  939. */
  940. public abstract ByteOrder order();
  941. }