1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.nio; 19 20import java.util.Arrays; 21import libcore.io.Memory; 22 23/** 24 * A buffer for bytes. 25 * <p> 26 * A byte buffer can be created in either one of the following ways: 27 * <ul> 28 * <li>{@link #allocate(int) Allocate} a new byte array and create a buffer 29 * based on it;</li> 30 * <li>{@link #allocateDirect(int) Allocate} a memory block and create a direct 31 * buffer based on it;</li> 32 * <li>{@link #wrap(byte[]) Wrap} an existing byte array to create a new 33 * buffer.</li> 34 * </ul> 35 * 36 */ 37public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer> { 38 /** 39 * The byte order of this buffer, default is {@code BIG_ENDIAN}. 40 */ 41 ByteOrder order = ByteOrder.BIG_ENDIAN; 42 43 /** 44 * Creates a byte buffer based on a newly allocated byte array. 45 * 46 * @param capacity 47 * the capacity of the new buffer 48 * @return the created byte buffer. 49 * @throws IllegalArgumentException 50 * if {@code capacity < 0}. 51 */ 52 public static ByteBuffer allocate(int capacity) { 53 if (capacity < 0) { 54 throw new IllegalArgumentException("capacity < 0: " + capacity); 55 } 56 return new ByteArrayBuffer(new byte[capacity]); 57 } 58 59 /** 60 * Creates a direct byte buffer based on a newly allocated memory block. 61 * 62 * @param capacity 63 * the capacity of the new buffer 64 * @return the created byte buffer. 65 * @throws IllegalArgumentException 66 * if {@code capacity < 0}. 67 */ 68 public static ByteBuffer allocateDirect(int capacity) { 69 if (capacity < 0) { 70 throw new IllegalArgumentException("capacity < 0: " + capacity); 71 } 72 return new DirectByteBuffer(MemoryBlock.allocate(capacity), capacity, 0, false, null); 73 } 74 75 /** 76 * Creates a new byte buffer by wrapping the given byte array. 77 * <p> 78 * Calling this method has the same effect as 79 * {@code wrap(array, 0, array.length)}. 80 * 81 * @param array 82 * the byte array which the new buffer will be based on 83 * @return the created byte buffer. 84 */ 85 public static ByteBuffer wrap(byte[] array) { 86 return new ByteArrayBuffer(array); 87 } 88 89 /** 90 * Creates a new byte buffer by wrapping the given byte array. 91 * <p> 92 * The new buffer's position will be {@code start}, limit will be 93 * {@code start + byteCount}, capacity will be the length of the array. 94 * 95 * @param array 96 * the byte array which the new buffer will be based on. 97 * @param start 98 * the start index, must not be negative and not greater than 99 * {@code array.length}. 100 * @param byteCount 101 * the length, must not be negative and not greater than 102 * {@code array.length - start}. 103 * @return the created byte buffer. 104 * @exception IndexOutOfBoundsException 105 * if either {@code start} or {@code byteCount} is invalid. 106 */ 107 public static ByteBuffer wrap(byte[] array, int start, int byteCount) { 108 Arrays.checkOffsetAndCount(array.length, start, byteCount); 109 ByteBuffer buf = new ByteArrayBuffer(array); 110 buf.position = start; 111 buf.limit = start + byteCount; 112 return buf; 113 } 114 115 ByteBuffer(int capacity, MemoryBlock block) { 116 super(0, capacity, block); 117 } 118 119 /** 120 * Returns the byte array which this buffer is based on, if there is one. 121 * 122 * @return the byte array which this buffer is based on. 123 * @exception ReadOnlyBufferException 124 * if this buffer is based on a read-only array. 125 * @exception UnsupportedOperationException 126 * if this buffer is not based on an array. 127 */ 128 @Override public final byte[] array() { 129 return protectedArray(); 130 } 131 132 /** 133 * Returns the offset of the byte array which this buffer is based on, if 134 * there is one. 135 * <p> 136 * The offset is the index of the array which corresponds to the zero 137 * position of the buffer. 138 * 139 * @return the offset of the byte array which this buffer is based on. 140 * @exception ReadOnlyBufferException 141 * if this buffer is based on a read-only array. 142 * @exception UnsupportedOperationException 143 * if this buffer is not based on an array. 144 */ 145 @Override public final int arrayOffset() { 146 return protectedArrayOffset(); 147 } 148 149 /** 150 * Returns a char buffer which is based on the remaining content of this 151 * byte buffer. 152 * <p> 153 * The new buffer's position is zero, its limit and capacity is the number 154 * of remaining bytes divided by two, and its mark is not set. The new 155 * buffer's read-only property and byte order are the same as this buffer's. 156 * The new buffer is direct if this byte buffer is direct. 157 * <p> 158 * The new buffer shares its content with this buffer, which means either 159 * buffer's change of content will be visible to the other. The two buffers' 160 * position, limit and mark are independent. 161 */ 162 public abstract CharBuffer asCharBuffer(); 163 164 /** 165 * Returns a double buffer which is based on the remaining content of this 166 * byte buffer. 167 * <p> 168 * The new buffer's position is zero, its limit and capacity is the number 169 * of remaining bytes divided by eight, and its mark is not set. The new 170 * buffer's read-only property and byte order are the same as this buffer's. 171 * The new buffer is direct if this byte buffer is direct. 172 * <p> 173 * The new buffer shares its content with this buffer, which means either 174 * buffer's change of content will be visible to the other. The two buffers' 175 * position, limit and mark are independent. 176 */ 177 public abstract DoubleBuffer asDoubleBuffer(); 178 179 /** 180 * Returns a float buffer which is based on the remaining content of this 181 * byte buffer. 182 * <p> 183 * The new buffer's position is zero, its limit and capacity is the number 184 * of remaining bytes divided by four, and its mark is not set. The new 185 * buffer's read-only property and byte order are the same as this buffer's. 186 * The new buffer is direct if this byte buffer is direct. 187 * <p> 188 * The new buffer shares its content with this buffer, which means either 189 * buffer's change of content will be visible to the other. The two buffers' 190 * position, limit and mark are independent. 191 */ 192 public abstract FloatBuffer asFloatBuffer(); 193 194 /** 195 * Returns a int buffer which is based on the remaining content of this byte 196 * buffer. 197 * <p> 198 * The new buffer's position is zero, its limit and capacity is the number 199 * of remaining bytes divided by four, and its mark is not set. The new 200 * buffer's read-only property and byte order are the same as this buffer's. 201 * The new buffer is direct if this byte buffer is direct. 202 * <p> 203 * The new buffer shares its content with this buffer, which means either 204 * buffer's change of content will be visible to the other. The two buffers' 205 * position, limit and mark are independent. 206 */ 207 public abstract IntBuffer asIntBuffer(); 208 209 /** 210 * Returns a long buffer which is based on the remaining content of this 211 * byte buffer. 212 * <p> 213 * The new buffer's position is zero, its limit and capacity is the number 214 * of remaining bytes divided by eight, and its mark is not set. The new 215 * buffer's read-only property and byte order are the same as this buffer's. 216 * The new buffer is direct if this byte buffer is direct. 217 * <p> 218 * The new buffer shares its content with this buffer, which means either 219 * buffer's change of content will be visible to the other. The two buffers' 220 * position, limit and mark are independent. 221 */ 222 public abstract LongBuffer asLongBuffer(); 223 224 /** 225 * Returns a read-only buffer that shares its content with this buffer. 226 * <p> 227 * The returned buffer is guaranteed to be a new instance, even if this 228 * buffer is read-only itself. The new buffer's position, limit, capacity 229 * and mark are the same as this buffer. 230 * <p> 231 * The new buffer shares its content with this buffer, which means this 232 * buffer's change of content will be visible to the new buffer. The two 233 * buffer's position, limit and mark are independent. 234 * 235 * @return a read-only version of this buffer. 236 */ 237 public abstract ByteBuffer asReadOnlyBuffer(); 238 239 /** 240 * Returns a short buffer which is based on the remaining content of this 241 * byte buffer. 242 * <p> 243 * The new buffer's position is zero, its limit and capacity is the number 244 * of remaining bytes divided by two, and its mark is not set. The new 245 * buffer's read-only property and byte order are the same as this buffer's. 246 * The new buffer is direct if this byte buffer is direct. 247 * <p> 248 * The new buffer shares its content with this buffer, which means either 249 * buffer's change of content will be visible to the other. The two buffers' 250 * position, limit and mark are independent. 251 */ 252 public abstract ShortBuffer asShortBuffer(); 253 254 /** 255 * Compacts this byte buffer. 256 * <p> 257 * The remaining bytes will be moved to the head of the 258 * buffer, starting from position zero. Then the position is set to 259 * {@code remaining()}; the limit is set to capacity; the mark is 260 * cleared. 261 * 262 * @return {@code this} 263 * @exception ReadOnlyBufferException 264 * if no changes may be made to the contents of this buffer. 265 */ 266 public abstract ByteBuffer compact(); 267 268 /** 269 * Compares the remaining bytes of this buffer to another byte buffer's 270 * remaining bytes. 271 * 272 * @param otherBuffer 273 * another byte buffer. 274 * @return a negative value if this is less than {@code other}; 0 if this 275 * equals to {@code other}; a positive value if this is greater 276 * than {@code other}. 277 * @exception ClassCastException 278 * if {@code other} is not a byte buffer. 279 */ 280 @Override public int compareTo(ByteBuffer otherBuffer) { 281 int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() 282 : otherBuffer.remaining(); 283 int thisPos = position; 284 int otherPos = otherBuffer.position; 285 byte thisByte, otherByte; 286 while (compareRemaining > 0) { 287 thisByte = get(thisPos); 288 otherByte = otherBuffer.get(otherPos); 289 if (thisByte != otherByte) { 290 return thisByte < otherByte ? -1 : 1; 291 } 292 thisPos++; 293 otherPos++; 294 compareRemaining--; 295 } 296 return remaining() - otherBuffer.remaining(); 297 } 298 299 /** 300 * Returns a duplicated buffer that shares its content with this buffer. 301 * <p> 302 * The duplicated buffer's position, limit, capacity and mark are the same 303 * as this buffer's. The duplicated buffer's read-only property is the same 304 * as this buffer's. 305 * 306 * <p>Note that <i>in contrast to all non-{@code byte} buffers</i>, 307 * byte order is not preserved in the duplicate, and is instead set to 308 * big-endian. 309 * 310 * <p>The new buffer shares its content with this buffer, which means either 311 * buffer's change of content will be visible to the other. The two buffers' 312 * position, limit and mark are independent. 313 */ 314 public abstract ByteBuffer duplicate(); 315 316 /** 317 * Checks whether this byte buffer is equal to another object. 318 * <p> 319 * If {@code other} is not a byte buffer then {@code false} is returned. Two 320 * byte buffers are equal if and only if their remaining bytes are exactly 321 * the same. Position, limit, capacity and mark are not considered. 322 * 323 * @param other 324 * the object to compare with this byte buffer. 325 * @return {@code true} if this byte buffer is equal to {@code other}, 326 * {@code false} otherwise. 327 */ 328 @Override 329 public boolean equals(Object other) { 330 if (!(other instanceof ByteBuffer)) { 331 return false; 332 } 333 ByteBuffer otherBuffer = (ByteBuffer) other; 334 335 if (remaining() != otherBuffer.remaining()) { 336 return false; 337 } 338 339 int myPosition = position; 340 int otherPosition = otherBuffer.position; 341 boolean equalSoFar = true; 342 while (equalSoFar && (myPosition < limit)) { 343 equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++); 344 } 345 346 return equalSoFar; 347 } 348 349 /** 350 * Returns the byte at the current position and increases the position by 1. 351 * 352 * @return the byte at the current position. 353 * @exception BufferUnderflowException 354 * if the position is equal or greater than limit. 355 */ 356 public abstract byte get(); 357 358 /** 359 * Reads bytes from the current position into the specified byte array and 360 * increases the position by the number of bytes read. 361 * <p> 362 * Calling this method has the same effect as 363 * {@code get(dst, 0, dst.length)}. 364 * 365 * @param dst 366 * the destination byte array. 367 * @return {@code this} 368 * @exception BufferUnderflowException 369 * if {@code dst.length} is greater than {@code remaining()}. 370 */ 371 public ByteBuffer get(byte[] dst) { 372 return get(dst, 0, dst.length); 373 } 374 375 /** 376 * Reads bytes from the current position into the specified byte array, 377 * starting at the specified offset, and increases the position by the 378 * number of bytes read. 379 * 380 * @param dst 381 * the target byte array. 382 * @param dstOffset 383 * the offset of the byte array, must not be negative and 384 * not greater than {@code dst.length}. 385 * @param byteCount 386 * the number of bytes to read, must not be negative and not 387 * greater than {@code dst.length - dstOffset} 388 * @return {@code this} 389 * @exception IndexOutOfBoundsException if {@code dstOffset < 0 || byteCount < 0} 390 * @exception BufferUnderflowException if {@code byteCount > remaining()} 391 */ 392 public ByteBuffer get(byte[] dst, int dstOffset, int byteCount) { 393 Arrays.checkOffsetAndCount(dst.length, dstOffset, byteCount); 394 if (byteCount > remaining()) { 395 throw new BufferUnderflowException(); 396 } 397 for (int i = dstOffset; i < dstOffset + byteCount; ++i) { 398 dst[i] = get(); 399 } 400 return this; 401 } 402 403 /** 404 * Returns the byte at the specified index and does not change the position. 405 * 406 * @param index 407 * the index, must not be negative and less than limit. 408 * @return the byte at the specified index. 409 * @exception IndexOutOfBoundsException 410 * if index is invalid. 411 */ 412 public abstract byte get(int index); 413 414 /** 415 * Returns the char at the current position and increases the position by 2. 416 * <p> 417 * The 2 bytes starting at the current position are composed into a char 418 * according to the current byte order and returned. 419 * 420 * @return the char at the current position. 421 * @exception BufferUnderflowException 422 * if the position is greater than {@code limit - 2}. 423 */ 424 public abstract char getChar(); 425 426 /** 427 * Returns the char at the specified index. 428 * <p> 429 * The 2 bytes starting from the specified index are composed into a char 430 * according to the current byte order and returned. The position is not 431 * changed. 432 * 433 * @param index 434 * the index, must not be negative and equal or less than 435 * {@code limit - 2}. 436 * @return the char at the specified index. 437 * @exception IndexOutOfBoundsException 438 * if {@code index} is invalid. 439 */ 440 public abstract char getChar(int index); 441 442 /** 443 * Returns the double at the current position and increases the position by 444 * 8. 445 * <p> 446 * The 8 bytes starting from the current position are composed into a double 447 * according to the current byte order and returned. 448 * 449 * @return the double at the current position. 450 * @exception BufferUnderflowException 451 * if the position is greater than {@code limit - 8}. 452 */ 453 public abstract double getDouble(); 454 455 /** 456 * Returns the double at the specified index. 457 * <p> 458 * The 8 bytes starting at the specified index are composed into a double 459 * according to the current byte order and returned. The position is not 460 * changed. 461 * 462 * @param index 463 * the index, must not be negative and equal or less than 464 * {@code limit - 8}. 465 * @return the double at the specified index. 466 * @exception IndexOutOfBoundsException 467 * if {@code index} is invalid. 468 */ 469 public abstract double getDouble(int index); 470 471 /** 472 * Returns the float at the current position and increases the position by 473 * 4. 474 * <p> 475 * The 4 bytes starting at the current position are composed into a float 476 * according to the current byte order and returned. 477 * 478 * @return the float at the current position. 479 * @exception BufferUnderflowException 480 * if the position is greater than {@code limit - 4}. 481 */ 482 public abstract float getFloat(); 483 484 /** 485 * Returns the float at the specified index. 486 * <p> 487 * The 4 bytes starting at the specified index are composed into a float 488 * according to the current byte order and returned. The position is not 489 * changed. 490 * 491 * @param index 492 * the index, must not be negative and equal or less than 493 * {@code limit - 4}. 494 * @return the float at the specified index. 495 * @exception IndexOutOfBoundsException 496 * if {@code index} is invalid. 497 */ 498 public abstract float getFloat(int index); 499 500 /** 501 * Returns the int at the current position and increases the position by 4. 502 * <p> 503 * The 4 bytes starting at the current position are composed into a int 504 * according to the current byte order and returned. 505 * 506 * @return the int at the current position. 507 * @exception BufferUnderflowException 508 * if the position is greater than {@code limit - 4}. 509 */ 510 public abstract int getInt(); 511 512 /** 513 * Returns the int at the specified index. 514 * <p> 515 * The 4 bytes starting at the specified index are composed into a int 516 * according to the current byte order and returned. The position is not 517 * changed. 518 * 519 * @param index 520 * the index, must not be negative and equal or less than 521 * {@code limit - 4}. 522 * @return the int at the specified index. 523 * @exception IndexOutOfBoundsException 524 * if {@code index} is invalid. 525 */ 526 public abstract int getInt(int index); 527 528 /** 529 * Returns the long at the current position and increases the position by 8. 530 * <p> 531 * The 8 bytes starting at the current position are composed into a long 532 * according to the current byte order and returned. 533 * 534 * @return the long at the current position. 535 * @exception BufferUnderflowException 536 * if the position is greater than {@code limit - 8}. 537 */ 538 public abstract long getLong(); 539 540 /** 541 * Returns the long at the specified index. 542 * <p> 543 * The 8 bytes starting at the specified index are composed into a long 544 * according to the current byte order and returned. The position is not 545 * changed. 546 * 547 * @param index 548 * the index, must not be negative and equal or less than 549 * {@code limit - 8}. 550 * @return the long at the specified index. 551 * @exception IndexOutOfBoundsException 552 * if {@code index} is invalid. 553 */ 554 public abstract long getLong(int index); 555 556 /** 557 * Returns the short at the current position and increases the position by 2. 558 * <p> 559 * The 2 bytes starting at the current position are composed into a short 560 * according to the current byte order and returned. 561 * 562 * @return the short at the current position. 563 * @exception BufferUnderflowException 564 * if the position is greater than {@code limit - 2}. 565 */ 566 public abstract short getShort(); 567 568 /** 569 * Returns the short at the specified index. 570 * <p> 571 * The 2 bytes starting at the specified index are composed into a short 572 * according to the current byte order and returned. The position is not 573 * changed. 574 * 575 * @param index 576 * the index, must not be negative and equal or less than 577 * {@code limit - 2}. 578 * @return the short at the specified index. 579 * @exception IndexOutOfBoundsException 580 * if {@code index} is invalid. 581 */ 582 public abstract short getShort(int index); 583 584 @Override public final boolean hasArray() { 585 return protectedHasArray(); 586 } 587 588 /** 589 * Calculates this buffer's hash code from the remaining chars. The 590 * position, limit, capacity and mark don't affect the hash code. 591 * 592 * @return the hash code calculated from the remaining bytes. 593 */ 594 @Override 595 public int hashCode() { 596 int myPosition = position; 597 int hash = 0; 598 while (myPosition < limit) { 599 hash = hash + get(myPosition++); 600 } 601 return hash; 602 } 603 604 /** 605 * Indicates whether this buffer is direct. 606 * 607 * @return {@code true} if this buffer is direct, {@code false} otherwise. 608 */ 609 @Override public abstract boolean isDirect(); 610 611 /** 612 * Returns the byte order used by this buffer when converting bytes from/to 613 * other primitive types. 614 * <p> 615 * The default byte order of byte buffer is always 616 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN} 617 * 618 * @return the byte order used by this buffer when converting bytes from/to 619 * other primitive types. 620 */ 621 public final ByteOrder order() { 622 return order; 623 } 624 625 /** 626 * Sets the byte order of this buffer. 627 * 628 * @param byteOrder 629 * the byte order to set. If {@code null} then the order 630 * will be {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}. 631 * @return {@code this} 632 * @see ByteOrder 633 */ 634 public final ByteBuffer order(ByteOrder byteOrder) { 635 if (byteOrder == null) { 636 byteOrder = ByteOrder.LITTLE_ENDIAN; 637 } 638 order = byteOrder; 639 return this; 640 } 641 642 /** 643 * Child class implements this method to realize {@code array()}. 644 * 645 * @see #array() 646 */ 647 abstract byte[] protectedArray(); 648 649 /** 650 * Child class implements this method to realize {@code arrayOffset()}. 651 * 652 * @see #arrayOffset() 653 */ 654 abstract int protectedArrayOffset(); 655 656 /** 657 * Child class implements this method to realize {@code hasArray()}. 658 * 659 * @see #hasArray() 660 */ 661 abstract boolean protectedHasArray(); 662 663 /** 664 * Writes the given byte to the current position and increases the position 665 * by 1. 666 * 667 * @param b 668 * the byte to write. 669 * @return {@code this} 670 * @exception BufferOverflowException 671 * if position is equal or greater than limit. 672 * @exception ReadOnlyBufferException 673 * if no changes may be made to the contents of this buffer. 674 */ 675 public abstract ByteBuffer put(byte b); 676 677 /** 678 * Writes bytes in the given byte array to the current position and 679 * increases the position by the number of bytes written. 680 * <p> 681 * Calling this method has the same effect as 682 * {@code put(src, 0, src.length)}. 683 * 684 * @param src 685 * the source byte array. 686 * @return {@code this} 687 * @exception BufferOverflowException 688 * if {@code remaining()} is less than {@code src.length}. 689 * @exception ReadOnlyBufferException 690 * if no changes may be made to the contents of this buffer. 691 */ 692 public final ByteBuffer put(byte[] src) { 693 return put(src, 0, src.length); 694 } 695 696 /** 697 * Writes bytes in the given byte array, starting from the specified offset, 698 * to the current position and increases the position by the number of bytes 699 * written. 700 * 701 * @param src 702 * the source byte array. 703 * @param srcOffset 704 * the offset of byte array, must not be negative and not greater 705 * than {@code src.length}. 706 * @param byteCount 707 * the number of bytes to write, must not be negative and not 708 * greater than {@code src.length - srcOffset}. 709 * @return {@code this} 710 * @exception BufferOverflowException 711 * if {@code remaining()} is less than {@code byteCount}. 712 * @exception IndexOutOfBoundsException 713 * if either {@code srcOffset} or {@code byteCount} is invalid. 714 * @exception ReadOnlyBufferException 715 * if no changes may be made to the contents of this buffer. 716 */ 717 public ByteBuffer put(byte[] src, int srcOffset, int byteCount) { 718 Arrays.checkOffsetAndCount(src.length, srcOffset, byteCount); 719 if (byteCount > remaining()) { 720 throw new BufferOverflowException(); 721 } 722 for (int i = srcOffset; i < srcOffset + byteCount; ++i) { 723 put(src[i]); 724 } 725 return this; 726 } 727 728 /** 729 * Writes all the remaining bytes of the {@code src} byte buffer to this 730 * buffer's current position, and increases both buffers' position by the 731 * number of bytes copied. 732 * 733 * @param src 734 * the source byte buffer. 735 * @return {@code this} 736 * @exception BufferOverflowException 737 * if {@code src.remaining()} is greater than this buffer's 738 * {@code remaining()}. 739 * @exception IllegalArgumentException 740 * if {@code src} is this buffer. 741 * @exception ReadOnlyBufferException 742 * if no changes may be made to the contents of this buffer. 743 */ 744 public ByteBuffer put(ByteBuffer src) { 745 if (isReadOnly()) { 746 throw new ReadOnlyBufferException(); 747 } 748 if (src == this) { 749 throw new IllegalArgumentException("src == this"); 750 } 751 int srcByteCount = src.remaining(); 752 if (srcByteCount > remaining()) { 753 throw new BufferOverflowException(); 754 } 755 756 Object srcObject = src.isDirect() ? src : NioUtils.unsafeArray(src); 757 int srcOffset = src.position(); 758 if (!src.isDirect()) { 759 srcOffset += NioUtils.unsafeArrayOffset(src); 760 } 761 762 ByteBuffer dst = this; 763 Object dstObject = dst.isDirect() ? dst : NioUtils.unsafeArray(dst); 764 int dstOffset = dst.position(); 765 if (!dst.isDirect()) { 766 dstOffset += NioUtils.unsafeArrayOffset(dst); 767 } 768 769 Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, srcByteCount); 770 src.position(src.limit()); 771 dst.position(dst.position() + srcByteCount); 772 773 return this; 774 } 775 776 /** 777 * Write a byte to the specified index of this buffer without changing the 778 * position. 779 * 780 * @param index 781 * the index, must not be negative and less than the limit. 782 * @param b 783 * the byte to write. 784 * @return {@code this} 785 * @exception IndexOutOfBoundsException 786 * if {@code index} is invalid. 787 * @exception ReadOnlyBufferException 788 * if no changes may be made to the contents of this buffer. 789 */ 790 public abstract ByteBuffer put(int index, byte b); 791 792 /** 793 * Writes the given char to the current position and increases the position 794 * by 2. 795 * <p> 796 * The char is converted to bytes using the current byte order. 797 * 798 * @param value 799 * the char to write. 800 * @return {@code this} 801 * @exception BufferOverflowException 802 * if position is greater than {@code limit - 2}. 803 * @exception ReadOnlyBufferException 804 * if no changes may be made to the contents of this buffer. 805 */ 806 public abstract ByteBuffer putChar(char value); 807 808 /** 809 * Writes the given char to the specified index of this buffer. 810 * <p> 811 * The char is converted to bytes using the current byte order. The position 812 * is not changed. 813 * 814 * @param index 815 * the index, must not be negative and equal or less than 816 * {@code limit - 2}. 817 * @param value 818 * the char to write. 819 * @return {@code this} 820 * @exception IndexOutOfBoundsException 821 * if {@code index} is invalid. 822 * @exception ReadOnlyBufferException 823 * if no changes may be made to the contents of this buffer. 824 */ 825 public abstract ByteBuffer putChar(int index, char value); 826 827 /** 828 * Writes the given double to the current position and increases the position 829 * by 8. 830 * <p> 831 * The double is converted to bytes using the current byte order. 832 * 833 * @param value 834 * the double to write. 835 * @return {@code this} 836 * @exception BufferOverflowException 837 * if position is greater than {@code limit - 8}. 838 * @exception ReadOnlyBufferException 839 * if no changes may be made to the contents of this buffer. 840 */ 841 public abstract ByteBuffer putDouble(double value); 842 843 /** 844 * Writes the given double to the specified index of this buffer. 845 * <p> 846 * The double is converted to bytes using the current byte order. The 847 * position is not changed. 848 * 849 * @param index 850 * the index, must not be negative and equal or less than 851 * {@code limit - 8}. 852 * @param value 853 * the double to write. 854 * @return {@code this} 855 * @exception IndexOutOfBoundsException 856 * if {@code index} is invalid. 857 * @exception ReadOnlyBufferException 858 * if no changes may be made to the contents of this buffer. 859 */ 860 public abstract ByteBuffer putDouble(int index, double value); 861 862 /** 863 * Writes the given float to the current position and increases the position 864 * by 4. 865 * <p> 866 * The float is converted to bytes using the current byte order. 867 * 868 * @param value 869 * the float to write. 870 * @return {@code this} 871 * @exception BufferOverflowException 872 * if position is greater than {@code limit - 4}. 873 * @exception ReadOnlyBufferException 874 * if no changes may be made to the contents of this buffer. 875 */ 876 public abstract ByteBuffer putFloat(float value); 877 878 /** 879 * Writes the given float to the specified index of this buffer. 880 * <p> 881 * The float is converted to bytes using the current byte order. The 882 * position is not changed. 883 * 884 * @param index 885 * the index, must not be negative and equal or less than 886 * {@code limit - 4}. 887 * @param value 888 * the float to write. 889 * @return {@code this} 890 * @exception IndexOutOfBoundsException 891 * if {@code index} is invalid. 892 * @exception ReadOnlyBufferException 893 * if no changes may be made to the contents of this buffer. 894 */ 895 public abstract ByteBuffer putFloat(int index, float value); 896 897 /** 898 * Writes the given int to the current position and increases the position by 899 * 4. 900 * <p> 901 * The int is converted to bytes using the current byte order. 902 * 903 * @param value 904 * the int to write. 905 * @return {@code this} 906 * @exception BufferOverflowException 907 * if position is greater than {@code limit - 4}. 908 * @exception ReadOnlyBufferException 909 * if no changes may be made to the contents of this buffer. 910 */ 911 public abstract ByteBuffer putInt(int value); 912 913 /** 914 * Writes the given int to the specified index of this buffer. 915 * <p> 916 * The int is converted to bytes using the current byte order. The position 917 * is not changed. 918 * 919 * @param index 920 * the index, must not be negative and equal or less than 921 * {@code limit - 4}. 922 * @param value 923 * the int to write. 924 * @return {@code this} 925 * @exception IndexOutOfBoundsException 926 * if {@code index} is invalid. 927 * @exception ReadOnlyBufferException 928 * if no changes may be made to the contents of this buffer. 929 */ 930 public abstract ByteBuffer putInt(int index, int value); 931 932 /** 933 * Writes the given long to the current position and increases the position 934 * by 8. 935 * <p> 936 * The long is converted to bytes using the current byte order. 937 * 938 * @param value 939 * the long to write. 940 * @return {@code this} 941 * @exception BufferOverflowException 942 * if position is greater than {@code limit - 8}. 943 * @exception ReadOnlyBufferException 944 * if no changes may be made to the contents of this buffer. 945 */ 946 public abstract ByteBuffer putLong(long value); 947 948 /** 949 * Writes the given long to the specified index of this buffer. 950 * <p> 951 * The long is converted to bytes using the current byte order. The position 952 * is not changed. 953 * 954 * @param index 955 * the index, must not be negative and equal or less than 956 * {@code limit - 8}. 957 * @param value 958 * the long to write. 959 * @return {@code this} 960 * @exception IndexOutOfBoundsException 961 * if {@code index} is invalid. 962 * @exception ReadOnlyBufferException 963 * if no changes may be made to the contents of this buffer. 964 */ 965 public abstract ByteBuffer putLong(int index, long value); 966 967 /** 968 * Writes the given short to the current position and increases the position 969 * by 2. 970 * <p> 971 * The short is converted to bytes using the current byte order. 972 * 973 * @param value 974 * the short to write. 975 * @return {@code this} 976 * @exception BufferOverflowException 977 * if position is greater than {@code limit - 2}. 978 * @exception ReadOnlyBufferException 979 * if no changes may be made to the contents of this buffer. 980 */ 981 public abstract ByteBuffer putShort(short value); 982 983 /** 984 * Writes the given short to the specified index of this buffer. 985 * <p> 986 * The short is converted to bytes using the current byte order. The 987 * position is not changed. 988 * 989 * @param index 990 * the index, must not be negative and equal or less than 991 * {@code limit - 2}. 992 * @param value 993 * the short to write. 994 * @return {@code this} 995 * @exception IndexOutOfBoundsException 996 * if {@code index} is invalid. 997 * @exception ReadOnlyBufferException 998 * if no changes may be made to the contents of this buffer. 999 */ 1000 public abstract ByteBuffer putShort(int index, short value); 1001 1002 /** 1003 * Returns a sliced buffer that shares its content with this buffer. 1004 * <p> 1005 * The sliced buffer's capacity will be this buffer's 1006 * {@code remaining()}, and it's zero position will correspond to 1007 * this buffer's current position. The new buffer's position will be 0, 1008 * limit will be its capacity, and its mark is cleared. The new buffer's 1009 * read-only property and byte order are the same as this buffer's. 1010 * <p> 1011 * The new buffer shares its content with this buffer, which means either 1012 * buffer's change of content will be visible to the other. The two buffers' 1013 * position, limit and mark are independent. 1014 */ 1015 public abstract ByteBuffer slice(); 1016} 1017