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