1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 2000, 2013, 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 127 /** 128 * Allocates a new int buffer. 129 * 130 * <p> The new buffer's position will be zero, its limit will be its 131 * capacity, its mark will be undefined, and each of its elements will be 132 * initialized to zero. It will have a {@link #array backing array}, 133 * and its {@link #arrayOffset array offset} will be zero. 134 * 135 * @param capacity 136 * The new buffer's capacity, in ints 137 * 138 * @return The new int buffer 139 * 140 * @throws IllegalArgumentException 141 * If the <tt>capacity</tt> is a negative integer 142 */ allocate(int capacity)143 public static IntBuffer allocate(int capacity) { 144 if (capacity < 0) 145 throw new IllegalArgumentException(); 146 return new HeapIntBuffer(capacity, capacity); 147 } 148 149 /** 150 * Wraps an int array into a buffer. 151 * 152 * <p> The new buffer will be backed by the given int array; 153 * that is, modifications to the buffer will cause the array to be modified 154 * and vice versa. The new buffer's capacity will be 155 * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit 156 * will be <tt>offset + length</tt>, and its mark will be undefined. Its 157 * {@link #array backing array} will be the given array, and 158 * its {@link #arrayOffset array offset} will be zero. </p> 159 * 160 * @param array 161 * The array that will back the new buffer 162 * 163 * @param offset 164 * The offset of the subarray to be used; must be non-negative and 165 * no larger than <tt>array.length</tt>. The new buffer's position 166 * will be set to this value. 167 * 168 * @param length 169 * The length of the subarray to be used; 170 * must be non-negative and no larger than 171 * <tt>array.length - offset</tt>. 172 * The new buffer's limit will be set to <tt>offset + length</tt>. 173 * 174 * @return The new int buffer 175 * 176 * @throws IndexOutOfBoundsException 177 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 178 * parameters do not hold 179 */ wrap(int[] array, int offset, int length)180 public static IntBuffer wrap(int[] array, 181 int offset, int length) 182 { 183 try { 184 return new HeapIntBuffer(array, offset, length); 185 } catch (IllegalArgumentException x) { 186 throw new IndexOutOfBoundsException(); 187 } 188 } 189 190 /** 191 * Wraps an int array into a buffer. 192 * 193 * <p> The new buffer will be backed by the given int array; 194 * that is, modifications to the buffer will cause the array to be modified 195 * and vice versa. The new buffer's capacity and limit will be 196 * <tt>array.length</tt>, its position will be zero, and its mark will be 197 * undefined. Its {@link #array backing array} will be the 198 * given array, and its {@link #arrayOffset array offset>} will 199 * be zero. </p> 200 * 201 * @param array 202 * The array that will back this buffer 203 * 204 * @return The new int buffer 205 */ wrap(int[] array)206 public static IntBuffer wrap(int[] array) { 207 return wrap(array, 0, array.length); 208 } 209 210 211 /** 212 * Creates a new int buffer whose content is a shared subsequence of 213 * this buffer's content. 214 * 215 * <p> The content of the new buffer will start at this buffer's current 216 * position. Changes to this buffer's content will be visible in the new 217 * buffer, and vice versa; the two buffers' position, limit, and mark 218 * values will be independent. 219 * 220 * <p> The new buffer's position will be zero, its capacity and its limit 221 * will be the number of ints remaining in this buffer, and its mark 222 * will be undefined. The new buffer will be direct if, and only if, this 223 * buffer is direct, and it will be read-only if, and only if, this buffer 224 * is read-only. </p> 225 * 226 * @return The new int buffer 227 */ slice()228 public abstract IntBuffer slice(); 229 230 /** 231 * Creates a new int buffer that shares this buffer's content. 232 * 233 * <p> The content of the new buffer will be that of this buffer. Changes 234 * to this buffer's content will be visible in the new buffer, and vice 235 * versa; the two buffers' position, limit, and mark values will be 236 * independent. 237 * 238 * <p> The new buffer's capacity, limit, position, and mark values will be 239 * identical to those of this buffer. The new buffer will be direct if, 240 * and only if, this buffer is direct, and it will be read-only if, and 241 * only if, this buffer is read-only. </p> 242 * 243 * @return The new int buffer 244 */ duplicate()245 public abstract IntBuffer duplicate(); 246 247 /** 248 * Creates a new, read-only int buffer that shares this buffer's 249 * content. 250 * 251 * <p> The content of the new buffer will be that of this buffer. Changes 252 * to this buffer's content will be visible in the new buffer; the new 253 * buffer itself, however, will be read-only and will not allow the shared 254 * content to be modified. The two buffers' position, limit, and mark 255 * values will be independent. 256 * 257 * <p> The new buffer's capacity, limit, position, and mark values will be 258 * identical to those of this buffer. 259 * 260 * <p> If this buffer is itself read-only then this method behaves in 261 * exactly the same way as the {@link #duplicate duplicate} method. </p> 262 * 263 * @return The new, read-only int buffer 264 */ asReadOnlyBuffer()265 public abstract IntBuffer asReadOnlyBuffer(); 266 267 268 // -- Singleton get/put methods -- 269 270 /** 271 * Relative <i>get</i> method. Reads the int at this buffer's 272 * current position, and then increments the position. 273 * 274 * @return The int at the buffer's current position 275 * 276 * @throws BufferUnderflowException 277 * If the buffer's current position is not smaller than its limit 278 */ get()279 public abstract int get(); 280 281 /** 282 * Relative <i>put</i> method <i>(optional operation)</i>. 283 * 284 * <p> Writes the given int into this buffer at the current 285 * position, and then increments the position. </p> 286 * 287 * @param i 288 * The int to be written 289 * 290 * @return This buffer 291 * 292 * @throws BufferOverflowException 293 * If this buffer's current position is not smaller than its limit 294 * 295 * @throws ReadOnlyBufferException 296 * If this buffer is read-only 297 */ put(int i)298 public abstract IntBuffer put(int i); 299 300 /** 301 * Absolute <i>get</i> method. Reads the int at the given 302 * index. 303 * 304 * @param index 305 * The index from which the int will be read 306 * 307 * @return The int at the given index 308 * 309 * @throws IndexOutOfBoundsException 310 * If <tt>index</tt> is negative 311 * or not smaller than the buffer's limit 312 */ get(int index)313 public abstract int get(int index); 314 315 /** 316 * Absolute <i>put</i> method <i>(optional operation)</i>. 317 * 318 * <p> Writes the given int into this buffer at the given 319 * index. </p> 320 * 321 * @param index 322 * The index at which the int will be written 323 * 324 * @param i 325 * The int value to be written 326 * 327 * @return This buffer 328 * 329 * @throws IndexOutOfBoundsException 330 * If <tt>index</tt> is negative 331 * or not smaller than the buffer's limit 332 * 333 * @throws ReadOnlyBufferException 334 * If this buffer is read-only 335 */ put(int index, int i)336 public abstract IntBuffer put(int index, int i); 337 338 339 // -- Bulk get operations -- 340 341 /** 342 * Relative bulk <i>get</i> method. 343 * 344 * <p> This method transfers ints from this buffer into the given 345 * destination array. If there are fewer ints remaining in the 346 * buffer than are required to satisfy the request, that is, if 347 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 348 * ints are transferred and a {@link BufferUnderflowException} is 349 * thrown. 350 * 351 * <p> Otherwise, this method copies <tt>length</tt> ints from this 352 * buffer into the given array, starting at the current position of this 353 * buffer and at the given offset in the array. The position of this 354 * buffer is then incremented by <tt>length</tt>. 355 * 356 * <p> In other words, an invocation of this method of the form 357 * <tt>src.get(dst, off, len)</tt> has exactly the same effect as 358 * the loop 359 * 360 * <pre>{@code 361 * for (int i = off; i < off + len; i++) 362 * dst[i] = src.get(); 363 * }</pre> 364 * 365 * except that it first checks that there are sufficient ints in 366 * this buffer and it is potentially much more efficient. 367 * 368 * @param dst 369 * The array into which ints are to be written 370 * 371 * @param offset 372 * The offset within the array of the first int to be 373 * written; must be non-negative and no larger than 374 * <tt>dst.length</tt> 375 * 376 * @param length 377 * The maximum number of ints to be written to the given 378 * array; must be non-negative and no larger than 379 * <tt>dst.length - offset</tt> 380 * 381 * @return This buffer 382 * 383 * @throws BufferUnderflowException 384 * If there are fewer than <tt>length</tt> ints 385 * remaining in this buffer 386 * 387 * @throws IndexOutOfBoundsException 388 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 389 * parameters do not hold 390 */ get(int[] dst, int offset, int length)391 public IntBuffer get(int[] dst, int offset, int length) { 392 checkBounds(offset, length, dst.length); 393 if (length > remaining()) 394 throw new BufferUnderflowException(); 395 int end = offset + length; 396 for (int i = offset; i < end; i++) 397 dst[i] = get(); 398 return this; 399 } 400 401 /** 402 * Relative bulk <i>get</i> method. 403 * 404 * <p> This method transfers ints from this buffer into the given 405 * destination array. An invocation of this method of the form 406 * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation 407 * 408 * <pre> 409 * src.get(a, 0, a.length) </pre> 410 * 411 * @param dst 412 * The destination array 413 * 414 * @return This buffer 415 * 416 * @throws BufferUnderflowException 417 * If there are fewer than <tt>length</tt> ints 418 * remaining in this buffer 419 */ get(int[] dst)420 public IntBuffer get(int[] dst) { 421 return get(dst, 0, dst.length); 422 } 423 424 425 // -- Bulk put operations -- 426 427 /** 428 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 429 * 430 * <p> This method transfers the ints remaining in the given source 431 * buffer into this buffer. If there are more ints remaining in the 432 * source buffer than in this buffer, that is, if 433 * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>, 434 * then no ints are transferred and a {@link 435 * BufferOverflowException} is thrown. 436 * 437 * <p> Otherwise, this method copies 438 * <i>n</i> = <tt>src.remaining()</tt> ints from the given 439 * buffer into this buffer, starting at each buffer's current position. 440 * The positions of both buffers are then incremented by <i>n</i>. 441 * 442 * <p> In other words, an invocation of this method of the form 443 * <tt>dst.put(src)</tt> has exactly the same effect as the loop 444 * 445 * <pre> 446 * while (src.hasRemaining()) 447 * dst.put(src.get()); </pre> 448 * 449 * except that it first checks that there is sufficient space in this 450 * buffer and it is potentially much more efficient. 451 * 452 * @param src 453 * The source buffer from which ints are to be read; 454 * must not be this buffer 455 * 456 * @return This buffer 457 * 458 * @throws BufferOverflowException 459 * If there is insufficient space in this buffer 460 * for the remaining ints in the source buffer 461 * 462 * @throws IllegalArgumentException 463 * If the source buffer is this buffer 464 * 465 * @throws ReadOnlyBufferException 466 * If this buffer is read-only 467 */ put(IntBuffer src)468 public IntBuffer put(IntBuffer src) { 469 if (src == this) 470 throw new IllegalArgumentException(); 471 if (isReadOnly()) 472 throw new ReadOnlyBufferException(); 473 int n = src.remaining(); 474 if (n > remaining()) 475 throw new BufferOverflowException(); 476 for (int i = 0; i < n; i++) 477 put(src.get()); 478 return this; 479 } 480 481 /** 482 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 483 * 484 * <p> This method transfers ints into this buffer from the given 485 * source array. If there are more ints to be copied from the array 486 * than remain in this buffer, that is, if 487 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 488 * ints are transferred and a {@link BufferOverflowException} is 489 * thrown. 490 * 491 * <p> Otherwise, this method copies <tt>length</tt> ints from the 492 * given array into this buffer, starting at the given offset in the array 493 * and at the current position of this buffer. The position of this buffer 494 * is then incremented by <tt>length</tt>. 495 * 496 * <p> In other words, an invocation of this method of the form 497 * <tt>dst.put(src, off, len)</tt> has exactly the same effect as 498 * the loop 499 * 500 * <pre>{@code 501 * for (int i = off; i < off + len; i++) 502 * dst.put(a[i]); 503 * }</pre> 504 * 505 * except that it first checks that there is sufficient space in this 506 * buffer and it is potentially much more efficient. 507 * 508 * @param src 509 * The array from which ints are to be read 510 * 511 * @param offset 512 * The offset within the array of the first int to be read; 513 * must be non-negative and no larger than <tt>array.length</tt> 514 * 515 * @param length 516 * The number of ints to be read from the given array; 517 * must be non-negative and no larger than 518 * <tt>array.length - offset</tt> 519 * 520 * @return This buffer 521 * 522 * @throws BufferOverflowException 523 * If there is insufficient space in this buffer 524 * 525 * @throws IndexOutOfBoundsException 526 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 527 * parameters do not hold 528 * 529 * @throws ReadOnlyBufferException 530 * If this buffer is read-only 531 */ put(int[] src, int offset, int length)532 public IntBuffer put(int[] src, int offset, int length) { 533 checkBounds(offset, length, src.length); 534 if (length > remaining()) 535 throw new BufferOverflowException(); 536 int end = offset + length; 537 for (int i = offset; i < end; i++) 538 this.put(src[i]); 539 return this; 540 } 541 542 /** 543 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 544 * 545 * <p> This method transfers the entire content of the given source 546 * int array into this buffer. An invocation of this method of the 547 * form <tt>dst.put(a)</tt> behaves in exactly the same way as the 548 * invocation 549 * 550 * <pre> 551 * dst.put(a, 0, a.length) </pre> 552 * 553 * @param src 554 * The source array 555 * 556 * @return This buffer 557 * 558 * @throws BufferOverflowException 559 * If there is insufficient space in this buffer 560 * 561 * @throws ReadOnlyBufferException 562 * If this buffer is read-only 563 */ put(int[] src)564 public final IntBuffer put(int[] src) { 565 return put(src, 0, src.length); 566 } 567 568 569 // -- Other stuff -- 570 571 /** 572 * Tells whether or not this buffer is backed by an accessible int 573 * array. 574 * 575 * <p> If this method returns <tt>true</tt> then the {@link #array() array} 576 * and {@link #arrayOffset() arrayOffset} methods may safely be invoked. 577 * </p> 578 * 579 * @return <tt>true</tt> if, and only if, this buffer 580 * is backed by an array and is not read-only 581 */ hasArray()582 public final boolean hasArray() { 583 return (hb != null) && !isReadOnly; 584 } 585 586 /** 587 * Returns the int array that backs this 588 * buffer <i>(optional operation)</i>. 589 * 590 * <p> Modifications to this buffer's content will cause the returned 591 * array's content to be modified, and vice versa. 592 * 593 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 594 * method in order to ensure that this buffer has an accessible backing 595 * array. </p> 596 * 597 * @return The array that backs this buffer 598 * 599 * @throws ReadOnlyBufferException 600 * If this buffer is backed by an array but is read-only 601 * 602 * @throws UnsupportedOperationException 603 * If this buffer is not backed by an accessible array 604 */ array()605 public final int[] array() { 606 if (hb == null) 607 throw new UnsupportedOperationException(); 608 if (isReadOnly) 609 throw new ReadOnlyBufferException(); 610 return hb; 611 } 612 613 /** 614 * Returns the offset within this buffer's backing array of the first 615 * element of the buffer <i>(optional operation)</i>. 616 * 617 * <p> If this buffer is backed by an array then buffer position <i>p</i> 618 * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>. 619 * 620 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 621 * method in order to ensure that this buffer has an accessible backing 622 * array. </p> 623 * 624 * @return The offset within this buffer's array 625 * of the first element of the buffer 626 * 627 * @throws ReadOnlyBufferException 628 * If this buffer is backed by an array but is read-only 629 * 630 * @throws UnsupportedOperationException 631 * If this buffer is not backed by an accessible array 632 */ arrayOffset()633 public final int arrayOffset() { 634 if (hb == null) 635 throw new UnsupportedOperationException(); 636 if (isReadOnly) 637 throw new ReadOnlyBufferException(); 638 return offset; 639 } 640 641 // BEGIN Android-added: covariant overloads of *Buffer methods that return this. 642 @CovariantReturnType(returnType = IntBuffer.class, presentAfter = 28) 643 @Override position(int newPosition)644 public Buffer position(int newPosition) { 645 return super.position(newPosition); 646 } 647 648 @CovariantReturnType(returnType = IntBuffer.class, presentAfter = 28) 649 @Override limit(int newLimit)650 public Buffer limit(int newLimit) { 651 return super.limit(newLimit); 652 } 653 654 @CovariantReturnType(returnType = IntBuffer.class, presentAfter = 28) 655 @Override mark()656 public Buffer mark() { 657 return super.mark(); 658 } 659 660 @CovariantReturnType(returnType = IntBuffer.class, presentAfter = 28) 661 @Override reset()662 public Buffer reset() { 663 return super.reset(); 664 } 665 666 @CovariantReturnType(returnType = IntBuffer.class, presentAfter = 28) 667 @Override clear()668 public Buffer clear() { 669 return super.clear(); 670 } 671 672 @CovariantReturnType(returnType = IntBuffer.class, presentAfter = 28) 673 @Override flip()674 public Buffer flip() { 675 return super.flip(); 676 } 677 678 @CovariantReturnType(returnType = IntBuffer.class, presentAfter = 28) 679 @Override rewind()680 public Buffer rewind() { 681 return super.rewind(); 682 } 683 // END Android-added: covariant overloads of *Buffer methods that return this. 684 685 /** 686 * Compacts this buffer <i>(optional operation)</i>. 687 * 688 * <p> The ints between the buffer's current position and its limit, 689 * if any, are copied to the beginning of the buffer. That is, the 690 * int at index <i>p</i> = <tt>position()</tt> is copied 691 * to index zero, the int at index <i>p</i> + 1 is copied 692 * to index one, and so forth until the int at index 693 * <tt>limit()</tt> - 1 is copied to index 694 * <i>n</i> = <tt>limit()</tt> - <tt>1</tt> - <i>p</i>. 695 * The buffer's position is then set to <i>n+1</i> and its limit is set to 696 * its capacity. The mark, if defined, is discarded. 697 * 698 * <p> The buffer's position is set to the number of ints copied, 699 * rather than to zero, so that an invocation of this method can be 700 * followed immediately by an invocation of another relative <i>put</i> 701 * method. </p> 702 * 703 704 * 705 * @return This buffer 706 * 707 * @throws ReadOnlyBufferException 708 * If this buffer is read-only 709 */ compact()710 public abstract IntBuffer compact(); 711 712 /** 713 * Tells whether or not this int buffer is direct. 714 * 715 * @return <tt>true</tt> if, and only if, this buffer is direct 716 */ isDirect()717 public abstract boolean isDirect(); 718 719 720 /** 721 * Returns a string summarizing the state of this buffer. 722 * 723 * @return A summary string 724 */ toString()725 public String toString() { 726 StringBuffer sb = new StringBuffer(); 727 sb.append(getClass().getName()); 728 sb.append("[pos="); 729 sb.append(position()); 730 sb.append(" lim="); 731 sb.append(limit()); 732 sb.append(" cap="); 733 sb.append(capacity()); 734 sb.append("]"); 735 return sb.toString(); 736 } 737 738 739 /** 740 * Returns the current hash code of this buffer. 741 * 742 * <p> The hash code of a int buffer depends only upon its remaining 743 * elements; that is, upon the elements from <tt>position()</tt> up to, and 744 * including, the element at <tt>limit()</tt> - <tt>1</tt>. 745 * 746 * <p> Because buffer hash codes are content-dependent, it is inadvisable 747 * to use buffers as keys in hash maps or similar data structures unless it 748 * is known that their contents will not change. </p> 749 * 750 * @return The current hash code of this buffer 751 */ hashCode()752 public int hashCode() { 753 int h = 1; 754 int p = position(); 755 for (int i = limit() - 1; i >= p; i--) 756 h = 31 * h + (int) get(i); 757 return h; 758 } 759 760 /** 761 * Tells whether or not this buffer is equal to another object. 762 * 763 * <p> Two int buffers are equal if, and only if, 764 * 765 * <ol> 766 * 767 * <li><p> They have the same element type, </p></li> 768 * 769 * <li><p> They have the same number of remaining elements, and 770 * </p></li> 771 * 772 * <li><p> The two sequences of remaining elements, considered 773 * independently of their starting positions, are pointwise equal. 774 * 775 * 776 * 777 * 778 * 779 * 780 * 781 * </p></li> 782 * 783 * </ol> 784 * 785 * <p> A int buffer is not equal to any other type of object. </p> 786 * 787 * @param ob The object to which this buffer is to be compared 788 * 789 * @return <tt>true</tt> if, and only if, this buffer is equal to the 790 * given object 791 */ equals(Object ob)792 public boolean equals(Object ob) { 793 if (this == ob) 794 return true; 795 if (!(ob instanceof IntBuffer)) 796 return false; 797 IntBuffer that = (IntBuffer)ob; 798 if (this.remaining() != that.remaining()) 799 return false; 800 int p = this.position(); 801 for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) 802 if (!equals(this.get(i), that.get(j))) 803 return false; 804 return true; 805 } 806 equals(int x, int y)807 private static boolean equals(int x, int y) { 808 809 810 return x == y; 811 812 } 813 814 /** 815 * Compares this buffer to another. 816 * 817 * <p> Two int buffers are compared by comparing their sequences of 818 * remaining elements lexicographically, without regard to the starting 819 * position of each sequence within its corresponding buffer. 820 * 821 * 822 * 823 * 824 * 825 * 826 * 827 * 828 * Pairs of {@code int} elements are compared as if by invoking 829 * {@link Integer#compare(int,int)}. 830 831 * 832 * <p> A int buffer is not comparable to any other type of object. 833 * 834 * @return A negative integer, zero, or a positive integer as this buffer 835 * is less than, equal to, or greater than the given buffer 836 */ compareTo(IntBuffer that)837 public int compareTo(IntBuffer that) { 838 int n = this.position() + Math.min(this.remaining(), that.remaining()); 839 for (int i = this.position(), j = that.position(); i < n; i++, j++) { 840 int cmp = compare(this.get(i), that.get(j)); 841 if (cmp != 0) 842 return cmp; 843 } 844 return this.remaining() - that.remaining(); 845 } 846 compare(int x, int y)847 private static int compare(int x, int y) { 848 849 850 return Integer.compare(x, y); 851 852 } 853 854 // -- Other char stuff -- 855 856 857 // -- Other byte stuff: Access to binary data -- 858 859 860 /** 861 * Retrieves this buffer's byte order. 862 * 863 * <p> The byte order of an int buffer created by allocation or by 864 * wrapping an existing <tt>int</tt> array is the {@link 865 * ByteOrder#nativeOrder native order} of the underlying 866 * hardware. The byte order of an int buffer created as a <a 867 * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the 868 * byte buffer at the moment that the view is created. </p> 869 * 870 * @return This buffer's byte order 871 */ order()872 public abstract ByteOrder order(); 873 874 875 } 876