View Javadoc
1   /*
2    * Copyright 2012 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  package io.netty.buffer;
17  
18  import io.netty.util.AsciiString;
19  import io.netty.util.ByteProcessor;
20  import io.netty.util.CharsetUtil;
21  import io.netty.util.IllegalReferenceCountException;
22  import io.netty.util.ResourceLeakDetector;
23  import io.netty.util.ResourceLeakDetectorFactory;
24  import io.netty.util.internal.ObjectUtil;
25  import io.netty.util.internal.PlatformDependent;
26  import io.netty.util.internal.StringUtil;
27  import io.netty.util.internal.SystemPropertyUtil;
28  import io.netty.util.internal.logging.InternalLogger;
29  import io.netty.util.internal.logging.InternalLoggerFactory;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  import java.nio.ByteBuffer;
35  import java.nio.ByteOrder;
36  import java.nio.channels.FileChannel;
37  import java.nio.channels.GatheringByteChannel;
38  import java.nio.channels.ScatteringByteChannel;
39  import java.nio.charset.Charset;
40  
41  import static io.netty.util.internal.MathUtil.isOutOfBounds;
42  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
43  
44  /**
45   * A skeletal implementation of a buffer.
46   */
47  public abstract class AbstractByteBuf extends ByteBuf {
48      private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractByteBuf.class);
49      private static final String LEGACY_PROP_CHECK_ACCESSIBLE = "io.netty.buffer.bytebuf.checkAccessible";
50      private static final String PROP_CHECK_ACCESSIBLE = "io.netty.buffer.checkAccessible";
51      static final boolean checkAccessible; // accessed from CompositeByteBuf
52      private static final String PROP_CHECK_BOUNDS = "io.netty.buffer.checkBounds";
53      private static final boolean checkBounds;
54  
55      static {
56          if (SystemPropertyUtil.contains(PROP_CHECK_ACCESSIBLE)) {
57              checkAccessible = SystemPropertyUtil.getBoolean(PROP_CHECK_ACCESSIBLE, true);
58          } else {
59              checkAccessible = SystemPropertyUtil.getBoolean(LEGACY_PROP_CHECK_ACCESSIBLE, true);
60          }
61          checkBounds = SystemPropertyUtil.getBoolean(PROP_CHECK_BOUNDS, true);
62          if (logger.isDebugEnabled()) {
63              logger.debug("-D{}: {}", PROP_CHECK_ACCESSIBLE, checkAccessible);
64              logger.debug("-D{}: {}", PROP_CHECK_BOUNDS, checkBounds);
65          }
66      }
67  
68      static final ResourceLeakDetector<ByteBuf> leakDetector =
69              ResourceLeakDetectorFactory.instance().newResourceLeakDetector(ByteBuf.class);
70  
71      int readerIndex;
72      int writerIndex;
73      private int markedReaderIndex;
74      private int markedWriterIndex;
75      private int maxCapacity;
76  
77      protected AbstractByteBuf(int maxCapacity) {
78          checkPositiveOrZero(maxCapacity, "maxCapacity");
79          this.maxCapacity = maxCapacity;
80      }
81  
82      @Override
83      public boolean isReadOnly() {
84          return false;
85      }
86  
87      @SuppressWarnings("deprecation")
88      @Override
89      public ByteBuf asReadOnly() {
90          if (isReadOnly()) {
91              return this;
92          }
93          return Unpooled.unmodifiableBuffer(this);
94      }
95  
96      @Override
97      public int maxCapacity() {
98          return maxCapacity;
99      }
100 
101     protected final void maxCapacity(int maxCapacity) {
102         this.maxCapacity = maxCapacity;
103     }
104 
105     @Override
106     public int readerIndex() {
107         return readerIndex;
108     }
109 
110     private static void checkIndexBounds(final int readerIndex, final int writerIndex, final int capacity) {
111         if (readerIndex < 0 || readerIndex > writerIndex || writerIndex > capacity) {
112             throw new IndexOutOfBoundsException(String.format(
113                     "readerIndex: %d, writerIndex: %d (expected: 0 <= readerIndex <= writerIndex <= capacity(%d))",
114                     readerIndex, writerIndex, capacity));
115         }
116     }
117 
118     @Override
119     public ByteBuf readerIndex(int readerIndex) {
120         if (checkBounds) {
121             checkIndexBounds(readerIndex, writerIndex, capacity());
122         }
123         this.readerIndex = readerIndex;
124         return this;
125     }
126 
127     @Override
128     public int writerIndex() {
129         return writerIndex;
130     }
131 
132     @Override
133     public ByteBuf writerIndex(int writerIndex) {
134         if (checkBounds) {
135             checkIndexBounds(readerIndex, writerIndex, capacity());
136         }
137         this.writerIndex = writerIndex;
138         return this;
139     }
140 
141     @Override
142     public ByteBuf setIndex(int readerIndex, int writerIndex) {
143         if (checkBounds) {
144             checkIndexBounds(readerIndex, writerIndex, capacity());
145         }
146         setIndex0(readerIndex, writerIndex);
147         return this;
148     }
149 
150     @Override
151     public ByteBuf clear() {
152         readerIndex = writerIndex = 0;
153         return this;
154     }
155 
156     @Override
157     public boolean isReadable() {
158         return writerIndex > readerIndex;
159     }
160 
161     @Override
162     public boolean isReadable(int numBytes) {
163         return writerIndex - readerIndex >= numBytes;
164     }
165 
166     @Override
167     public boolean isWritable() {
168         return capacity() > writerIndex;
169     }
170 
171     @Override
172     public boolean isWritable(int numBytes) {
173         return capacity() - writerIndex >= numBytes;
174     }
175 
176     @Override
177     public int readableBytes() {
178         return writerIndex - readerIndex;
179     }
180 
181     @Override
182     public int writableBytes() {
183         return capacity() - writerIndex;
184     }
185 
186     @Override
187     public int maxWritableBytes() {
188         return maxCapacity() - writerIndex;
189     }
190 
191     @Override
192     public ByteBuf markReaderIndex() {
193         markedReaderIndex = readerIndex;
194         return this;
195     }
196 
197     @Override
198     public ByteBuf resetReaderIndex() {
199         readerIndex(markedReaderIndex);
200         return this;
201     }
202 
203     @Override
204     public ByteBuf markWriterIndex() {
205         markedWriterIndex = writerIndex;
206         return this;
207     }
208 
209     @Override
210     public ByteBuf resetWriterIndex() {
211         writerIndex(markedWriterIndex);
212         return this;
213     }
214 
215     @Override
216     public ByteBuf discardReadBytes() {
217         if (readerIndex == 0) {
218             ensureAccessible();
219             return this;
220         }
221 
222         if (readerIndex != writerIndex) {
223             setBytes(0, this, readerIndex, writerIndex - readerIndex);
224             writerIndex -= readerIndex;
225             adjustMarkers(readerIndex);
226             readerIndex = 0;
227         } else {
228             ensureAccessible();
229             adjustMarkers(readerIndex);
230             writerIndex = readerIndex = 0;
231         }
232         return this;
233     }
234 
235     @Override
236     public ByteBuf discardSomeReadBytes() {
237         if (readerIndex > 0) {
238             if (readerIndex == writerIndex) {
239                 ensureAccessible();
240                 adjustMarkers(readerIndex);
241                 writerIndex = readerIndex = 0;
242                 return this;
243             }
244 
245             if (readerIndex >= capacity() >>> 1) {
246                 setBytes(0, this, readerIndex, writerIndex - readerIndex);
247                 writerIndex -= readerIndex;
248                 adjustMarkers(readerIndex);
249                 readerIndex = 0;
250                 return this;
251             }
252         }
253         ensureAccessible();
254         return this;
255     }
256 
257     protected final void adjustMarkers(int decrement) {
258         if (markedReaderIndex <= decrement) {
259             markedReaderIndex = 0;
260             if (markedWriterIndex <= decrement) {
261                 markedWriterIndex = 0;
262             } else {
263                 markedWriterIndex -= decrement;
264             }
265         } else {
266             markedReaderIndex -= decrement;
267             markedWriterIndex -= decrement;
268         }
269     }
270 
271     // Called after a capacity reduction
272     protected final void trimIndicesToCapacity(int newCapacity) {
273         if (writerIndex() > newCapacity) {
274             setIndex0(Math.min(readerIndex(), newCapacity), newCapacity);
275         }
276     }
277 
278     @Override
279     public ByteBuf ensureWritable(int minWritableBytes) {
280         ensureWritable0(checkPositiveOrZero(minWritableBytes, "minWritableBytes"));
281         return this;
282     }
283 
284     final void ensureWritable0(int minWritableBytes) {
285         final int writerIndex = writerIndex();
286         final int targetCapacity = writerIndex + minWritableBytes;
287         // using non-short-circuit & to reduce branching - this is a hot path and targetCapacity should rarely overflow
288         if (targetCapacity >= 0 & targetCapacity <= capacity()) {
289             ensureAccessible();
290             return;
291         }
292         if (checkBounds && (targetCapacity < 0 || targetCapacity > maxCapacity)) {
293             ensureAccessible();
294             throw new IndexOutOfBoundsException(String.format(
295                     "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
296                     writerIndex, minWritableBytes, maxCapacity, this));
297         }
298 
299         // Normalize the target capacity to the power of 2.
300         final int fastWritable = maxFastWritableBytes();
301         int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
302                 : alloc().calculateNewCapacity(targetCapacity, maxCapacity);
303 
304         // Adjust to the new capacity.
305         capacity(newCapacity);
306     }
307 
308     @Override
309     public int ensureWritable(int minWritableBytes, boolean force) {
310         ensureAccessible();
311         checkPositiveOrZero(minWritableBytes, "minWritableBytes");
312 
313         if (minWritableBytes <= writableBytes()) {
314             return 0;
315         }
316 
317         final int maxCapacity = maxCapacity();
318         final int writerIndex = writerIndex();
319         if (minWritableBytes > maxCapacity - writerIndex) {
320             if (!force || capacity() == maxCapacity) {
321                 return 1;
322             }
323 
324             capacity(maxCapacity);
325             return 3;
326         }
327 
328         int fastWritable = maxFastWritableBytes();
329         int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
330                 : alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
331 
332         // Adjust to the new capacity.
333         capacity(newCapacity);
334         return 2;
335     }
336 
337     @Override
338     public ByteBuf order(ByteOrder endianness) {
339         if (endianness == order()) {
340             return this;
341         }
342         ObjectUtil.checkNotNull(endianness, "endianness");
343         return newSwappedByteBuf();
344     }
345 
346     /**
347      * Creates a new {@link SwappedByteBuf} for this {@link ByteBuf} instance.
348      */
349     protected SwappedByteBuf newSwappedByteBuf() {
350         return new SwappedByteBuf(this);
351     }
352 
353     @Override
354     public byte getByte(int index) {
355         checkIndex(index);
356         return _getByte(index);
357     }
358 
359     protected abstract byte _getByte(int index);
360 
361     @Override
362     public boolean getBoolean(int index) {
363         return getByte(index) != 0;
364     }
365 
366     @Override
367     public short getUnsignedByte(int index) {
368         return (short) (getByte(index) & 0xFF);
369     }
370 
371     @Override
372     public short getShort(int index) {
373         checkIndex(index, 2);
374         return _getShort(index);
375     }
376 
377     protected abstract short _getShort(int index);
378 
379     @Override
380     public short getShortLE(int index) {
381         checkIndex(index, 2);
382         return _getShortLE(index);
383     }
384 
385     protected abstract short _getShortLE(int index);
386 
387     @Override
388     public int getUnsignedShort(int index) {
389         return getShort(index) & 0xFFFF;
390     }
391 
392     @Override
393     public int getUnsignedShortLE(int index) {
394         return getShortLE(index) & 0xFFFF;
395     }
396 
397     @Override
398     public int getUnsignedMedium(int index) {
399         checkIndex(index, 3);
400         return _getUnsignedMedium(index);
401     }
402 
403     protected abstract int _getUnsignedMedium(int index);
404 
405     @Override
406     public int getUnsignedMediumLE(int index) {
407         checkIndex(index, 3);
408         return _getUnsignedMediumLE(index);
409     }
410 
411     protected abstract int _getUnsignedMediumLE(int index);
412 
413     @Override
414     public int getMedium(int index) {
415         int value = getUnsignedMedium(index);
416         if ((value & 0x800000) != 0) {
417             value |= 0xff000000;
418         }
419         return value;
420     }
421 
422     @Override
423     public int getMediumLE(int index) {
424         int value = getUnsignedMediumLE(index);
425         if ((value & 0x800000) != 0) {
426             value |= 0xff000000;
427         }
428         return value;
429     }
430 
431     @Override
432     public int getInt(int index) {
433         checkIndex(index, 4);
434         return _getInt(index);
435     }
436 
437     protected abstract int _getInt(int index);
438 
439     @Override
440     public int getIntLE(int index) {
441         checkIndex(index, 4);
442         return _getIntLE(index);
443     }
444 
445     protected abstract int _getIntLE(int index);
446 
447     @Override
448     public long getUnsignedInt(int index) {
449         return getInt(index) & 0xFFFFFFFFL;
450     }
451 
452     @Override
453     public long getUnsignedIntLE(int index) {
454         return getIntLE(index) & 0xFFFFFFFFL;
455     }
456 
457     @Override
458     public long getLong(int index) {
459         checkIndex(index, 8);
460         return _getLong(index);
461     }
462 
463     protected abstract long _getLong(int index);
464 
465     @Override
466     public long getLongLE(int index) {
467         checkIndex(index, 8);
468         return _getLongLE(index);
469     }
470 
471     protected abstract long _getLongLE(int index);
472 
473     @Override
474     public char getChar(int index) {
475         return (char) getShort(index);
476     }
477 
478     @Override
479     public float getFloat(int index) {
480         return Float.intBitsToFloat(getInt(index));
481     }
482 
483     @Override
484     public double getDouble(int index) {
485         return Double.longBitsToDouble(getLong(index));
486     }
487 
488     @Override
489     public ByteBuf getBytes(int index, byte[] dst) {
490         getBytes(index, dst, 0, dst.length);
491         return this;
492     }
493 
494     @Override
495     public ByteBuf getBytes(int index, ByteBuf dst) {
496         getBytes(index, dst, dst.writableBytes());
497         return this;
498     }
499 
500     @Override
501     public ByteBuf getBytes(int index, ByteBuf dst, int length) {
502         getBytes(index, dst, dst.writerIndex(), length);
503         dst.writerIndex(dst.writerIndex() + length);
504         return this;
505     }
506 
507     @Override
508     public CharSequence getCharSequence(int index, int length, Charset charset) {
509         if (CharsetUtil.US_ASCII.equals(charset) || CharsetUtil.ISO_8859_1.equals(charset)) {
510             // ByteBufUtil.getBytes(...) will return a new copy which the AsciiString uses directly
511             return new AsciiString(ByteBufUtil.getBytes(this, index, length, true), false);
512         }
513         return toString(index, length, charset);
514     }
515 
516     @Override
517     public CharSequence readCharSequence(int length, Charset charset) {
518         CharSequence sequence = getCharSequence(readerIndex, length, charset);
519         readerIndex += length;
520         return sequence;
521     }
522 
523     @Override
524     public ByteBuf setByte(int index, int value) {
525         checkIndex(index);
526         _setByte(index, value);
527         return this;
528     }
529 
530     protected abstract void _setByte(int index, int value);
531 
532     @Override
533     public ByteBuf setBoolean(int index, boolean value) {
534         setByte(index, value? 1 : 0);
535         return this;
536     }
537 
538     @Override
539     public ByteBuf setShort(int index, int value) {
540         checkIndex(index, 2);
541         _setShort(index, value);
542         return this;
543     }
544 
545     protected abstract void _setShort(int index, int value);
546 
547     @Override
548     public ByteBuf setShortLE(int index, int value) {
549         checkIndex(index, 2);
550         _setShortLE(index, value);
551         return this;
552     }
553 
554     protected abstract void _setShortLE(int index, int value);
555 
556     @Override
557     public ByteBuf setChar(int index, int value) {
558         setShort(index, value);
559         return this;
560     }
561 
562     @Override
563     public ByteBuf setMedium(int index, int value) {
564         checkIndex(index, 3);
565         _setMedium(index, value);
566         return this;
567     }
568 
569     protected abstract void _setMedium(int index, int value);
570 
571     @Override
572     public ByteBuf setMediumLE(int index, int value) {
573         checkIndex(index, 3);
574         _setMediumLE(index, value);
575         return this;
576     }
577 
578     protected abstract void _setMediumLE(int index, int value);
579 
580     @Override
581     public ByteBuf setInt(int index, int value) {
582         checkIndex(index, 4);
583         _setInt(index, value);
584         return this;
585     }
586 
587     protected abstract void _setInt(int index, int value);
588 
589     @Override
590     public ByteBuf setIntLE(int index, int value) {
591         checkIndex(index, 4);
592         _setIntLE(index, value);
593         return this;
594     }
595 
596     protected abstract void _setIntLE(int index, int value);
597 
598     @Override
599     public ByteBuf setFloat(int index, float value) {
600         setInt(index, Float.floatToRawIntBits(value));
601         return this;
602     }
603 
604     @Override
605     public ByteBuf setLong(int index, long value) {
606         checkIndex(index, 8);
607         _setLong(index, value);
608         return this;
609     }
610 
611     protected abstract void _setLong(int index, long value);
612 
613     @Override
614     public ByteBuf setLongLE(int index, long value) {
615         checkIndex(index, 8);
616         _setLongLE(index, value);
617         return this;
618     }
619 
620     protected abstract void _setLongLE(int index, long value);
621 
622     @Override
623     public ByteBuf setDouble(int index, double value) {
624         setLong(index, Double.doubleToRawLongBits(value));
625         return this;
626     }
627 
628     @Override
629     public ByteBuf setBytes(int index, byte[] src) {
630         setBytes(index, src, 0, src.length);
631         return this;
632     }
633 
634     @Override
635     public ByteBuf setBytes(int index, ByteBuf src) {
636         setBytes(index, src, src.readableBytes());
637         return this;
638     }
639 
640     private static void checkReadableBounds(final ByteBuf src, final int length) {
641         if (length > src.readableBytes()) {
642             throw new IndexOutOfBoundsException(String.format(
643                     "length(%d) exceeds src.readableBytes(%d) where src is: %s", length, src.readableBytes(), src));
644         }
645     }
646 
647     @Override
648     public ByteBuf setBytes(int index, ByteBuf src, int length) {
649         checkIndex(index, length);
650         ObjectUtil.checkNotNull(src, "src");
651         if (checkBounds) {
652             checkReadableBounds(src, length);
653         }
654 
655         setBytes(index, src, src.readerIndex(), length);
656         src.readerIndex(src.readerIndex() + length);
657         return this;
658     }
659 
660     @Override
661     public ByteBuf setZero(int index, int length) {
662         if (length == 0) {
663             return this;
664         }
665 
666         checkIndex(index, length);
667 
668         int nLong = length >>> 3;
669         int nBytes = length & 7;
670         for (int i = nLong; i > 0; i --) {
671             _setLong(index, 0);
672             index += 8;
673         }
674         if (nBytes == 4) {
675             _setInt(index, 0);
676             // Not need to update the index as we not will use it after this.
677         } else if (nBytes < 4) {
678             for (int i = nBytes; i > 0; i --) {
679                 _setByte(index, 0);
680                 index ++;
681             }
682         } else {
683             _setInt(index, 0);
684             index += 4;
685             for (int i = nBytes - 4; i > 0; i --) {
686                 _setByte(index, 0);
687                 index ++;
688             }
689         }
690         return this;
691     }
692 
693     @Override
694     public int setCharSequence(int index, CharSequence sequence, Charset charset) {
695         return setCharSequence0(index, sequence, charset, false);
696     }
697 
698     private int setCharSequence0(int index, CharSequence sequence, Charset charset, boolean expand) {
699         if (charset.equals(CharsetUtil.UTF_8)) {
700             int length = ByteBufUtil.utf8MaxBytes(sequence);
701             if (expand) {
702                 ensureWritable0(length);
703                 checkIndex0(index, length);
704             } else {
705                 checkIndex(index, length);
706             }
707             return ByteBufUtil.writeUtf8(this, index, length, sequence, sequence.length());
708         }
709         if (charset.equals(CharsetUtil.US_ASCII) || charset.equals(CharsetUtil.ISO_8859_1)) {
710             int length = sequence.length();
711             if (expand) {
712                 ensureWritable0(length);
713                 checkIndex0(index, length);
714             } else {
715                 checkIndex(index, length);
716             }
717             return ByteBufUtil.writeAscii(this, index, sequence, length);
718         }
719         byte[] bytes = sequence.toString().getBytes(charset);
720         if (expand) {
721             ensureWritable0(bytes.length);
722             // setBytes(...) will take care of checking the indices.
723         }
724         setBytes(index, bytes);
725         return bytes.length;
726     }
727 
728     @Override
729     public byte readByte() {
730         checkReadableBytes0(1);
731         int i = readerIndex;
732         byte b = _getByte(i);
733         readerIndex = i + 1;
734         return b;
735     }
736 
737     @Override
738     public boolean readBoolean() {
739         return readByte() != 0;
740     }
741 
742     @Override
743     public short readUnsignedByte() {
744         return (short) (readByte() & 0xFF);
745     }
746 
747     @Override
748     public short readShort() {
749         checkReadableBytes0(2);
750         short v = _getShort(readerIndex);
751         readerIndex += 2;
752         return v;
753     }
754 
755     @Override
756     public short readShortLE() {
757         checkReadableBytes0(2);
758         short v = _getShortLE(readerIndex);
759         readerIndex += 2;
760         return v;
761     }
762 
763     @Override
764     public int readUnsignedShort() {
765         return readShort() & 0xFFFF;
766     }
767 
768     @Override
769     public int readUnsignedShortLE() {
770         return readShortLE() & 0xFFFF;
771     }
772 
773     @Override
774     public int readMedium() {
775         int value = readUnsignedMedium();
776         if ((value & 0x800000) != 0) {
777             value |= 0xff000000;
778         }
779         return value;
780     }
781 
782     @Override
783     public int readMediumLE() {
784         int value = readUnsignedMediumLE();
785         if ((value & 0x800000) != 0) {
786             value |= 0xff000000;
787         }
788         return value;
789     }
790 
791     @Override
792     public int readUnsignedMedium() {
793         checkReadableBytes0(3);
794         int v = _getUnsignedMedium(readerIndex);
795         readerIndex += 3;
796         return v;
797     }
798 
799     @Override
800     public int readUnsignedMediumLE() {
801         checkReadableBytes0(3);
802         int v = _getUnsignedMediumLE(readerIndex);
803         readerIndex += 3;
804         return v;
805     }
806 
807     @Override
808     public int readInt() {
809         checkReadableBytes0(4);
810         int v = _getInt(readerIndex);
811         readerIndex += 4;
812         return v;
813     }
814 
815     @Override
816     public int readIntLE() {
817         checkReadableBytes0(4);
818         int v = _getIntLE(readerIndex);
819         readerIndex += 4;
820         return v;
821     }
822 
823     @Override
824     public long readUnsignedInt() {
825         return readInt() & 0xFFFFFFFFL;
826     }
827 
828     @Override
829     public long readUnsignedIntLE() {
830         return readIntLE() & 0xFFFFFFFFL;
831     }
832 
833     @Override
834     public long readLong() {
835         checkReadableBytes0(8);
836         long v = _getLong(readerIndex);
837         readerIndex += 8;
838         return v;
839     }
840 
841     @Override
842     public long readLongLE() {
843         checkReadableBytes0(8);
844         long v = _getLongLE(readerIndex);
845         readerIndex += 8;
846         return v;
847     }
848 
849     @Override
850     public char readChar() {
851         return (char) readShort();
852     }
853 
854     @Override
855     public float readFloat() {
856         return Float.intBitsToFloat(readInt());
857     }
858 
859     @Override
860     public double readDouble() {
861         return Double.longBitsToDouble(readLong());
862     }
863 
864     @Override
865     public ByteBuf readBytes(int length) {
866         checkReadableBytes(length);
867         if (length == 0) {
868             return Unpooled.EMPTY_BUFFER;
869         }
870 
871         ByteBuf buf = alloc().buffer(length, maxCapacity);
872         buf.writeBytes(this, readerIndex, length);
873         readerIndex += length;
874         return buf;
875     }
876 
877     @Override
878     public ByteBuf readSlice(int length) {
879         checkReadableBytes(length);
880         ByteBuf slice = slice(readerIndex, length);
881         readerIndex += length;
882         return slice;
883     }
884 
885     @Override
886     public ByteBuf readRetainedSlice(int length) {
887         checkReadableBytes(length);
888         ByteBuf slice = retainedSlice(readerIndex, length);
889         readerIndex += length;
890         return slice;
891     }
892 
893     @Override
894     public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
895         checkReadableBytes(length);
896         getBytes(readerIndex, dst, dstIndex, length);
897         readerIndex += length;
898         return this;
899     }
900 
901     @Override
902     public ByteBuf readBytes(byte[] dst) {
903         readBytes(dst, 0, dst.length);
904         return this;
905     }
906 
907     @Override
908     public ByteBuf readBytes(ByteBuf dst) {
909         readBytes(dst, dst.writableBytes());
910         return this;
911     }
912 
913     @Override
914     public ByteBuf readBytes(ByteBuf dst, int length) {
915         if (checkBounds) {
916             if (length > dst.writableBytes()) {
917                 throw new IndexOutOfBoundsException(String.format(
918                         "length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
919             }
920         }
921         readBytes(dst, dst.writerIndex(), length);
922         dst.writerIndex(dst.writerIndex() + length);
923         return this;
924     }
925 
926     @Override
927     public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
928         checkReadableBytes(length);
929         getBytes(readerIndex, dst, dstIndex, length);
930         readerIndex += length;
931         return this;
932     }
933 
934     @Override
935     public ByteBuf readBytes(ByteBuffer dst) {
936         int length = dst.remaining();
937         checkReadableBytes(length);
938         getBytes(readerIndex, dst);
939         readerIndex += length;
940         return this;
941     }
942 
943     @Override
944     public int readBytes(GatheringByteChannel out, int length)
945             throws IOException {
946         checkReadableBytes(length);
947         int readBytes = getBytes(readerIndex, out, length);
948         readerIndex += readBytes;
949         return readBytes;
950     }
951 
952     @Override
953     public int readBytes(FileChannel out, long position, int length)
954             throws IOException {
955         checkReadableBytes(length);
956         int readBytes = getBytes(readerIndex, out, position, length);
957         readerIndex += readBytes;
958         return readBytes;
959     }
960 
961     @Override
962     public ByteBuf readBytes(OutputStream out, int length) throws IOException {
963         checkReadableBytes(length);
964         getBytes(readerIndex, out, length);
965         readerIndex += length;
966         return this;
967     }
968 
969     @Override
970     public ByteBuf skipBytes(int length) {
971         checkReadableBytes(length);
972         readerIndex += length;
973         return this;
974     }
975 
976     @Override
977     public ByteBuf writeBoolean(boolean value) {
978         writeByte(value ? 1 : 0);
979         return this;
980     }
981 
982     @Override
983     public ByteBuf writeByte(int value) {
984         ensureWritable0(1);
985         _setByte(writerIndex++, value);
986         return this;
987     }
988 
989     @Override
990     public ByteBuf writeShort(int value) {
991         ensureWritable0(2);
992         _setShort(writerIndex, value);
993         writerIndex += 2;
994         return this;
995     }
996 
997     @Override
998     public ByteBuf writeShortLE(int value) {
999         ensureWritable0(2);
1000         _setShortLE(writerIndex, value);
1001         writerIndex += 2;
1002         return this;
1003     }
1004 
1005     @Override
1006     public ByteBuf writeMedium(int value) {
1007         ensureWritable0(3);
1008         _setMedium(writerIndex, value);
1009         writerIndex += 3;
1010         return this;
1011     }
1012 
1013     @Override
1014     public ByteBuf writeMediumLE(int value) {
1015         ensureWritable0(3);
1016         _setMediumLE(writerIndex, value);
1017         writerIndex += 3;
1018         return this;
1019     }
1020 
1021     @Override
1022     public ByteBuf writeInt(int value) {
1023         ensureWritable0(4);
1024         _setInt(writerIndex, value);
1025         writerIndex += 4;
1026         return this;
1027     }
1028 
1029     @Override
1030     public ByteBuf writeIntLE(int value) {
1031         ensureWritable0(4);
1032         _setIntLE(writerIndex, value);
1033         writerIndex += 4;
1034         return this;
1035     }
1036 
1037     @Override
1038     public ByteBuf writeLong(long value) {
1039         ensureWritable0(8);
1040         _setLong(writerIndex, value);
1041         writerIndex += 8;
1042         return this;
1043     }
1044 
1045     @Override
1046     public ByteBuf writeLongLE(long value) {
1047         ensureWritable0(8);
1048         _setLongLE(writerIndex, value);
1049         writerIndex += 8;
1050         return this;
1051     }
1052 
1053     @Override
1054     public ByteBuf writeChar(int value) {
1055         writeShort(value);
1056         return this;
1057     }
1058 
1059     @Override
1060     public ByteBuf writeFloat(float value) {
1061         writeInt(Float.floatToRawIntBits(value));
1062         return this;
1063     }
1064 
1065     @Override
1066     public ByteBuf writeDouble(double value) {
1067         writeLong(Double.doubleToRawLongBits(value));
1068         return this;
1069     }
1070 
1071     @Override
1072     public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
1073         ensureWritable(length);
1074         setBytes(writerIndex, src, srcIndex, length);
1075         writerIndex += length;
1076         return this;
1077     }
1078 
1079     @Override
1080     public ByteBuf writeBytes(byte[] src) {
1081         writeBytes(src, 0, src.length);
1082         return this;
1083     }
1084 
1085     @Override
1086     public ByteBuf writeBytes(ByteBuf src) {
1087         writeBytes(src, src.readableBytes());
1088         return this;
1089     }
1090 
1091     @Override
1092     public ByteBuf writeBytes(ByteBuf src, int length) {
1093         if (checkBounds) {
1094             checkReadableBounds(src, length);
1095         }
1096         writeBytes(src, src.readerIndex(), length);
1097         src.readerIndex(src.readerIndex() + length);
1098         return this;
1099     }
1100 
1101     @Override
1102     public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
1103         ensureWritable(length);
1104         setBytes(writerIndex, src, srcIndex, length);
1105         writerIndex += length;
1106         return this;
1107     }
1108 
1109     @Override
1110     public ByteBuf writeBytes(ByteBuffer src) {
1111         int length = src.remaining();
1112         ensureWritable0(length);
1113         setBytes(writerIndex, src);
1114         writerIndex += length;
1115         return this;
1116     }
1117 
1118     @Override
1119     public int writeBytes(InputStream in, int length)
1120             throws IOException {
1121         ensureWritable(length);
1122         int writtenBytes = setBytes(writerIndex, in, length);
1123         if (writtenBytes > 0) {
1124             writerIndex += writtenBytes;
1125         }
1126         return writtenBytes;
1127     }
1128 
1129     @Override
1130     public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
1131         ensureWritable(length);
1132         int writtenBytes = setBytes(writerIndex, in, length);
1133         if (writtenBytes > 0) {
1134             writerIndex += writtenBytes;
1135         }
1136         return writtenBytes;
1137     }
1138 
1139     @Override
1140     public int writeBytes(FileChannel in, long position, int length) throws IOException {
1141         ensureWritable(length);
1142         int writtenBytes = setBytes(writerIndex, in, position, length);
1143         if (writtenBytes > 0) {
1144             writerIndex += writtenBytes;
1145         }
1146         return writtenBytes;
1147     }
1148 
1149     @Override
1150     public ByteBuf writeZero(int length) {
1151         if (length == 0) {
1152             return this;
1153         }
1154 
1155         ensureWritable(length);
1156         int wIndex = writerIndex;
1157         checkIndex0(wIndex, length);
1158 
1159         int nLong = length >>> 3;
1160         int nBytes = length & 7;
1161         for (int i = nLong; i > 0; i --) {
1162             _setLong(wIndex, 0);
1163             wIndex += 8;
1164         }
1165         if (nBytes == 4) {
1166             _setInt(wIndex, 0);
1167             wIndex += 4;
1168         } else if (nBytes < 4) {
1169             for (int i = nBytes; i > 0; i --) {
1170                 _setByte(wIndex, 0);
1171                 wIndex++;
1172             }
1173         } else {
1174             _setInt(wIndex, 0);
1175             wIndex += 4;
1176             for (int i = nBytes - 4; i > 0; i --) {
1177                 _setByte(wIndex, 0);
1178                 wIndex++;
1179             }
1180         }
1181         writerIndex = wIndex;
1182         return this;
1183     }
1184 
1185     @Override
1186     public int writeCharSequence(CharSequence sequence, Charset charset) {
1187         int written = setCharSequence0(writerIndex, sequence, charset, true);
1188         writerIndex += written;
1189         return written;
1190     }
1191 
1192     @Override
1193     public ByteBuf copy() {
1194         return copy(readerIndex, readableBytes());
1195     }
1196 
1197     @Override
1198     public ByteBuf duplicate() {
1199         ensureAccessible();
1200         return new UnpooledDuplicatedByteBuf(this);
1201     }
1202 
1203     @Override
1204     public ByteBuf retainedDuplicate() {
1205         return duplicate().retain();
1206     }
1207 
1208     @Override
1209     public ByteBuf slice() {
1210         return slice(readerIndex, readableBytes());
1211     }
1212 
1213     @Override
1214     public ByteBuf retainedSlice() {
1215         return slice().retain();
1216     }
1217 
1218     @Override
1219     public ByteBuf slice(int index, int length) {
1220         ensureAccessible();
1221         return new UnpooledSlicedByteBuf(this, index, length);
1222     }
1223 
1224     @Override
1225     public ByteBuf retainedSlice(int index, int length) {
1226         return slice(index, length).retain();
1227     }
1228 
1229     @Override
1230     public ByteBuffer nioBuffer() {
1231         return nioBuffer(readerIndex, readableBytes());
1232     }
1233 
1234     @Override
1235     public ByteBuffer[] nioBuffers() {
1236         return nioBuffers(readerIndex, readableBytes());
1237     }
1238 
1239     @Override
1240     public String toString(Charset charset) {
1241         return toString(readerIndex, readableBytes(), charset);
1242     }
1243 
1244     @Override
1245     public String toString(int index, int length, Charset charset) {
1246         return ByteBufUtil.decodeString(this, index, length, charset);
1247     }
1248 
1249     @Override
1250     public int indexOf(int fromIndex, int toIndex, byte value) {
1251         if (fromIndex <= toIndex) {
1252             return ByteBufUtil.firstIndexOf(this, fromIndex, toIndex, value);
1253         }
1254         return ByteBufUtil.lastIndexOf(this, fromIndex, toIndex, value);
1255     }
1256 
1257     @Override
1258     public int bytesBefore(byte value) {
1259         return bytesBefore(readerIndex(), readableBytes(), value);
1260     }
1261 
1262     @Override
1263     public int bytesBefore(int length, byte value) {
1264         checkReadableBytes(length);
1265         return bytesBefore(readerIndex(), length, value);
1266     }
1267 
1268     @Override
1269     public int bytesBefore(int index, int length, byte value) {
1270         int endIndex = indexOf(index, index + length, value);
1271         if (endIndex < 0) {
1272             return -1;
1273         }
1274         return endIndex - index;
1275     }
1276 
1277     @Override
1278     public int forEachByte(ByteProcessor processor) {
1279         ensureAccessible();
1280         try {
1281             return forEachByteAsc0(readerIndex, writerIndex, processor);
1282         } catch (Exception e) {
1283             PlatformDependent.throwException(e);
1284             return -1;
1285         }
1286     }
1287 
1288     @Override
1289     public int forEachByte(int index, int length, ByteProcessor processor) {
1290         checkIndex(index, length);
1291         try {
1292             return forEachByteAsc0(index, index + length, processor);
1293         } catch (Exception e) {
1294             PlatformDependent.throwException(e);
1295             return -1;
1296         }
1297     }
1298 
1299     int forEachByteAsc0(int start, int end, ByteProcessor processor) throws Exception {
1300         for (; start < end; ++start) {
1301             if (!processor.process(_getByte(start))) {
1302                 return start;
1303             }
1304         }
1305 
1306         return -1;
1307     }
1308 
1309     @Override
1310     public int forEachByteDesc(ByteProcessor processor) {
1311         ensureAccessible();
1312         try {
1313             return forEachByteDesc0(writerIndex - 1, readerIndex, processor);
1314         } catch (Exception e) {
1315             PlatformDependent.throwException(e);
1316             return -1;
1317         }
1318     }
1319 
1320     @Override
1321     public int forEachByteDesc(int index, int length, ByteProcessor processor) {
1322         checkIndex(index, length);
1323         try {
1324             return forEachByteDesc0(index + length - 1, index, processor);
1325         } catch (Exception e) {
1326             PlatformDependent.throwException(e);
1327             return -1;
1328         }
1329     }
1330 
1331     int forEachByteDesc0(int rStart, final int rEnd, ByteProcessor processor) throws Exception {
1332         for (; rStart >= rEnd; --rStart) {
1333             if (!processor.process(_getByte(rStart))) {
1334                 return rStart;
1335             }
1336         }
1337         return -1;
1338     }
1339 
1340     @Override
1341     public int hashCode() {
1342         return ByteBufUtil.hashCode(this);
1343     }
1344 
1345     @Override
1346     public boolean equals(Object o) {
1347         return o instanceof ByteBuf && ByteBufUtil.equals(this, (ByteBuf) o);
1348     }
1349 
1350     @Override
1351     public int compareTo(ByteBuf that) {
1352         return ByteBufUtil.compare(this, that);
1353     }
1354 
1355     @Override
1356     public String toString() {
1357         if (refCnt() == 0) {
1358             return StringUtil.simpleClassName(this) + "(freed)";
1359         }
1360 
1361         StringBuilder buf = new StringBuilder()
1362             .append(StringUtil.simpleClassName(this))
1363             .append("(ridx: ").append(readerIndex)
1364             .append(", widx: ").append(writerIndex)
1365             .append(", cap: ").append(capacity());
1366         if (maxCapacity != Integer.MAX_VALUE) {
1367             buf.append('/').append(maxCapacity);
1368         }
1369 
1370         ByteBuf unwrapped = unwrap();
1371         if (unwrapped != null) {
1372             buf.append(", unwrapped: ").append(unwrapped);
1373         }
1374         buf.append(')');
1375         return buf.toString();
1376     }
1377 
1378     protected final void checkIndex(int index) {
1379         checkIndex(index, 1);
1380     }
1381 
1382     protected final void checkIndex(int index, int fieldLength) {
1383         ensureAccessible();
1384         checkIndex0(index, fieldLength);
1385     }
1386 
1387     private static void checkRangeBounds(final String indexName, final int index,
1388             final int fieldLength, final int capacity) {
1389         if (isOutOfBounds(index, fieldLength, capacity)) {
1390             throw new IndexOutOfBoundsException(String.format(
1391                     "%s: %d, length: %d (expected: range(0, %d))", indexName, index, fieldLength, capacity));
1392         }
1393     }
1394 
1395     final void checkIndex0(int index, int fieldLength) {
1396         if (checkBounds) {
1397             checkRangeBounds("index", index, fieldLength, capacity());
1398         }
1399     }
1400 
1401     protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) {
1402         checkIndex(index, length);
1403         if (checkBounds) {
1404             checkRangeBounds("srcIndex", srcIndex, length, srcCapacity);
1405         }
1406     }
1407 
1408     protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) {
1409         checkIndex(index, length);
1410         if (checkBounds) {
1411             checkRangeBounds("dstIndex", dstIndex, length, dstCapacity);
1412         }
1413     }
1414 
1415     protected final void checkDstIndex(int length, int dstIndex, int dstCapacity) {
1416         checkReadableBytes(length);
1417         if (checkBounds) {
1418             checkRangeBounds("dstIndex", dstIndex, length, dstCapacity);
1419         }
1420     }
1421 
1422     /**
1423      * Throws an {@link IndexOutOfBoundsException} if the current
1424      * {@linkplain #readableBytes() readable bytes} of this buffer is less
1425      * than the specified value.
1426      */
1427     protected final void checkReadableBytes(int minimumReadableBytes) {
1428         checkReadableBytes0(checkPositiveOrZero(minimumReadableBytes, "minimumReadableBytes"));
1429     }
1430 
1431     protected final void checkNewCapacity(int newCapacity) {
1432         ensureAccessible();
1433         if (checkBounds && (newCapacity < 0 || newCapacity > maxCapacity())) {
1434             throw new IllegalArgumentException("newCapacity: " + newCapacity +
1435                     " (expected: 0-" + maxCapacity() + ')');
1436         }
1437     }
1438 
1439     private void checkReadableBytes0(int minimumReadableBytes) {
1440         ensureAccessible();
1441         if (checkBounds && readerIndex > writerIndex - minimumReadableBytes) {
1442             throw new IndexOutOfBoundsException(String.format(
1443                     "readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s",
1444                     readerIndex, minimumReadableBytes, writerIndex, this));
1445         }
1446     }
1447 
1448     /**
1449      * Should be called by every method that tries to access the buffers content to check
1450      * if the buffer was released before.
1451      */
1452     protected final void ensureAccessible() {
1453         if (checkAccessible && !isAccessible()) {
1454             throw new IllegalReferenceCountException(0);
1455         }
1456     }
1457 
1458     final void setIndex0(int readerIndex, int writerIndex) {
1459         this.readerIndex = readerIndex;
1460         this.writerIndex = writerIndex;
1461     }
1462 
1463     final void discardMarks() {
1464         markedReaderIndex = markedWriterIndex = 0;
1465     }
1466 }