CharBuffer.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* CharBuffer.java --
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.nio;
  32. // GCJ LOCAL: Use RawData instead of gnu.classpath.Pointer
  33. import gnu.gcj.RawData;
  34. import java.io.IOException;
  35. /**
  36. * @since 1.4
  37. */
  38. public abstract class CharBuffer extends Buffer
  39. implements Comparable<CharBuffer>, CharSequence, Readable, Appendable
  40. {
  41. final int array_offset;
  42. final char[] backing_buffer;
  43. CharBuffer (int capacity, int limit, int position, int mark,
  44. RawData address, char[] backing_buffer, int array_offset)
  45. {
  46. super (capacity, limit, position, mark, address);
  47. this.backing_buffer = backing_buffer;
  48. this.array_offset = array_offset;
  49. }
  50. /**
  51. * Allocates a new <code>CharBuffer</code> object with a given capacity.
  52. */
  53. public static CharBuffer allocate (int capacity)
  54. {
  55. return new CharBufferImpl (capacity);
  56. }
  57. /**
  58. * Wraps a <code>char</code> array into a <code>CharBuffer</code>
  59. * object.
  60. *
  61. * @param array the array to wrap
  62. * @param offset the offset of the region in the array to wrap
  63. * @param length the length of the region in the array to wrap
  64. *
  65. * @return a new <code>CharBuffer</code> object
  66. *
  67. * @exception IndexOutOfBoundsException If the preconditions on the offset
  68. * and length parameters do not hold
  69. */
  70. public static final CharBuffer wrap(char[] array, int offset, int length)
  71. {
  72. return new CharBufferImpl(array, 0, array.length, offset + length, offset,
  73. -1, false);
  74. }
  75. /**
  76. * Wraps a character sequence into a <code>CharBuffer</code> object.
  77. *
  78. * @param seq the sequence to wrap
  79. *
  80. * @return a new <code>CharBuffer</code> object
  81. */
  82. public static final CharBuffer wrap(CharSequence seq)
  83. {
  84. return wrap(seq, 0, seq.length());
  85. }
  86. /**
  87. * Wraps a character sequence into a <code>CharBuffer</code> object.
  88. *
  89. * @param seq the sequence to wrap
  90. * @param start the index of the first character to wrap
  91. * @param end the index of the first character not to wrap
  92. *
  93. * @return a new <code>CharBuffer</code> object
  94. *
  95. * @exception IndexOutOfBoundsException If the preconditions on the offset
  96. * and length parameters do not hold
  97. */
  98. public static final CharBuffer wrap(CharSequence seq, int start, int end)
  99. {
  100. return new CharSequenceBuffer(seq, start, end);
  101. }
  102. /**
  103. * Wraps a <code>char</code> array into a <code>CharBuffer</code>
  104. * object.
  105. *
  106. * @param array the array to wrap
  107. *
  108. * @return a new <code>CharBuffer</code> object
  109. */
  110. public static final CharBuffer wrap(char[] array)
  111. {
  112. return wrap(array, 0, array.length);
  113. }
  114. /**
  115. * This method transfers <code>char</code>s from this buffer into the given
  116. * destination array. Before the transfer, it checks if there are fewer than
  117. * length <code>char</code>s remaining in this buffer.
  118. *
  119. * @param dst The destination array
  120. * @param offset The offset within the array of the first <code>char</code>
  121. * to be written; must be non-negative and no larger than dst.length.
  122. * @param length The maximum number of bytes to be written to the given array;
  123. * must be non-negative and no larger than dst.length - offset.
  124. *
  125. * @exception BufferUnderflowException If there are fewer than length
  126. * <code>char</code>s remaining in this buffer.
  127. * @exception IndexOutOfBoundsException If the preconditions on the offset
  128. * and length parameters do not hold.
  129. */
  130. public CharBuffer get (char[] dst, int offset, int length)
  131. {
  132. checkArraySize(dst.length, offset, length);
  133. checkForUnderflow(length);
  134. for (int i = offset; i < offset + length; i++)
  135. {
  136. dst [i] = get ();
  137. }
  138. return this;
  139. }
  140. /** @since 1.5 */
  141. public int read(CharBuffer buffer) throws IOException
  142. {
  143. // We want to call put(), so we don't manipulate the CharBuffer
  144. // directly.
  145. int rem = Math.min(buffer.remaining(), remaining());
  146. char[] buf = new char[rem];
  147. get(buf);
  148. buffer.put(buf);
  149. return rem;
  150. }
  151. /**
  152. * This method transfers <code>char</code>s from this buffer into the given
  153. * destination array.
  154. *
  155. * @param dst The byte array to write into.
  156. *
  157. * @exception BufferUnderflowException If there are fewer than dst.length
  158. * <code>char</code>s remaining in this buffer.
  159. */
  160. public CharBuffer get (char[] dst)
  161. {
  162. return get (dst, 0, dst.length);
  163. }
  164. /**
  165. * Writes the content of the the <code>CharBUFFER</code> src
  166. * into the buffer. Before the transfer, it checks if there is fewer than
  167. * <code>src.remaining()</code> space remaining in this buffer.
  168. *
  169. * @param src The source data.
  170. *
  171. * @exception BufferOverflowException If there is insufficient space in this
  172. * buffer for the remaining <code>char</code>s in the source buffer.
  173. * @exception IllegalArgumentException If the source buffer is this buffer.
  174. * @exception ReadOnlyBufferException If this buffer is read-only.
  175. */
  176. public CharBuffer put (CharBuffer src)
  177. {
  178. if (src == this)
  179. throw new IllegalArgumentException ();
  180. checkForOverflow(src.remaining());
  181. if (src.remaining () > 0)
  182. {
  183. char[] toPut = new char [src.remaining ()];
  184. src.get (toPut);
  185. put (toPut);
  186. }
  187. return this;
  188. }
  189. /**
  190. * Writes the content of the the <code>char array</code> src
  191. * into the buffer. Before the transfer, it checks if there is fewer than
  192. * length space remaining in this buffer.
  193. *
  194. * @param src The array to copy into the buffer.
  195. * @param offset The offset within the array of the first byte to be read;
  196. * must be non-negative and no larger than src.length.
  197. * @param length The number of bytes to be read from the given array;
  198. * must be non-negative and no larger than src.length - offset.
  199. *
  200. * @exception BufferOverflowException If there is insufficient space in this
  201. * buffer for the remaining <code>char</code>s in the source array.
  202. * @exception IndexOutOfBoundsException If the preconditions on the offset
  203. * and length parameters do not hold
  204. * @exception ReadOnlyBufferException If this buffer is read-only.
  205. */
  206. public CharBuffer put (char[] src, int offset, int length)
  207. {
  208. checkArraySize(src.length, offset, length);
  209. checkForOverflow(length);
  210. for (int i = offset; i < offset + length; i++)
  211. put (src [i]);
  212. return this;
  213. }
  214. /**
  215. * Writes the content of the the <code>char array</code> src
  216. * into the buffer.
  217. *
  218. * @param src The array to copy into the buffer.
  219. *
  220. * @exception BufferOverflowException If there is insufficient space in this
  221. * buffer for the remaining <code>char</code>s in the source array.
  222. * @exception ReadOnlyBufferException If this buffer is read-only.
  223. */
  224. public final CharBuffer put (char[] src)
  225. {
  226. return put (src, 0, src.length);
  227. }
  228. /**
  229. * Tells whether ot not this buffer is backed by an accessible
  230. * <code>char</code> array.
  231. */
  232. public final boolean hasArray ()
  233. {
  234. return (backing_buffer != null
  235. && !isReadOnly ());
  236. }
  237. /**
  238. * Returns the <code>char</code> array that backs this buffer.
  239. *
  240. * @exception ReadOnlyBufferException If this buffer is read-only.
  241. * @exception UnsupportedOperationException If this buffer is not backed
  242. * by an accessible array.
  243. */
  244. public final char[] array ()
  245. {
  246. if (backing_buffer == null)
  247. throw new UnsupportedOperationException ();
  248. checkIfReadOnly();
  249. return backing_buffer;
  250. }
  251. /**
  252. * Returns the offset within this buffer's backing array of the first element.
  253. *
  254. * @exception ReadOnlyBufferException If this buffer is read-only.
  255. * @exception UnsupportedOperationException If this buffer is not backed
  256. * by an accessible array.
  257. */
  258. public final int arrayOffset ()
  259. {
  260. if (backing_buffer == null)
  261. throw new UnsupportedOperationException ();
  262. checkIfReadOnly();
  263. return array_offset;
  264. }
  265. /**
  266. * Calculates a hash code for this buffer.
  267. *
  268. * This is done with int arithmetic,
  269. * where ** represents exponentiation, by this formula:<br>
  270. * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
  271. * (s[limit()-1]+30)*31**(limit()-1)</code>.
  272. * Where s is the buffer data. Note that the hashcode is dependent
  273. * on buffer content, and therefore is not useful if the buffer
  274. * content may change.
  275. */
  276. public int hashCode ()
  277. {
  278. int hashCode = get(position()) + 31;
  279. int multiplier = 1;
  280. for (int i = position() + 1; i < limit(); ++i)
  281. {
  282. multiplier *= 31;
  283. hashCode += (get(i) + 30)*multiplier;
  284. }
  285. return hashCode;
  286. }
  287. /**
  288. * Checks if this buffer is equal to obj.
  289. */
  290. public boolean equals (Object obj)
  291. {
  292. if (obj instanceof CharBuffer)
  293. {
  294. return compareTo ((CharBuffer) obj) == 0;
  295. }
  296. return false;
  297. }
  298. /**
  299. * Compares two <code>CharBuffer</code> objects.
  300. *
  301. * @exception ClassCastException If obj is not an object derived from
  302. * <code>CharBuffer</code>.
  303. */
  304. public int compareTo (CharBuffer other)
  305. {
  306. int num = Math.min(remaining(), other.remaining());
  307. int pos_this = position();
  308. int pos_other = other.position();
  309. for (int count = 0; count < num; count++)
  310. {
  311. char a = get(pos_this++);
  312. char b = other.get(pos_other++);
  313. if (a == b)
  314. continue;
  315. if (a < b)
  316. return -1;
  317. return 1;
  318. }
  319. return remaining() - other.remaining();
  320. }
  321. /**
  322. * Returns the byte order of this buffer.
  323. */
  324. public abstract ByteOrder order ();
  325. /**
  326. * Reads the <code>char</code> at this buffer's current position,
  327. * and then increments the position.
  328. *
  329. * @exception BufferUnderflowException If there are no remaining
  330. * <code>char</code>s in this buffer.
  331. */
  332. public abstract char get ();
  333. /**
  334. * Writes the <code>char</code> at this buffer's current position,
  335. * and then increments the position.
  336. *
  337. * @exception BufferOverflowException If there no remaining
  338. * <code>char</code>s in this buffer.
  339. * @exception ReadOnlyBufferException If this buffer is read-only.
  340. */
  341. public abstract CharBuffer put (char b);
  342. /**
  343. * Absolute get method.
  344. *
  345. * @exception IndexOutOfBoundsException If index is negative or not smaller
  346. * than the buffer's limit.
  347. */
  348. public abstract char get (int index);
  349. /**
  350. * Absolute put method.
  351. *
  352. * @exception IndexOutOfBoundsException If index is negative or not smaller
  353. * than the buffer's limit.
  354. * @exception ReadOnlyBufferException If this buffer is read-only.
  355. */
  356. public abstract CharBuffer put (int index, char b);
  357. /**
  358. * Compacts this buffer.
  359. *
  360. * @exception ReadOnlyBufferException If this buffer is read-only.
  361. */
  362. public abstract CharBuffer compact ();
  363. /**
  364. * Tells wether or not this buffer is direct.
  365. */
  366. public abstract boolean isDirect ();
  367. /**
  368. * Creates a new <code>CharBuffer</code> whose content is a shared
  369. * subsequence of this buffer's content.
  370. */
  371. public abstract CharBuffer slice ();
  372. /**
  373. * Creates a new <code>CharBuffer</code> that shares this buffer's
  374. * content.
  375. */
  376. public abstract CharBuffer duplicate ();
  377. /**
  378. * Creates a new read-only <code>CharBuffer</code> that shares this
  379. * buffer's content.
  380. */
  381. public abstract CharBuffer asReadOnlyBuffer ();
  382. /**
  383. * Returns the remaining content of the buffer as a string.
  384. */
  385. public String toString ()
  386. {
  387. if (hasArray ())
  388. return new String (array (), position (), length ());
  389. char[] buf = new char [length ()];
  390. int pos = position ();
  391. get (buf, 0, buf.length);
  392. position (pos);
  393. return new String (buf);
  394. }
  395. /**
  396. * Returns the length of the remaining chars in this buffer.
  397. */
  398. public final int length ()
  399. {
  400. return remaining ();
  401. }
  402. /**
  403. * Creates a new character buffer that represents the specified subsequence
  404. * of this buffer, relative to the current position.
  405. *
  406. * @exception IndexOutOfBoundsException If the preconditions on start and
  407. * end do not hold.
  408. */
  409. public abstract CharSequence subSequence (int start, int length);
  410. /**
  411. * Relative put method.
  412. *
  413. * @exception BufferOverflowException If there is insufficient space in this
  414. * buffer.
  415. * @exception IndexOutOfBoundsException If the preconditions on the start
  416. * and end parameters do not hold.
  417. * @exception ReadOnlyBufferException If this buffer is read-only.
  418. */
  419. public CharBuffer put (String str, int start, int length)
  420. {
  421. return put (str.toCharArray (), start, length);
  422. }
  423. /**
  424. * Relative put method.
  425. *
  426. * @exception BufferOverflowException If there is insufficient space in this
  427. * buffer.
  428. * @exception ReadOnlyBufferException If this buffer is read-only.
  429. */
  430. public final CharBuffer put (String str)
  431. {
  432. return put (str.toCharArray (), 0, str.length ());
  433. }
  434. /**
  435. * Returns the character at <code>position() + index</code>.
  436. *
  437. * @exception IndexOutOfBoundsException If index is negative not smaller than
  438. * <code>remaining()</code>.
  439. */
  440. public final char charAt (int index)
  441. {
  442. if (index < 0
  443. || index >= remaining ())
  444. throw new IndexOutOfBoundsException ();
  445. return get (position () + index);
  446. }
  447. /** @since 1.5 */
  448. public CharBuffer append(char c)
  449. {
  450. put(c);
  451. return this;
  452. }
  453. /** @since 1.5 */
  454. public CharBuffer append(CharSequence cs)
  455. {
  456. put(cs == null ? "null" : cs.toString());
  457. return this;
  458. }
  459. /** @since 1.5 */
  460. public CharBuffer append(CharSequence cs, int start, int end)
  461. {
  462. put(cs == null ? "null" : cs.subSequence(start, end).toString());
  463. return this;
  464. }
  465. }