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 /** 32 * A short buffer. 33 * 34 * <p> This class defines four categories of operations upon 35 * short buffers: 36 * 37 * <ul> 38 * 39 * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and 40 * {@link #put(short) </code><i>put</i><code>} methods that read and write 41 * single shorts; </p></li> 42 * 43 * <li><p> Relative {@link #get(short[]) </code><i>bulk get</i><code>} 44 * methods that transfer contiguous sequences of shorts from this buffer 45 * into an array; and</p></li> 46 * 47 * <li><p> Relative {@link #put(short[]) </code><i>bulk put</i><code>} 48 * methods that transfer contiguous sequences of shorts from a 49 * short array or some other short 50 * buffer into this buffer; and </p></li> 51 * 52 * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link 53 * #duplicate </code>duplicating<code>}, and {@link #slice 54 * </code>slicing<code>} a short buffer. </p></li> 55 * 56 * </ul> 57 * 58 * <p> Short buffers can be created either by {@link #allocate 59 * </code><i>allocation</i><code>}, which allocates space for the buffer's 60 * 61 * content, by {@link #wrap(short[]) </code><i>wrapping</i><code>} an existing 62 * short array into a buffer, or by creating a 63 * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer. 64 * 65 * <p> Like a byte buffer, a short buffer is either <a 66 * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A 67 * short buffer created via the <tt>wrap</tt> methods of this class will 68 * be non-direct. A short buffer created as a view of a byte buffer will 69 * be direct if, and only if, the byte buffer itself is direct. Whether or not 70 * a short buffer is direct may be determined by invoking the {@link 71 * #isDirect isDirect} method. </p> 72 * 73 * <p> Methods in this class that do not otherwise have a value to return are 74 * specified to return the buffer upon which they are invoked. This allows 75 * method invocations to be chained. 76 * 77 * @author Mark Reinhold 78 * @author JSR-51 Expert Group 79 * @since 1.4 80 */ 81 82 public abstract class ShortBuffer 83 extends Buffer 84 implements Comparable<ShortBuffer> { 85 86 // These fields are declared here rather than in Heap-X-Buffer in order to 87 // reduce the number of virtual method invocations needed to access these 88 // values, which is especially costly when coding small buffers. 89 // 90 final short[] hb; // Non-null only for heap buffers 91 final int offset; 92 boolean isReadOnly; // Valid only for heap buffers 93 94 // Creates a new buffer with the given mark, position, limit, capacity, 95 // backing array, and array offset 96 // ShortBuffer(int mark, int pos, int lim, int cap, short[] hb, int offset)97 ShortBuffer(int mark, int pos, int lim, int cap, // package-private 98 short[] hb, int offset) { 99 super(mark, pos, lim, cap, 1); 100 this.hb = hb; 101 this.offset = offset; 102 } 103 104 // Creates a new buffer with the given mark, position, limit, and capacity 105 // ShortBuffer(int mark, int pos, int lim, int cap)106 ShortBuffer(int mark, int pos, int lim, int cap) { // package-private 107 this(mark, pos, lim, cap, null, 0); 108 } 109 110 111 /** 112 * Allocates a new short buffer. 113 * 114 * <p> The new buffer's position will be zero, its limit will be its 115 * capacity, its mark will be undefined, and each of its elements will be 116 * initialized to zero. It will have a {@link #array 117 * </code>backing array<code>}, and its {@link #arrayOffset </code>array 118 * offset<code>} will be zero. 119 * 120 * @param capacity The new buffer's capacity, in shorts 121 * @return The new short buffer 122 * @throws IllegalArgumentException If the <tt>capacity</tt> is a negative integer 123 */ allocate(int capacity)124 public static ShortBuffer allocate(int capacity) { 125 if (capacity < 0) 126 throw new IllegalArgumentException(); 127 return new HeapShortBuffer(capacity, capacity); 128 } 129 130 /** 131 * Wraps a short array into a buffer. 132 * 133 * <p> The new buffer will be backed by the given short array; 134 * that is, modifications to the buffer will cause the array to be modified 135 * and vice versa. The new buffer's capacity will be 136 * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit 137 * will be <tt>offset + length</tt>, and its mark will be undefined. Its 138 * {@link #array </code>backing array<code>} will be the given array, and 139 * its {@link #arrayOffset </code>array offset<code>} will be zero. </p> 140 * 141 * @param array The array that will back the new buffer 142 * @param offset The offset of the subarray to be used; must be non-negative and 143 * no larger than <tt>array.length</tt>. The new buffer's position 144 * will be set to this value. 145 * @param length The length of the subarray to be used; 146 * must be non-negative and no larger than 147 * <tt>array.length - offset</tt>. 148 * The new buffer's limit will be set to <tt>offset + length</tt>. 149 * @return The new short buffer 150 * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and 151 * <tt>length</tt> 152 * parameters do not hold 153 */ wrap(short[] array, int offset, int length)154 public static ShortBuffer wrap(short[] array, 155 int offset, int length) { 156 try { 157 return new HeapShortBuffer(array, offset, length); 158 } catch (IllegalArgumentException x) { 159 throw new IndexOutOfBoundsException(); 160 } 161 } 162 163 /** 164 * Wraps a short array into a buffer. 165 * 166 * <p> The new buffer will be backed by the given short array; 167 * that is, modifications to the buffer will cause the array to be modified 168 * and vice versa. The new buffer's capacity and limit will be 169 * <tt>array.length</tt>, its position will be zero, and its mark will be 170 * undefined. Its {@link #array </code>backing array<code>} will be the 171 * given array, and its {@link #arrayOffset </code>array offset<code>} will 172 * be zero. </p> 173 * 174 * @param array The array that will back this buffer 175 * @return The new short buffer 176 */ wrap(short[] array)177 public static ShortBuffer wrap(short[] array) { 178 return wrap(array, 0, array.length); 179 } 180 181 182 /** 183 * Creates a new short buffer whose content is a shared subsequence of 184 * this buffer's content. 185 * 186 * <p> The content of the new buffer will start at this buffer's current 187 * position. Changes to this buffer's content will be visible in the new 188 * buffer, and vice versa; the two buffers' position, limit, and mark 189 * values will be independent. 190 * 191 * <p> The new buffer's position will be zero, its capacity and its limit 192 * will be the number of shorts remaining in this buffer, and its mark 193 * will be undefined. The new buffer will be direct if, and only if, this 194 * buffer is direct, and it will be read-only if, and only if, this buffer 195 * is read-only. </p> 196 * 197 * @return The new short buffer 198 */ slice()199 public abstract ShortBuffer slice(); 200 201 /** 202 * Creates a new short buffer that shares this buffer's content. 203 * 204 * <p> The content of the new buffer will be that of this buffer. Changes 205 * to this buffer's content will be visible in the new buffer, and vice 206 * versa; the two buffers' position, limit, and mark values will be 207 * independent. 208 * 209 * <p> The new buffer's capacity, limit, position, and mark values will be 210 * identical to those of this buffer. The new buffer will be direct if, 211 * and only if, this buffer is direct, and it will be read-only if, and 212 * only if, this buffer is read-only. </p> 213 * 214 * @return The new short buffer 215 */ duplicate()216 public abstract ShortBuffer duplicate(); 217 218 /** 219 * Creates a new, read-only short buffer that shares this buffer's 220 * content. 221 * 222 * <p> The content of the new buffer will be that of this buffer. Changes 223 * to this buffer's content will be visible in the new buffer; the new 224 * buffer itself, however, will be read-only and will not allow the shared 225 * content to be modified. The two buffers' position, limit, and mark 226 * values will be independent. 227 * 228 * <p> The new buffer's capacity, limit, position, and mark values will be 229 * identical to those of this buffer. 230 * 231 * <p> If this buffer is itself read-only then this method behaves in 232 * exactly the same way as the {@link #duplicate duplicate} method. </p> 233 * 234 * @return The new, read-only short buffer 235 */ asReadOnlyBuffer()236 public abstract ShortBuffer asReadOnlyBuffer(); 237 238 239 // -- Singleton get/put methods -- 240 241 /** 242 * Relative <i>get</i> method. Reads the short at this buffer's 243 * current position, and then increments the position. </p> 244 * 245 * @return The short at the buffer's current position 246 * @throws BufferUnderflowException If the buffer's current position is not smaller than its 247 * limit 248 */ get()249 public abstract short get(); 250 251 /** 252 * Relative <i>put</i> method <i>(optional operation)</i>. 253 * 254 * <p> Writes the given short into this buffer at the current 255 * position, and then increments the position. </p> 256 * 257 * @param s The short to be written 258 * @return This buffer 259 * @throws BufferOverflowException If this buffer's current position is not smaller than its 260 * limit 261 * @throws ReadOnlyBufferException If this buffer is read-only 262 */ put(short s)263 public abstract ShortBuffer put(short s); 264 265 /** 266 * Absolute <i>get</i> method. Reads the short at the given 267 * index. </p> 268 * 269 * @param index The index from which the short will be read 270 * @return The short at the given index 271 * @throws IndexOutOfBoundsException If <tt>index</tt> is negative 272 * or not smaller than the buffer's limit 273 */ get(int index)274 public abstract short get(int index); 275 276 /** 277 * Absolute <i>put</i> method <i>(optional operation)</i>. 278 * 279 * <p> Writes the given short into this buffer at the given 280 * index. </p> 281 * 282 * @param index The index at which the short will be written 283 * @param s The short value to be written 284 * @return This buffer 285 * @throws IndexOutOfBoundsException If <tt>index</tt> is negative 286 * or not smaller than the buffer's limit 287 * @throws ReadOnlyBufferException If this buffer is read-only 288 */ put(int index, short s)289 public abstract ShortBuffer put(int index, short s); 290 291 292 // -- Bulk get operations -- 293 294 /** 295 * Relative bulk <i>get</i> method. 296 * 297 * <p> This method transfers shorts from this buffer into the given 298 * destination array. If there are fewer shorts remaining in the 299 * buffer than are required to satisfy the request, that is, if 300 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 301 * shorts are transferred and a {@link BufferUnderflowException} is 302 * thrown. 303 * 304 * <p> Otherwise, this method copies <tt>length</tt> shorts from this 305 * buffer into the given array, starting at the current position of this 306 * buffer and at the given offset in the array. The position of this 307 * buffer is then incremented by <tt>length</tt>. 308 * 309 * <p> In other words, an invocation of this method of the form 310 * <tt>src.get(dst, off, len)</tt> has exactly the same effect as 311 * the loop 312 * 313 * <pre> 314 * for (int i = off; i < off + len; i++) 315 * dst[i] = src.get(); </pre> 316 * 317 * except that it first checks that there are sufficient shorts in 318 * this buffer and it is potentially much more efficient. </p> 319 * 320 * @param dst The array into which shorts are to be written 321 * @param offset The offset within the array of the first short to be 322 * written; must be non-negative and no larger than 323 * <tt>dst.length</tt> 324 * @param length The maximum number of shorts to be written to the given 325 * array; must be non-negative and no larger than 326 * <tt>dst.length - offset</tt> 327 * @return This buffer 328 * @throws BufferUnderflowException If there are fewer than <tt>length</tt> shorts 329 * remaining in this buffer 330 * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and 331 * <tt>length</tt> 332 * parameters do not hold 333 */ get(short[] dst, int offset, int length)334 public ShortBuffer get(short[] dst, int offset, int length) { 335 checkBounds(offset, length, dst.length); 336 if (length > remaining()) 337 throw new BufferUnderflowException(); 338 int end = offset + length; 339 for (int i = offset; i < end; i++) 340 dst[i] = get(); 341 return this; 342 } 343 344 /** 345 * Relative bulk <i>get</i> method. 346 * 347 * <p> This method transfers shorts from this buffer into the given 348 * destination array. An invocation of this method of the form 349 * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation 350 * 351 * <pre> 352 * src.get(a, 0, a.length) </pre> 353 * 354 * @return This buffer 355 * @throws BufferUnderflowException If there are fewer than <tt>length</tt> shorts 356 * remaining in this buffer 357 */ get(short[] dst)358 public ShortBuffer get(short[] dst) { 359 return get(dst, 0, dst.length); 360 } 361 362 363 // -- Bulk put operations -- 364 365 /** 366 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 367 * 368 * <p> This method transfers the shorts remaining in the given source 369 * buffer into this buffer. If there are more shorts remaining in the 370 * source buffer than in this buffer, that is, if 371 * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>, 372 * then no shorts are transferred and a {@link 373 * BufferOverflowException} is thrown. 374 * 375 * <p> Otherwise, this method copies 376 * <i>n</i> = <tt>src.remaining()</tt> shorts from the given 377 * buffer into this buffer, starting at each buffer's current position. 378 * The positions of both buffers are then incremented by <i>n</i>. 379 * 380 * <p> In other words, an invocation of this method of the form 381 * <tt>dst.put(src)</tt> has exactly the same effect as the loop 382 * 383 * <pre> 384 * while (src.hasRemaining()) 385 * dst.put(src.get()); </pre> 386 * 387 * except that it first checks that there is sufficient space in this 388 * buffer and it is potentially much more efficient. </p> 389 * 390 * @param src The source buffer from which shorts are to be read; 391 * must not be this buffer 392 * @return This buffer 393 * @throws BufferOverflowException If there is insufficient space in this buffer 394 * for the remaining shorts in the source buffer 395 * @throws IllegalArgumentException If the source buffer is this buffer 396 * @throws ReadOnlyBufferException If this buffer is read-only 397 */ put(ShortBuffer src)398 public ShortBuffer put(ShortBuffer src) { 399 if (src == this) 400 throw new IllegalArgumentException(); 401 int n = src.remaining(); 402 if (n > remaining()) 403 throw new BufferOverflowException(); 404 for (int i = 0; i < n; i++) 405 put(src.get()); 406 return this; 407 } 408 409 /** 410 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 411 * 412 * <p> This method transfers shorts into this buffer from the given 413 * source array. If there are more shorts to be copied from the array 414 * than remain in this buffer, that is, if 415 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 416 * shorts are transferred and a {@link BufferOverflowException} is 417 * thrown. 418 * 419 * <p> Otherwise, this method copies <tt>length</tt> shorts from the 420 * given array into this buffer, starting at the given offset in the array 421 * and at the current position of this buffer. The position of this buffer 422 * is then incremented by <tt>length</tt>. 423 * 424 * <p> In other words, an invocation of this method of the form 425 * <tt>dst.put(src, off, len)</tt> has exactly the same effect as 426 * the loop 427 * 428 * <pre> 429 * for (int i = off; i < off + len; i++) 430 * dst.put(a[i]); </pre> 431 * 432 * except that it first checks that there is sufficient space in this 433 * buffer and it is potentially much more efficient. </p> 434 * 435 * @param src The array from which shorts are to be read 436 * @param offset The offset within the array of the first short to be read; 437 * must be non-negative and no larger than <tt>array.length</tt> 438 * @param length The number of shorts to be read from the given array; 439 * must be non-negative and no larger than 440 * <tt>array.length - offset</tt> 441 * @return This buffer 442 * @throws BufferOverflowException If there is insufficient space in this buffer 443 * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and 444 * <tt>length</tt> 445 * parameters do not hold 446 * @throws ReadOnlyBufferException If this buffer is read-only 447 */ put(short[] src, int offset, int length)448 public ShortBuffer put(short[] src, int offset, int length) { 449 checkBounds(offset, length, src.length); 450 if (length > remaining()) 451 throw new BufferOverflowException(); 452 int end = offset + length; 453 for (int i = offset; i < end; i++) 454 this.put(src[i]); 455 return this; 456 } 457 458 /** 459 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 460 * 461 * <p> This method transfers the entire content of the given source 462 * short array into this buffer. An invocation of this method of the 463 * form <tt>dst.put(a)</tt> behaves in exactly the same way as the 464 * invocation 465 * 466 * <pre> 467 * dst.put(a, 0, a.length) </pre> 468 * 469 * @return This buffer 470 * @throws BufferOverflowException If there is insufficient space in this buffer 471 * @throws ReadOnlyBufferException If this buffer is read-only 472 */ put(short[] src)473 public final ShortBuffer put(short[] src) { 474 return put(src, 0, src.length); 475 } 476 477 478 // -- Other stuff -- 479 480 /** 481 * Tells whether or not this buffer is backed by an accessible short 482 * array. 483 * 484 * <p> If this method returns <tt>true</tt> then the {@link #array() array} 485 * and {@link #arrayOffset() arrayOffset} methods may safely be invoked. 486 * </p> 487 * 488 * @return <tt>true</tt> if, and only if, this buffer 489 * is backed by an array and is not read-only 490 */ hasArray()491 public final boolean hasArray() { 492 return (hb != null) && !isReadOnly; 493 } 494 495 /** 496 * Returns the short array that backs this 497 * buffer <i>(optional operation)</i>. 498 * 499 * <p> Modifications to this buffer's content will cause the returned 500 * array's content to be modified, and vice versa. 501 * 502 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 503 * method in order to ensure that this buffer has an accessible backing 504 * array. </p> 505 * 506 * @return The array that backs this buffer 507 * @throws ReadOnlyBufferException If this buffer is backed by an array but is read-only 508 * @throws UnsupportedOperationException If this buffer is not backed by an accessible array 509 */ array()510 public final short[] array() { 511 if (hb == null) 512 throw new UnsupportedOperationException(); 513 if (isReadOnly) 514 throw new ReadOnlyBufferException(); 515 return hb; 516 } 517 518 /** 519 * Returns the offset within this buffer's backing array of the first 520 * element of the buffer <i>(optional operation)</i>. 521 * 522 * <p> If this buffer is backed by an array then buffer position <i>p</i> 523 * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>. 524 * 525 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 526 * method in order to ensure that this buffer has an accessible backing 527 * array. </p> 528 * 529 * @return The offset within this buffer's array 530 * of the first element of the buffer 531 * @throws ReadOnlyBufferException If this buffer is backed by an array but is read-only 532 * @throws UnsupportedOperationException If this buffer is not backed by an accessible array 533 */ arrayOffset()534 public final int arrayOffset() { 535 if (hb == null) 536 throw new UnsupportedOperationException(); 537 if (isReadOnly) 538 throw new ReadOnlyBufferException(); 539 return offset; 540 } 541 542 /** 543 * Compacts this buffer <i>(optional operation)</i>. 544 * 545 * <p> The shorts between the buffer's current position and its limit, 546 * if any, are copied to the beginning of the buffer. That is, the 547 * short at index <i>p</i> = <tt>position()</tt> is copied 548 * to index zero, the short at index <i>p</i> + 1 is copied 549 * to index one, and so forth until the short at index 550 * <tt>limit()</tt> - 1 is copied to index 551 * <i>n</i> = <tt>limit()</tt> - <tt>1</tt> - <i>p</i>. 552 * The buffer's position is then set to <i>n+1</i> and its limit is set to 553 * its capacity. The mark, if defined, is discarded. 554 * 555 * <p> The buffer's position is set to the number of shorts copied, 556 * rather than to zero, so that an invocation of this method can be 557 * followed immediately by an invocation of another relative <i>put</i> 558 * method. </p> 559 * 560 * @return This buffer 561 * @throws ReadOnlyBufferException If this buffer is read-only 562 */ compact()563 public abstract ShortBuffer compact(); 564 565 /** 566 * Tells whether or not this short buffer is direct. </p> 567 * 568 * @return <tt>true</tt> if, and only if, this buffer is direct 569 */ isDirect()570 public abstract boolean isDirect(); 571 572 573 /** 574 * Returns a string summarizing the state of this buffer. </p> 575 * 576 * @return A summary string 577 */ toString()578 public String toString() { 579 StringBuffer sb = new StringBuffer(); 580 sb.append(getClass().getName()); 581 sb.append("[pos="); 582 sb.append(position()); 583 sb.append(" lim="); 584 sb.append(limit()); 585 sb.append(" cap="); 586 sb.append(capacity()); 587 sb.append("]"); 588 return sb.toString(); 589 } 590 591 592 /** 593 * Returns the current hash code of this buffer. 594 * 595 * <p> The hash code of a short buffer depends only upon its remaining 596 * elements; that is, upon the elements from <tt>position()</tt> up to, and 597 * including, the element at <tt>limit()</tt> - <tt>1</tt>. 598 * 599 * <p> Because buffer hash codes are content-dependent, it is inadvisable 600 * to use buffers as keys in hash maps or similar data structures unless it 601 * is known that their contents will not change. </p> 602 * 603 * @return The current hash code of this buffer 604 */ hashCode()605 public int hashCode() { 606 int h = 1; 607 int p = position(); 608 for (int i = limit() - 1; i >= p; i--) 609 h = 31 * h + (int) get(i); 610 return h; 611 } 612 613 /** 614 * Tells whether or not this buffer is equal to another object. 615 * 616 * <p> Two short buffers are equal if, and only if, 617 * 618 * <p><ol> 619 * 620 * <li><p> They have the same element type, </p></li> 621 * 622 * <li><p> They have the same number of remaining elements, and 623 * </p></li> 624 * 625 * <li><p> The two sequences of remaining elements, considered 626 * independently of their starting positions, are pointwise equal. 627 * 628 * 629 * 630 * 631 * 632 * 633 * 634 * </p></li> 635 * 636 * </ol> 637 * 638 * <p> A short buffer is not equal to any other type of object. </p> 639 * 640 * @param ob The object to which this buffer is to be compared 641 * @return <tt>true</tt> if, and only if, this buffer is equal to the 642 * given object 643 */ equals(Object ob)644 public boolean equals(Object ob) { 645 if (this == ob) 646 return true; 647 if (!(ob instanceof ShortBuffer)) 648 return false; 649 ShortBuffer that = (ShortBuffer) ob; 650 if (this.remaining() != that.remaining()) 651 return false; 652 int p = this.position(); 653 for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) 654 if (!equals(this.get(i), that.get(j))) 655 return false; 656 return true; 657 } 658 equals(short x, short y)659 private static boolean equals(short x, short y) { 660 661 662 return x == y; 663 664 } 665 666 /** 667 * Compares this buffer to another. 668 * 669 * <p> Two short buffers are compared by comparing their sequences of 670 * remaining elements lexicographically, without regard to the starting 671 * position of each sequence within its corresponding buffer. 672 * 673 * 674 * 675 * 676 * 677 * 678 * 679 * 680 * Pairs of {@code short} elements are compared as if by invoking 681 * {@link Short#compare(short, short)}. 682 * 683 * 684 * <p> A short buffer is not comparable to any other type of object. 685 * 686 * @return A negative integer, zero, or a positive integer as this buffer 687 * is less than, equal to, or greater than the given buffer 688 */ compareTo(ShortBuffer that)689 public int compareTo(ShortBuffer that) { 690 int n = this.position() + Math.min(this.remaining(), that.remaining()); 691 for (int i = this.position(), j = that.position(); i < n; i++, j++) { 692 int cmp = compare(this.get(i), that.get(j)); 693 if (cmp != 0) 694 return cmp; 695 } 696 return this.remaining() - that.remaining(); 697 } 698 compare(short x, short y)699 private static int compare(short x, short y) { 700 701 702 return Short.compare(x, y); 703 704 } 705 706 // -- Other char stuff -- 707 708 709 // -- Other byte stuff: Access to binary data -- 710 711 712 /** 713 * Retrieves this buffer's byte order. 714 * 715 * <p> The byte order of a short buffer created by allocation or by 716 * wrapping an existing <tt>short</tt> array is the {@link 717 * ByteOrder#nativeOrder </code>native order<code>} of the underlying 718 * hardware. The byte order of a short buffer created as a <a 719 * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the 720 * byte buffer at the moment that the view is created. </p> 721 * 722 * @return This buffer's byte order 723 */ order()724 public abstract ByteOrder order(); 725 726 727 } 728