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