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