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