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