1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 // -- This file was mechanically generated: Do not edit! -- // 28 // Android-note: This file is generated by ojluni/src/tools/gensrc_android.sh. 29 30 package java.nio; 31 32 import java.util.Objects; 33 import libcore.io.Memory; 34 35 /** 36 37 * A read/write HeapCharBuffer. 38 39 40 41 42 43 44 */ 45 // Android-changed: Make it final as no subclasses exist. 46 final class HeapCharBuffer 47 extends CharBuffer 48 { 49 // Android-removed: Removed unused constants. 50 /* 51 // Cached array base offset 52 private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(char[].class); 53 54 // Cached array index scale 55 private static final long ARRAY_INDEX_SCALE = UNSAFE.arrayIndexScale(char[].class); 56 */ 57 58 // For speed these fields are actually declared in X-Buffer; 59 // these declarations are here as documentation 60 /* 61 62 protected final char[] hb; 63 protected final int offset; 64 65 */ 66 // Android-removed: Removed MemorySegmentProxy to be supported yet. HeapCharBuffer(int cap, int lim)67 HeapCharBuffer(int cap, int lim) { // package-private 68 69 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 70 // super(-1, 0, lim, cap, new char[cap], 0); 71 this(cap, lim, false); 72 /* 73 hb = new char[cap]; 74 offset = 0; 75 */ 76 // Android-removed: buffer.address is only used by Direct*Buffer. 77 // this.address = ARRAY_BASE_OFFSET; 78 79 80 81 82 } 83 84 85 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. HeapCharBuffer(int cap, int lim, boolean isReadOnly)86 private HeapCharBuffer(int cap, int lim, boolean isReadOnly) { 87 super(-1, 0, lim, cap, new char[cap], 0); 88 this.isReadOnly = isReadOnly; 89 } 90 91 92 // Android-removed: Removed MemorySegmentProxy to be supported yet. HeapCharBuffer(char[] buf, int off, int len)93 HeapCharBuffer(char[] buf, int off, int len) { // package-private 94 95 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 96 // super(-1, off, off + len, buf.length, buf, 0); 97 this(buf, off, len, false); 98 /* 99 hb = buf; 100 offset = 0; 101 */ 102 // Android-removed: buffer.address is only used by Direct*Buffer. 103 // this.address = ARRAY_BASE_OFFSET; 104 105 106 107 108 } 109 110 111 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. HeapCharBuffer(char[] buf, int off, int len, boolean isReadOnly)112 private HeapCharBuffer(char[] buf, int off, int len, boolean isReadOnly) { 113 super(-1, off, off + len, buf.length, buf, 0); 114 this.isReadOnly = isReadOnly; 115 } 116 117 118 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 119 // Android-changed: Make the method private. 120 // Android-removed: Removed MemorySegmentProxy to be supported yet. HeapCharBuffer(char[] buf, int mark, int pos, int lim, int cap, int off, boolean isReadOnly)121 private HeapCharBuffer(char[] buf, 122 int mark, int pos, int lim, int cap, 123 int off, boolean isReadOnly) 124 { 125 126 super(mark, pos, lim, cap, buf, off); 127 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 128 this.isReadOnly = isReadOnly; 129 /* 130 hb = buf; 131 offset = off; 132 */ 133 // Android-removed: buffer.address is only used by Direct*Buffer. 134 // this.address = ARRAY_BASE_OFFSET + off * ARRAY_INDEX_SCALE; 135 136 137 138 139 } 140 slice()141 public CharBuffer slice() { 142 int pos = this.position(); 143 int lim = this.limit(); 144 int rem = (pos <= lim ? lim - pos : 0); 145 return new HeapCharBuffer(hb, 146 -1, 147 0, 148 rem, 149 rem, 150 // Android-removed: Removed MemorySegmentProxy not supported yet. 151 pos + offset, 152 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 153 isReadOnly); 154 } 155 156 @Override slice(int index, int length)157 public CharBuffer slice(int index, int length) { 158 Objects.checkFromIndexSize(index, length, limit()); 159 return new HeapCharBuffer(hb, 160 -1, 161 0, 162 length, 163 length, 164 // Android-removed: Removed MemorySegmentProxy not supported yet. 165 index + offset, 166 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 167 isReadOnly); 168 } 169 duplicate()170 public CharBuffer duplicate() { 171 return new HeapCharBuffer(hb, 172 this.markValue(), 173 this.position(), 174 this.limit(), 175 this.capacity(), 176 // Android-removed: Removed MemorySegmentProxy not supported yet. 177 offset, 178 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 179 isReadOnly); 180 } 181 asReadOnlyBuffer()182 public CharBuffer asReadOnlyBuffer() { 183 184 // Android-removed: Removed MemorySegmentProxy not supported yet. 185 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 186 /* 187 return new HeapCharBufferR(hb, 188 this.markValue(), 189 this.position(), 190 this.limit(), 191 this.capacity(), 192 offset, segment); 193 */ 194 return new HeapCharBuffer(hb, 195 this.markValue(), 196 this.position(), 197 this.limit(), 198 this.capacity(), 199 offset, 200 true /* isReadOnly */); 201 202 203 204 } 205 206 207 208 // Android-changed: Make it private as no subclasses exist. ix(int i)209 private int ix(int i) { 210 return i + offset; 211 } 212 213 214 215 216 217 218 219 @Override get()220 public char get() { 221 return hb[ix(nextGetIndex())]; 222 } 223 224 @Override get(int i)225 public char get(int i) { 226 return hb[ix(checkIndex(i))]; 227 } 228 229 230 @Override getUnchecked(int i)231 char getUnchecked(int i) { 232 return hb[ix(i)]; 233 } 234 235 236 @Override get(char[] dst, int offset, int length)237 public CharBuffer get(char[] dst, int offset, int length) { 238 checkScope(); 239 Objects.checkFromIndexSize(offset, length, dst.length); 240 int pos = position(); 241 if (length > limit() - pos) 242 throw new BufferUnderflowException(); 243 System.arraycopy(hb, ix(pos), dst, offset, length); 244 position(pos + length); 245 return this; 246 } 247 248 @Override get(int index, char[] dst, int offset, int length)249 public CharBuffer get(int index, char[] dst, int offset, int length) { 250 checkScope(); 251 Objects.checkFromIndexSize(index, length, limit()); 252 Objects.checkFromIndexSize(offset, length, dst.length); 253 System.arraycopy(hb, ix(index), dst, offset, length); 254 return this; 255 } 256 isDirect()257 public boolean isDirect() { 258 return false; 259 } 260 261 262 263 @Override isReadOnly()264 public boolean isReadOnly() { 265 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 266 return isReadOnly; 267 } 268 269 @Override put(char x)270 public CharBuffer put(char x) { 271 272 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. 273 throwIfReadOnly(); 274 hb[ix(nextPutIndex())] = x; 275 return this; 276 277 278 279 } 280 281 @Override put(int i, char x)282 public CharBuffer put(int i, char x) { 283 284 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. 285 throwIfReadOnly(); 286 hb[ix(checkIndex(i))] = x; 287 return this; 288 289 290 291 } 292 293 @Override put(char[] src, int offset, int length)294 public CharBuffer put(char[] src, int offset, int length) { 295 296 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. 297 throwIfReadOnly(); 298 checkScope(); 299 Objects.checkFromIndexSize(offset, length, src.length); 300 int pos = position(); 301 if (length > limit() - pos) 302 throw new BufferOverflowException(); 303 System.arraycopy(src, offset, hb, ix(pos), length); 304 position(pos + length); 305 return this; 306 307 308 309 } 310 311 @Override put(CharBuffer src)312 public CharBuffer put(CharBuffer src) { 313 314 checkScope(); 315 316 317 318 319 // Android-changed: Speed-up this operation if the src is a heap or direct buffer. 320 // super.put(src); 321 if (src == this) { 322 throw createSameBufferException(); 323 } 324 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. 325 throwIfReadOnly(); 326 if (src instanceof HeapCharBuffer sb) { 327 int n = sb.remaining(); 328 if (n > remaining()) 329 throw new BufferOverflowException(); 330 System.arraycopy(sb.hb, sb.ix(sb.position()), 331 hb, ix(position()), n); 332 sb.position(sb.position() + n); 333 position(position() + n); 334 } else if (src.isDirect()) { 335 int n = src.remaining(); 336 if (n > remaining()) 337 throw new BufferOverflowException(); 338 src.get(hb, ix(position()), n); 339 position(position() + n); 340 } else { 341 super.put(src); 342 } 343 344 return this; 345 346 347 348 } 349 350 @Override put(int index, CharBuffer src, int offset, int length)351 public CharBuffer put(int index, CharBuffer src, int offset, int length) { 352 353 checkScope(); 354 super.put(index, src, offset, length); 355 return this; 356 357 358 359 } 360 361 @Override put(int index, char[] src, int offset, int length)362 public CharBuffer put(int index, char[] src, int offset, int length) { 363 364 checkScope(); 365 Objects.checkFromIndexSize(index, length, limit()); 366 Objects.checkFromIndexSize(offset, length, src.length); 367 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. 368 throwIfReadOnly(); 369 System.arraycopy(src, offset, hb, ix(index), length); 370 return this; 371 372 373 374 } 375 376 377 378 @Override put(String src, int start, int end)379 public CharBuffer put(String src, int start, int end) { 380 381 checkScope(); 382 int length = end - start; 383 Objects.checkFromIndexSize(start, length, src.length()); 384 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. 385 throwIfReadOnly(); 386 int pos = position(); 387 int lim = limit(); 388 int rem = (pos <= lim) ? lim - pos : 0; 389 if (length > rem) 390 throw new BufferOverflowException(); 391 src.getChars(start, end, hb, ix(pos)); 392 position(pos + length); 393 return this; 394 395 396 397 } 398 399 400 401 @Override compact()402 public CharBuffer compact() { 403 404 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. 405 throwIfReadOnly(); 406 int pos = position(); 407 int lim = limit(); 408 assert (pos <= lim); 409 int rem = (pos <= lim ? lim - pos : 0); 410 System.arraycopy(hb, ix(pos), hb, ix(0), rem); 411 position(rem); 412 limit(capacity()); 413 discardMark(); 414 return this; 415 416 417 418 } 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 toString(int start, int end)1074 String toString(int start, int end) { // package-private 1075 try { 1076 return new String(hb, start + offset, end - start); 1077 } catch (StringIndexOutOfBoundsException x) { 1078 throw new IndexOutOfBoundsException(); 1079 } 1080 } 1081 1082 1083 // --- Methods to support CharSequence --- 1084 subSequence(int start, int end)1085 public CharBuffer subSequence(int start, int end) { 1086 int pos = position(); 1087 Objects.checkFromToIndex(start, end, limit() - pos); 1088 return new HeapCharBuffer(hb, 1089 -1, 1090 pos + start, 1091 pos + end, 1092 capacity(), 1093 // Android-removed: Removed MemorySegmentProxy not supported yet. 1094 offset, 1095 // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class. 1096 isReadOnly); 1097 } 1098 1099 1100 1101 1102 1103 order()1104 public ByteOrder order() { 1105 return ByteOrder.nativeOrder(); 1106 } 1107 1108 1109 charRegionOrder()1110 ByteOrder charRegionOrder() { 1111 return order(); 1112 } 1113 1114 1115 // Android-added: Merge the Read-only buffer class with this Read-Write buffer class. throwIfReadOnly()1116 private void throwIfReadOnly() { 1117 if (isReadOnly) { 1118 throw new ReadOnlyBufferException(); 1119 } 1120 } 1121 } 1122