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