1 /* 2 * Copyright 2013 The Netty Project 3 * 4 * The Netty Project licenses this file to you under the Apache License, 5 * version 2.0 (the "License"); you may not use this file except in compliance 6 * with the License. You may obtain a copy of the License at: 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" ~BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations 14 * under the License. 15 */ 16 17 module hunt.net.buffer.WrappedByteBuf; 18 19 import hunt.net.buffer.ByteBuf; 20 import hunt.net.buffer.ByteBufAllocator; 21 import hunt.net.buffer.ByteBufUtil; 22 import hunt.net.buffer.ByteProcessor; 23 24 import hunt.Byte; 25 import hunt.io.ByteBuffer; 26 import hunt.Exceptions; 27 import hunt.net.Exceptions; 28 import hunt.stream.Common; 29 import hunt.util.StringBuilder; 30 31 // import io.netty.util.ByteProcessor; 32 // import io.netty.util.internal.StringUtil; 33 34 // import java.io.IOException; 35 // import java.io.InputStream; 36 // import java.io.OutputStream; 37 // import java.nio.ByteBuffer; 38 // import java.nio.ByteOrder; 39 // import java.nio.channels.FileChannel; 40 // import java.nio.channels.GatheringByteChannel; 41 // import java.nio.channels.ScatteringByteChannel; 42 // import java.nio.charset.Charset; 43 44 /** 45 * Wraps another {@link ByteBuf}. 46 * 47 * It's important that the {@link #readerIndex()} and {@link #writerIndex()} will not do any adjustments on the 48 * indices on the fly because of internal optimizations made by {@link ByteBufUtil#writeAscii(ByteBuf, CharSequence)} 49 * and {@link ByteBufUtil#writeUtf8(ByteBuf, CharSequence)}. 50 */ 51 class WrappedByteBuf : ByteBuf { 52 53 protected ByteBuf buf; 54 55 protected this(ByteBuf buf) { 56 if (buf is null) { 57 throw new NullPointerException("buf"); 58 } 59 this.buf = buf; 60 } 61 62 override 63 final bool hasMemoryAddress() { 64 return buf.hasMemoryAddress(); 65 } 66 67 override 68 final long memoryAddress() { 69 return buf.memoryAddress(); 70 } 71 72 override 73 final int capacity() { 74 return buf.capacity(); 75 } 76 77 override 78 ByteBuf capacity(int newCapacity) { 79 buf.capacity(newCapacity); 80 return this; 81 } 82 83 override 84 final int maxCapacity() { 85 return buf.maxCapacity(); 86 } 87 88 override 89 final ByteBufAllocator alloc() { 90 return buf.alloc(); 91 } 92 93 // override 94 // final ByteOrder order() { 95 // return buf.order(); 96 // } 97 98 // override 99 // ByteBuf order(ByteOrder endianness) { 100 // return buf.order(endianness); 101 // } 102 103 override 104 final ByteBuf unwrap() { 105 return buf; 106 } 107 108 override 109 ByteBuf asReadOnly() { 110 return buf.asReadOnly(); 111 } 112 113 override 114 bool isReadOnly() { 115 return buf.isReadOnly(); 116 } 117 118 override 119 final bool isDirect() { 120 return buf.isDirect(); 121 } 122 123 override 124 final int readerIndex() { 125 return buf.readerIndex(); 126 } 127 128 override 129 final ByteBuf readerIndex(int readerIndex) { 130 buf.readerIndex(readerIndex); 131 return this; 132 } 133 134 override 135 final int writerIndex() { 136 return buf.writerIndex(); 137 } 138 139 override 140 final ByteBuf writerIndex(int writerIndex) { 141 buf.writerIndex(writerIndex); 142 return this; 143 } 144 145 override 146 ByteBuf setIndex(int readerIndex, int writerIndex) { 147 buf.setIndex(readerIndex, writerIndex); 148 return this; 149 } 150 151 override 152 final int readableBytes() { 153 return buf.readableBytes(); 154 } 155 156 override 157 final int writableBytes() { 158 return buf.writableBytes(); 159 } 160 161 override 162 final int maxWritableBytes() { 163 return buf.maxWritableBytes(); 164 } 165 166 override 167 int maxFastWritableBytes() { 168 return buf.maxFastWritableBytes(); 169 } 170 171 override 172 final bool isReadable() { 173 return buf.isReadable(); 174 } 175 176 override 177 final bool isWritable() { 178 return buf.isWritable(); 179 } 180 181 override 182 final ByteBuf clear() { 183 buf.clear(); 184 return this; 185 } 186 187 override 188 final ByteBuf markReaderIndex() { 189 buf.markReaderIndex(); 190 return this; 191 } 192 193 override 194 final ByteBuf resetReaderIndex() { 195 buf.resetReaderIndex(); 196 return this; 197 } 198 199 override 200 final ByteBuf markWriterIndex() { 201 buf.markWriterIndex(); 202 return this; 203 } 204 205 override 206 final ByteBuf resetWriterIndex() { 207 buf.resetWriterIndex(); 208 return this; 209 } 210 211 override 212 ByteBuf discardReadBytes() { 213 buf.discardReadBytes(); 214 return this; 215 } 216 217 override 218 ByteBuf discardSomeReadBytes() { 219 buf.discardSomeReadBytes(); 220 return this; 221 } 222 223 override 224 ByteBuf ensureWritable(int minWritableBytes) { 225 buf.ensureWritable(minWritableBytes); 226 return this; 227 } 228 229 override 230 int ensureWritable(int minWritableBytes, bool force) { 231 return buf.ensureWritable(minWritableBytes, force); 232 } 233 234 override 235 bool getBoolean(int index) { 236 return buf.getBoolean(index); 237 } 238 239 override 240 byte getByte(int index) { 241 return buf.getByte(index); 242 } 243 244 override 245 short getUnsignedByte(int index) { 246 return buf.getUnsignedByte(index); 247 } 248 249 override 250 short getShort(int index) { 251 return buf.getShort(index); 252 } 253 254 override 255 short getShortLE(int index) { 256 return buf.getShortLE(index); 257 } 258 259 override 260 int getUnsignedShort(int index) { 261 return buf.getUnsignedShort(index); 262 } 263 264 override 265 int getUnsignedShortLE(int index) { 266 return buf.getUnsignedShortLE(index); 267 } 268 269 override 270 int getMedium(int index) { 271 return buf.getMedium(index); 272 } 273 274 override 275 int getMediumLE(int index) { 276 return buf.getMediumLE(index); 277 } 278 279 override 280 int getUnsignedMedium(int index) { 281 return buf.getUnsignedMedium(index); 282 } 283 284 override 285 int getUnsignedMediumLE(int index) { 286 return buf.getUnsignedMediumLE(index); 287 } 288 289 override 290 int getInt(int index) { 291 return buf.getInt(index); 292 } 293 294 override 295 int getIntLE(int index) { 296 return buf.getIntLE(index); 297 } 298 299 override 300 long getUnsignedInt(int index) { 301 return buf.getUnsignedInt(index); 302 } 303 304 override 305 long getUnsignedIntLE(int index) { 306 return buf.getUnsignedIntLE(index); 307 } 308 309 override 310 long getLong(int index) { 311 return buf.getLong(index); 312 } 313 314 override 315 long getLongLE(int index) { 316 return buf.getLongLE(index); 317 } 318 319 override 320 char getChar(int index) { 321 return buf.getChar(index); 322 } 323 324 override 325 float getFloat(int index) { 326 return buf.getFloat(index); 327 } 328 329 override 330 double getDouble(int index) { 331 return buf.getDouble(index); 332 } 333 334 override 335 ByteBuf getBytes(int index, ByteBuf dst) { 336 buf.getBytes(index, dst); 337 return this; 338 } 339 340 override 341 ByteBuf getBytes(int index, ByteBuf dst, int length) { 342 buf.getBytes(index, dst, length); 343 return this; 344 } 345 346 override 347 ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { 348 buf.getBytes(index, dst, dstIndex, length); 349 return this; 350 } 351 352 override 353 ByteBuf getBytes(int index, byte[] dst) { 354 buf.getBytes(index, dst); 355 return this; 356 } 357 358 override 359 ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { 360 buf.getBytes(index, dst, dstIndex, length); 361 return this; 362 } 363 364 override 365 ByteBuf getBytes(int index, ByteBuffer dst) { 366 buf.getBytes(index, dst); 367 return this; 368 } 369 370 override 371 ByteBuf getBytes(int index, OutputStream output, int length) { 372 buf.getBytes(index, output, length); 373 return this; 374 } 375 376 // override 377 // int getBytes(int index, GatheringByteChannel output, int length) { 378 // return buf.getBytes(index, output, length); 379 // } 380 381 // override 382 // int getBytes(int index, FileChannel output, long position, int length) { 383 // return buf.getBytes(index, output, position, length); 384 // } 385 386 // override 387 // CharSequence getCharSequence(int index, int length, Charset charset) { 388 // return buf.getCharSequence(index, length, charset); 389 // } 390 391 override 392 ByteBuf setBoolean(int index, bool value) { 393 buf.setBoolean(index, value); 394 return this; 395 } 396 397 override 398 ByteBuf setByte(int index, int value) { 399 buf.setByte(index, value); 400 return this; 401 } 402 403 override 404 ByteBuf setShort(int index, int value) { 405 buf.setShort(index, value); 406 return this; 407 } 408 409 override 410 ByteBuf setShortLE(int index, int value) { 411 buf.setShortLE(index, value); 412 return this; 413 } 414 415 override 416 ByteBuf setMedium(int index, int value) { 417 buf.setMedium(index, value); 418 return this; 419 } 420 421 override 422 ByteBuf setMediumLE(int index, int value) { 423 buf.setMediumLE(index, value); 424 return this; 425 } 426 427 override 428 ByteBuf setInt(int index, int value) { 429 buf.setInt(index, value); 430 return this; 431 } 432 433 override 434 ByteBuf setIntLE(int index, int value) { 435 buf.setIntLE(index, value); 436 return this; 437 } 438 439 override 440 ByteBuf setLong(int index, long value) { 441 buf.setLong(index, value); 442 return this; 443 } 444 445 override 446 ByteBuf setLongLE(int index, long value) { 447 buf.setLongLE(index, value); 448 return this; 449 } 450 451 override 452 ByteBuf setChar(int index, int value) { 453 buf.setChar(index, value); 454 return this; 455 } 456 457 override 458 ByteBuf setFloat(int index, float value) { 459 buf.setFloat(index, value); 460 return this; 461 } 462 463 override 464 ByteBuf setDouble(int index, double value) { 465 buf.setDouble(index, value); 466 return this; 467 } 468 469 override 470 ByteBuf setBytes(int index, ByteBuf src) { 471 buf.setBytes(index, src); 472 return this; 473 } 474 475 override 476 ByteBuf setBytes(int index, ByteBuf src, int length) { 477 buf.setBytes(index, src, length); 478 return this; 479 } 480 481 override 482 ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { 483 buf.setBytes(index, src, srcIndex, length); 484 return this; 485 } 486 487 override 488 ByteBuf setBytes(int index, byte[] src) { 489 buf.setBytes(index, src); 490 return this; 491 } 492 493 override 494 ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { 495 buf.setBytes(index, src, srcIndex, length); 496 return this; 497 } 498 499 override 500 ByteBuf setBytes(int index, ByteBuffer src) { 501 buf.setBytes(index, src); 502 return this; 503 } 504 505 override 506 int setBytes(int index, InputStream input, int length) { 507 return buf.setBytes(index, input, length); 508 } 509 510 // override 511 // int setBytes(int index, ScatteringByteChannel input, int length) { 512 // return buf.setBytes(index, input, length); 513 // } 514 515 // override 516 // int setBytes(int index, FileChannel input, long position, int length) { 517 // return buf.setBytes(index, input, position, length); 518 // } 519 520 override 521 ByteBuf setZero(int index, int length) { 522 buf.setZero(index, length); 523 return this; 524 } 525 526 // override 527 // int setCharSequence(int index, CharSequence sequence, Charset charset) { 528 // return buf.setCharSequence(index, sequence, charset); 529 // } 530 531 override 532 bool readBoolean() { 533 return buf.readBoolean(); 534 } 535 536 override 537 byte readByte() { 538 return buf.readByte(); 539 } 540 541 override 542 short readUnsignedByte() { 543 return buf.readUnsignedByte(); 544 } 545 546 override 547 short readShort() { 548 return buf.readShort(); 549 } 550 551 override 552 short readShortLE() { 553 return buf.readShortLE(); 554 } 555 556 override 557 int readUnsignedShort() { 558 return buf.readUnsignedShort(); 559 } 560 561 override 562 int readUnsignedShortLE() { 563 return buf.readUnsignedShortLE(); 564 } 565 566 override 567 int readMedium() { 568 return buf.readMedium(); 569 } 570 571 override 572 int readMediumLE() { 573 return buf.readMediumLE(); 574 } 575 576 override 577 int readUnsignedMedium() { 578 return buf.readUnsignedMedium(); 579 } 580 581 override 582 int readUnsignedMediumLE() { 583 return buf.readUnsignedMediumLE(); 584 } 585 586 override 587 int readInt() { 588 return buf.readInt(); 589 } 590 591 override 592 int readIntLE() { 593 return buf.readIntLE(); 594 } 595 596 override 597 long readUnsignedInt() { 598 return buf.readUnsignedInt(); 599 } 600 601 override 602 long readUnsignedIntLE() { 603 return buf.readUnsignedIntLE(); 604 } 605 606 override 607 long readLong() { 608 return buf.readLong(); 609 } 610 611 override 612 long readLongLE() { 613 return buf.readLongLE(); 614 } 615 616 override 617 char readChar() { 618 return buf.readChar(); 619 } 620 621 override 622 float readFloat() { 623 return buf.readFloat(); 624 } 625 626 override 627 double readDouble() { 628 return buf.readDouble(); 629 } 630 631 override 632 ByteBuf readBytes(int length) { 633 return buf.readBytes(length); 634 } 635 636 override 637 ByteBuf readSlice(int length) { 638 return buf.readSlice(length); 639 } 640 641 override 642 ByteBuf readRetainedSlice(int length) { 643 return buf.readRetainedSlice(length); 644 } 645 646 override 647 ByteBuf readBytes(ByteBuf dst) { 648 buf.readBytes(dst); 649 return this; 650 } 651 652 override 653 ByteBuf readBytes(ByteBuf dst, int length) { 654 buf.readBytes(dst, length); 655 return this; 656 } 657 658 override 659 ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { 660 buf.readBytes(dst, dstIndex, length); 661 return this; 662 } 663 664 override 665 ByteBuf readBytes(byte[] dst) { 666 buf.readBytes(dst); 667 return this; 668 } 669 670 override 671 ByteBuf readBytes(byte[] dst, int dstIndex, int length) { 672 buf.readBytes(dst, dstIndex, length); 673 return this; 674 } 675 676 override 677 ByteBuf readBytes(ByteBuffer dst) { 678 buf.readBytes(dst); 679 return this; 680 } 681 682 override 683 ByteBuf readBytes(OutputStream output, int length) { 684 buf.readBytes(output, length); 685 return this; 686 } 687 688 // override 689 // int readBytes(GatheringByteChannel output, int length) { 690 // return buf.readBytes(output, length); 691 // } 692 693 // override 694 // int readBytes(FileChannel output, long position, int length) { 695 // return buf.readBytes(output, position, length); 696 // } 697 698 // override 699 // CharSequence readCharSequence(int length, Charset charset) { 700 // return buf.readCharSequence(length, charset); 701 // } 702 703 override 704 ByteBuf skipBytes(int length) { 705 buf.skipBytes(length); 706 return this; 707 } 708 709 override 710 ByteBuf writeBoolean(bool value) { 711 buf.writeBoolean(value); 712 return this; 713 } 714 715 override 716 ByteBuf writeByte(int value) { 717 buf.writeByte(value); 718 return this; 719 } 720 721 override 722 ByteBuf writeShort(int value) { 723 buf.writeShort(value); 724 return this; 725 } 726 727 override 728 ByteBuf writeShortLE(int value) { 729 buf.writeShortLE(value); 730 return this; 731 } 732 733 override 734 ByteBuf writeMedium(int value) { 735 buf.writeMedium(value); 736 return this; 737 } 738 739 override 740 ByteBuf writeMediumLE(int value) { 741 buf.writeMediumLE(value); 742 return this; 743 } 744 745 override 746 ByteBuf writeInt(int value) { 747 buf.writeInt(value); 748 return this; 749 } 750 751 override 752 ByteBuf writeIntLE(int value) { 753 buf.writeIntLE(value); 754 return this; 755 } 756 757 override 758 ByteBuf writeLong(long value) { 759 buf.writeLong(value); 760 return this; 761 } 762 763 override 764 ByteBuf writeLongLE(long value) { 765 buf.writeLongLE(value); 766 return this; 767 } 768 769 override 770 ByteBuf writeChar(int value) { 771 buf.writeChar(value); 772 return this; 773 } 774 775 override 776 ByteBuf writeFloat(float value) { 777 buf.writeFloat(value); 778 return this; 779 } 780 781 override 782 ByteBuf writeDouble(double value) { 783 buf.writeDouble(value); 784 return this; 785 } 786 787 override 788 ByteBuf writeBytes(ByteBuf src) { 789 buf.writeBytes(src); 790 return this; 791 } 792 793 override 794 ByteBuf writeBytes(ByteBuf src, int length) { 795 buf.writeBytes(src, length); 796 return this; 797 } 798 799 override 800 ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) { 801 buf.writeBytes(src, srcIndex, length); 802 return this; 803 } 804 805 override 806 ByteBuf writeBytes(byte[] src) { 807 buf.writeBytes(src); 808 return this; 809 } 810 811 override 812 ByteBuf writeBytes(byte[] src, int srcIndex, int length) { 813 buf.writeBytes(src, srcIndex, length); 814 return this; 815 } 816 817 override 818 ByteBuf writeBytes(ByteBuffer src) { 819 buf.writeBytes(src); 820 return this; 821 } 822 823 override 824 int writeBytes(InputStream input, int length) { 825 return buf.writeBytes(input, length); 826 } 827 828 // override 829 // int writeBytes(ScatteringByteChannel input, int length) { 830 // return buf.writeBytes(input, length); 831 // } 832 833 // override 834 // int writeBytes(FileChannel input, long position, int length) { 835 // return buf.writeBytes(input, position, length); 836 // } 837 838 override 839 ByteBuf writeZero(int length) { 840 buf.writeZero(length); 841 return this; 842 } 843 844 // override 845 // int writeCharSequence(CharSequence sequence, Charset charset) { 846 // return buf.writeCharSequence(sequence, charset); 847 // } 848 849 override 850 int indexOf(int fromIndex, int toIndex, byte value) { 851 return buf.indexOf(fromIndex, toIndex, value); 852 } 853 854 override 855 int bytesBefore(byte value) { 856 return buf.bytesBefore(value); 857 } 858 859 override 860 int bytesBefore(int length, byte value) { 861 return buf.bytesBefore(length, value); 862 } 863 864 override 865 int bytesBefore(int index, int length, byte value) { 866 return buf.bytesBefore(index, length, value); 867 } 868 869 override 870 int forEachByte(ByteProcessor processor) { 871 return buf.forEachByte(processor); 872 } 873 874 override 875 int forEachByte(int index, int length, ByteProcessor processor) { 876 return buf.forEachByte(index, length, processor); 877 } 878 879 override 880 int forEachByteDesc(ByteProcessor processor) { 881 return buf.forEachByteDesc(processor); 882 } 883 884 override 885 int forEachByteDesc(int index, int length, ByteProcessor processor) { 886 return buf.forEachByteDesc(index, length, processor); 887 } 888 889 override 890 ByteBuf copy() { 891 return buf.copy(); 892 } 893 894 override 895 ByteBuf copy(int index, int length) { 896 return buf.copy(index, length); 897 } 898 899 override 900 ByteBuf slice() { 901 return buf.slice(); 902 } 903 904 override 905 ByteBuf retainedSlice() { 906 return buf.retainedSlice(); 907 } 908 909 override 910 ByteBuf slice(int index, int length) { 911 return buf.slice(index, length); 912 } 913 914 override 915 ByteBuf retainedSlice(int index, int length) { 916 return buf.retainedSlice(index, length); 917 } 918 919 override 920 ByteBuf duplicate() { 921 return buf.duplicate(); 922 } 923 924 override 925 ByteBuf retainedDuplicate() { 926 return buf.retainedDuplicate(); 927 } 928 929 override 930 int nioBufferCount() { 931 return buf.nioBufferCount(); 932 } 933 934 override 935 ByteBuffer nioBuffer() { 936 return buf.nioBuffer(); 937 } 938 939 override 940 ByteBuffer nioBuffer(int index, int length) { 941 return buf.nioBuffer(index, length); 942 } 943 944 override 945 ByteBuffer[] nioBuffers() { 946 return buf.nioBuffers(); 947 } 948 949 override 950 ByteBuffer[] nioBuffers(int index, int length) { 951 return buf.nioBuffers(index, length); 952 } 953 954 override 955 ByteBuffer internalNioBuffer(int index, int length) { 956 return buf.internalNioBuffer(index, length); 957 } 958 959 override 960 bool hasArray() { 961 return buf.hasArray(); 962 } 963 964 override 965 byte[] array() { 966 return buf.array(); 967 } 968 969 override 970 int arrayOffset() { 971 return buf.arrayOffset(); 972 } 973 974 // override 975 // string toString(Charset charset) { 976 // return buf.toString(charset); 977 // } 978 979 // override 980 // string toString(int index, int length, Charset charset) { 981 // return buf.toString(index, length, charset); 982 // } 983 984 override 985 size_t toHash() @trusted nothrow { 986 return buf.toHash(); 987 } 988 989 override 990 bool opEquals(Object obj) { 991 return buf == obj; 992 } 993 994 override 995 int compareTo(ByteBuf buffer) { 996 return buf.compareTo(buffer); 997 } 998 999 override 1000 string toString() { 1001 return typeid(this).name ~ "(" ~ buf.toString() ~ ")"; 1002 } 1003 1004 override 1005 ByteBuf retain(int increment) { 1006 buf.retain(increment); 1007 return this; 1008 } 1009 1010 override 1011 ByteBuf retain() { 1012 buf.retain(); 1013 return this; 1014 } 1015 1016 override 1017 ByteBuf touch() { 1018 buf.touch(); 1019 return this; 1020 } 1021 1022 override 1023 ByteBuf touch(Object hint) { 1024 buf.touch(hint); 1025 return this; 1026 } 1027 1028 override 1029 final bool isReadable(int size) { 1030 return buf.isReadable(size); 1031 } 1032 1033 override 1034 final bool isWritable(int size) { 1035 return buf.isWritable(size); 1036 } 1037 1038 // override 1039 final int refCnt() { 1040 return buf.refCnt(); 1041 } 1042 1043 // override 1044 bool release() { 1045 return buf.release(); 1046 } 1047 1048 // override 1049 bool release(int decrement) { 1050 return buf.release(decrement); 1051 } 1052 1053 override 1054 final bool isAccessible() { 1055 return buf.isAccessible(); 1056 } 1057 }