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