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.ObjectUtil;
21 import io.netty.util.internal.StringUtil;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.nio.ByteBuffer;
27 import java.nio.ByteOrder;
28 import java.nio.channels.FileChannel;
29 import java.nio.channels.GatheringByteChannel;
30 import java.nio.channels.ScatteringByteChannel;
31 import java.nio.charset.Charset;
32
33 /**
34  * Wraps another {@link ByteBuf}.
35  *
36  * It's important that the {@link #readerIndex()} and {@link #writerIndex()} will not do any adjustments on the
37  * indices on the fly because of internal optimizations made by {@link ByteBufUtil#writeAscii(ByteBuf, CharSequence)}
38  * and {@link ByteBufUtil#writeUtf8(ByteBuf, CharSequence)}.
39  */

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