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