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