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