View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.buffer;
21  
22  import java.io.FilterOutputStream;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.nio.ByteBuffer;
26  import java.nio.ByteOrder;
27  import java.nio.CharBuffer;
28  import java.nio.DoubleBuffer;
29  import java.nio.FloatBuffer;
30  import java.nio.IntBuffer;
31  import java.nio.LongBuffer;
32  import java.nio.ShortBuffer;
33  import java.nio.charset.CharacterCodingException;
34  import java.nio.charset.CharsetDecoder;
35  import java.nio.charset.CharsetEncoder;
36  import java.util.EnumSet;
37  import java.util.Set;
38  
39  /**
40   * A {@link IoBuffer} that wraps a buffer and proxies any operations to it.
41   * <p>
42   * You can think this class like a {@link FilterOutputStream}.  All operations
43   * are proxied by default so that you can extend this class and override existing
44   * operations selectively.  You can introduce new operations, too.
45   *
46   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
47   */
48  public class IoBufferWrapper extends IoBuffer {
49  
50      /**
51       * The buffer proxied by this proxy.
52       */
53      private final IoBuffer buf;
54  
55      /**
56       * Create a new instance.
57       * @param buf the buffer to be proxied
58       */
59      protected IoBufferWrapper(IoBuffer buf) {
60          if (buf == null) {
61              throw new NullPointerException("buf");
62          }
63          this.buf = buf;
64      }
65      
66      /**
67       * Returns the parent buffer that this buffer wrapped.
68       */
69      public IoBuffer getParentBuffer() {
70          return buf;
71      }
72  
73      @Override
74      public boolean isDirect() {
75          return buf.isDirect();
76      }
77  
78      @Override
79      public ByteBuffer buf() {
80          return buf.buf();
81      }
82  
83      @Override
84      public int capacity() {
85          return buf.capacity();
86      }
87  
88      @Override
89      public int position() {
90          return buf.position();
91      }
92  
93      @Override
94      public IoBuffer position(int newPosition) {
95          buf.position(newPosition);
96          return this;
97      }
98  
99      @Override
100     public int limit() {
101         return buf.limit();
102     }
103 
104     @Override
105     public IoBuffer limit(int newLimit) {
106         buf.limit(newLimit);
107         return this;
108     }
109 
110     @Override
111     public IoBuffer mark() {
112         buf.mark();
113         return this;
114     }
115 
116     @Override
117     public IoBuffer reset() {
118         buf.reset();
119         return this;
120     }
121 
122     @Override
123     public IoBuffer clear() {
124         buf.clear();
125         return this;
126     }
127 
128     @Override
129     public IoBuffer sweep() {
130         buf.sweep();
131         return this;
132     }
133 
134     @Override
135     public IoBuffer sweep(byte value) {
136         buf.sweep(value);
137         return this;
138     }
139 
140     @Override
141     public IoBuffer flip() {
142         buf.flip();
143         return this;
144     }
145 
146     @Override
147     public IoBuffer rewind() {
148         buf.rewind();
149         return this;
150     }
151 
152     @Override
153     public int remaining() {
154         return buf.remaining();
155     }
156 
157     @Override
158     public boolean hasRemaining() {
159         return buf.hasRemaining();
160     }
161 
162     @Override
163     public byte get() {
164         return buf.get();
165     }
166 
167     @Override
168     public short getUnsigned() {
169         return buf.getUnsigned();
170     }
171 
172     @Override
173     public IoBuffer put(byte b) {
174         buf.put(b);
175         return this;
176     }
177 
178     @Override
179     public byte get(int index) {
180         return buf.get(index);
181     }
182 
183     @Override
184     public short getUnsigned(int index) {
185         return buf.getUnsigned(index);
186     }
187 
188     @Override
189     public IoBuffer put(int index, byte b) {
190         buf.put(index, b);
191         return this;
192     }
193 
194     @Override
195     public IoBuffer get(byte[] dst, int offset, int length) {
196         buf.get(dst, offset, length);
197         return this;
198     }
199 
200     @Override
201     public IoBuffer getSlice(int index, int length) {
202         return buf.getSlice(index, length);
203     }
204 
205     @Override
206     public IoBuffer getSlice(int length) {
207         return buf.getSlice(length);
208     }
209 
210     @Override
211     public IoBuffer get(byte[] dst) {
212         buf.get(dst);
213         return this;
214     }
215 
216     @Override
217     public IoBuffer put(IoBuffer src) {
218         buf.put(src);
219         return this;
220     }
221 
222     @Override
223     public IoBuffer put(ByteBuffer src) {
224         buf.put(src);
225         return this;
226     }
227 
228     @Override
229     public IoBuffer put(byte[] src, int offset, int length) {
230         buf.put(src, offset, length);
231         return this;
232     }
233 
234     @Override
235     public IoBuffer put(byte[] src) {
236         buf.put(src);
237         return this;
238     }
239 
240     @Override
241     public IoBuffer compact() {
242         buf.compact();
243         return this;
244     }
245 
246     @Override
247     public String toString() {
248         return buf.toString();
249     }
250 
251     @Override
252     public int hashCode() {
253         return buf.hashCode();
254     }
255 
256     @Override
257     public boolean equals(Object ob) {
258         return buf.equals(ob);
259     }
260 
261     public int compareTo(IoBuffer that) {
262         return buf.compareTo(that);
263     }
264 
265     @Override
266     public ByteOrder order() {
267         return buf.order();
268     }
269 
270     @Override
271     public IoBuffer order(ByteOrder bo) {
272         buf.order(bo);
273         return this;
274     }
275 
276     @Override
277     public char getChar() {
278         return buf.getChar();
279     }
280 
281     @Override
282     public IoBuffer putChar(char value) {
283         buf.putChar(value);
284         return this;
285     }
286 
287     @Override
288     public char getChar(int index) {
289         return buf.getChar(index);
290     }
291 
292     @Override
293     public IoBuffer putChar(int index, char value) {
294         buf.putChar(index, value);
295         return this;
296     }
297 
298     @Override
299     public CharBuffer asCharBuffer() {
300         return buf.asCharBuffer();
301     }
302 
303     @Override
304     public short getShort() {
305         return buf.getShort();
306     }
307 
308     @Override
309     public int getUnsignedShort() {
310         return buf.getUnsignedShort();
311     }
312 
313     @Override
314     public IoBuffer putShort(short value) {
315         buf.putShort(value);
316         return this;
317     }
318 
319     @Override
320     public short getShort(int index) {
321         return buf.getShort(index);
322     }
323 
324     @Override
325     public int getUnsignedShort(int index) {
326         return buf.getUnsignedShort(index);
327     }
328 
329     @Override
330     public IoBuffer putShort(int index, short value) {
331         buf.putShort(index, value);
332         return this;
333     }
334 
335     @Override
336     public ShortBuffer asShortBuffer() {
337         return buf.asShortBuffer();
338     }
339 
340     @Override
341     public int getInt() {
342         return buf.getInt();
343     }
344 
345     @Override
346     public long getUnsignedInt() {
347         return buf.getUnsignedInt();
348     }
349 
350     @Override
351     public IoBuffer putInt(int value) {
352         buf.putInt(value);
353         return this;
354     }
355 
356     @Override
357     public int getInt(int index) {
358         return buf.getInt(index);
359     }
360 
361     @Override
362     public long getUnsignedInt(int index) {
363         return buf.getUnsignedInt(index);
364     }
365 
366     @Override
367     public IoBuffer putInt(int index, int value) {
368         buf.putInt(index, value);
369         return this;
370     }
371 
372     @Override
373     public IntBuffer asIntBuffer() {
374         return buf.asIntBuffer();
375     }
376 
377     @Override
378     public long getLong() {
379         return buf.getLong();
380     }
381 
382     @Override
383     public IoBuffer putLong(long value) {
384         buf.putLong(value);
385         return this;
386     }
387 
388     @Override
389     public long getLong(int index) {
390         return buf.getLong(index);
391     }
392 
393     @Override
394     public IoBuffer putLong(int index, long value) {
395         buf.putLong(index, value);
396         return this;
397     }
398 
399     @Override
400     public LongBuffer asLongBuffer() {
401         return buf.asLongBuffer();
402     }
403 
404     @Override
405     public float getFloat() {
406         return buf.getFloat();
407     }
408 
409     @Override
410     public IoBuffer putFloat(float value) {
411         buf.putFloat(value);
412         return this;
413     }
414 
415     @Override
416     public float getFloat(int index) {
417         return buf.getFloat(index);
418     }
419 
420     @Override
421     public IoBuffer putFloat(int index, float value) {
422         buf.putFloat(index, value);
423         return this;
424     }
425 
426     @Override
427     public FloatBuffer asFloatBuffer() {
428         return buf.asFloatBuffer();
429     }
430 
431     @Override
432     public double getDouble() {
433         return buf.getDouble();
434     }
435 
436     @Override
437     public IoBuffer putDouble(double value) {
438         buf.putDouble(value);
439         return this;
440     }
441 
442     @Override
443     public double getDouble(int index) {
444         return buf.getDouble(index);
445     }
446 
447     @Override
448     public IoBuffer putDouble(int index, double value) {
449         buf.putDouble(index, value);
450         return this;
451     }
452 
453     @Override
454     public DoubleBuffer asDoubleBuffer() {
455         return buf.asDoubleBuffer();
456     }
457 
458     @Override
459     public String getHexDump() {
460         return buf.getHexDump();
461     }
462 
463     @Override
464     public String getString(int fieldSize, CharsetDecoder decoder)
465             throws CharacterCodingException {
466         return buf.getString(fieldSize, decoder);
467     }
468 
469     @Override
470     public String getString(CharsetDecoder decoder)
471             throws CharacterCodingException {
472         return buf.getString(decoder);
473     }
474 
475     @Override
476     public String getPrefixedString(CharsetDecoder decoder)
477             throws CharacterCodingException {
478         return buf.getPrefixedString(decoder);
479     }
480 
481     @Override
482     public String getPrefixedString(int prefixLength, CharsetDecoder decoder)
483             throws CharacterCodingException {
484         return buf.getPrefixedString(prefixLength, decoder);
485     }
486 
487     @Override
488     public IoBuffer putString(CharSequence in, int fieldSize,
489             CharsetEncoder encoder) throws CharacterCodingException {
490         buf.putString(in, fieldSize, encoder);
491         return this;
492     }
493 
494     @Override
495     public IoBuffer putString(CharSequence in, CharsetEncoder encoder)
496             throws CharacterCodingException {
497         buf.putString(in, encoder);
498         return this;
499     }
500 
501     @Override
502     public IoBuffer putPrefixedString(CharSequence in, CharsetEncoder encoder)
503             throws CharacterCodingException {
504         buf.putPrefixedString(in, encoder);
505         return this;
506     }
507 
508     @Override
509     public IoBuffer putPrefixedString(CharSequence in, int prefixLength,
510             CharsetEncoder encoder) throws CharacterCodingException {
511         buf.putPrefixedString(in, prefixLength, encoder);
512         return this;
513     }
514 
515     @Override
516     public IoBuffer putPrefixedString(CharSequence in, int prefixLength,
517             int padding, CharsetEncoder encoder)
518             throws CharacterCodingException {
519         buf.putPrefixedString(in, prefixLength, padding, encoder);
520         return this;
521     }
522 
523     @Override
524     public IoBuffer putPrefixedString(CharSequence in, int prefixLength,
525             int padding, byte padValue, CharsetEncoder encoder)
526             throws CharacterCodingException {
527         buf.putPrefixedString(in, prefixLength, padding, padValue, encoder);
528         return this;
529     }
530 
531     @Override
532     public IoBuffer skip(int size) {
533         buf.skip(size);
534         return this;
535     }
536 
537     @Override
538     public IoBuffer fill(byte value, int size) {
539         buf.fill(value, size);
540         return this;
541     }
542 
543     @Override
544     public IoBuffer fillAndReset(byte value, int size) {
545         buf.fillAndReset(value, size);
546         return this;
547     }
548 
549     @Override
550     public IoBuffer fill(int size) {
551         buf.fill(size);
552         return this;
553     }
554 
555     @Override
556     public IoBuffer fillAndReset(int size) {
557         buf.fillAndReset(size);
558         return this;
559     }
560 
561     @Override
562     public boolean isAutoExpand() {
563         return buf.isAutoExpand();
564     }
565 
566     @Override
567     public IoBuffer setAutoExpand(boolean autoExpand) {
568         buf.setAutoExpand(autoExpand);
569         return this;
570     }
571 
572     @Override
573     public IoBuffer expand(int pos, int expectedRemaining) {
574         buf.expand(pos, expectedRemaining);
575         return this;
576     }
577 
578     @Override
579     public IoBuffer expand(int expectedRemaining) {
580         buf.expand(expectedRemaining);
581         return this;
582     }
583 
584     @Override
585     public Object getObject() throws ClassNotFoundException {
586         return buf.getObject();
587     }
588 
589     @Override
590     public Object getObject(ClassLoader classLoader)
591             throws ClassNotFoundException {
592         return buf.getObject(classLoader);
593     }
594 
595     @Override
596     public IoBuffer putObject(Object o) {
597         buf.putObject(o);
598         return this;
599     }
600 
601     @Override
602     public InputStream asInputStream() {
603         return buf.asInputStream();
604     }
605 
606     @Override
607     public OutputStream asOutputStream() {
608         return buf.asOutputStream();
609     }
610 
611     @Override
612     public IoBuffer duplicate() {
613         return buf.duplicate();
614     }
615 
616     @Override
617     public IoBuffer slice() {
618         return buf.slice();
619     }
620 
621     @Override
622     public IoBuffer asReadOnlyBuffer() {
623         return buf.asReadOnlyBuffer();
624     }
625 
626     @Override
627     public byte[] array() {
628         return buf.array();
629     }
630 
631     @Override
632     public int arrayOffset() {
633         return buf.arrayOffset();
634     }
635 
636     @Override
637     public int minimumCapacity() {
638         return buf.minimumCapacity();
639     }
640 
641     @Override
642     public IoBuffer minimumCapacity(int minimumCapacity) {
643         buf.minimumCapacity(minimumCapacity);
644         return this;
645     }
646 
647     @Override
648     public IoBuffer capacity(int newCapacity) {
649         buf.capacity(newCapacity);
650         return this;
651     }
652 
653     @Override
654     public boolean isReadOnly() {
655         return buf.isReadOnly();
656     }
657 
658     @Override
659     public int markValue() {
660         return buf.markValue();
661     }
662 
663     @Override
664     public boolean hasArray() {
665         return buf.hasArray();
666     }
667 
668     @Override
669     public void free() {
670         buf.free();
671     }
672 
673     @Override
674     public boolean isDerived() {
675         return buf.isDerived();
676     }
677 
678     @Override
679     public boolean isAutoShrink() {
680         return buf.isAutoShrink();
681     }
682 
683     @Override
684     public IoBuffer setAutoShrink(boolean autoShrink) {
685         buf.setAutoShrink(autoShrink);
686         return this;
687     }
688 
689     @Override
690     public IoBuffer shrink() {
691         buf.shrink();
692         return this;
693     }
694 
695     @Override
696     public int getMediumInt() {
697         return buf.getMediumInt();
698     }
699 
700     @Override
701     public int getUnsignedMediumInt() {
702         return buf.getUnsignedMediumInt();
703     }
704 
705     @Override
706     public int getMediumInt(int index) {
707         return buf.getMediumInt(index);
708     }
709 
710     @Override
711     public int getUnsignedMediumInt(int index) {
712         return buf.getUnsignedMediumInt(index);
713     }
714 
715     @Override
716     public IoBuffer putMediumInt(int value) {
717         buf.putMediumInt(value);
718         return this;
719     }
720 
721     @Override
722     public IoBuffer putMediumInt(int index, int value) {
723         buf.putMediumInt(index, value);
724         return this;
725     }
726 
727     @Override
728     public String getHexDump(int lengthLimit) {
729         return buf.getHexDump(lengthLimit);
730     }
731 
732     @Override
733     public boolean prefixedDataAvailable(int prefixLength) {
734         return buf.prefixedDataAvailable(prefixLength);
735     }
736 
737     @Override
738     public boolean prefixedDataAvailable(int prefixLength, int maxDataLength) {
739         return buf.prefixedDataAvailable(prefixLength, maxDataLength);
740     }
741 
742     @Override
743     public int indexOf(byte b) {
744         return buf.indexOf(b);
745     }
746 
747     @Override
748     public <E extends Enum<E>> E getEnum(Class<E> enumClass) {
749         return buf.getEnum(enumClass);
750     }
751 
752     @Override
753     public <E extends Enum<E>> E getEnum(int index, Class<E> enumClass) {
754         return buf.getEnum(index, enumClass);
755     }
756 
757     @Override
758     public <E extends Enum<E>> E getEnumShort(Class<E> enumClass) {
759         return buf.getEnumShort(enumClass);
760     }
761 
762     @Override
763     public <E extends Enum<E>> E getEnumShort(int index, Class<E> enumClass) {
764         return buf.getEnumShort(index, enumClass);
765     }
766 
767     @Override
768     public <E extends Enum<E>> E getEnumInt(Class<E> enumClass) {
769         return buf.getEnumInt(enumClass);
770     }
771 
772     @Override
773     public <E extends Enum<E>> E getEnumInt(int index, Class<E> enumClass) {
774         return buf.getEnumInt(index, enumClass);
775     }
776 
777     @Override
778     public IoBuffer putEnum(Enum<?> e) {
779         buf.putEnum(e);
780         return this;
781     }
782 
783     @Override
784     public IoBuffer putEnum(int index, Enum<?> e) {
785         buf.putEnum(index, e);
786         return this;
787     }
788 
789     @Override
790     public IoBuffer putEnumShort(Enum<?> e) {
791         buf.putEnumShort(e);
792         return this;
793     }
794 
795     @Override
796     public IoBuffer putEnumShort(int index, Enum<?> e) {
797         buf.putEnumShort(index, e);
798         return this;
799     }
800 
801     @Override
802     public IoBuffer putEnumInt(Enum<?> e) {
803         buf.putEnumInt(e);
804         return this;
805     }
806 
807     @Override
808     public IoBuffer putEnumInt(int index, Enum<?> e) {
809         buf.putEnumInt(index, e);
810         return this;
811     }
812 
813     @Override
814     public <E extends Enum<E>> EnumSet<E> getEnumSet(Class<E> enumClass) {
815         return buf.getEnumSet(enumClass);
816     }
817 
818     @Override
819     public <E extends Enum<E>> EnumSet<E> getEnumSet(int index, Class<E> enumClass) {
820         return buf.getEnumSet(index, enumClass);
821     }
822 
823     @Override
824     public <E extends Enum<E>> EnumSet<E> getEnumSetShort(Class<E> enumClass) {
825         return buf.getEnumSetShort(enumClass);
826     }
827 
828     @Override
829     public <E extends Enum<E>> EnumSet<E> getEnumSetShort(int index, Class<E> enumClass) {
830         return buf.getEnumSetShort(index, enumClass);
831     }
832 
833     @Override
834     public <E extends Enum<E>> EnumSet<E> getEnumSetInt(Class<E> enumClass) {
835         return buf.getEnumSetInt(enumClass);
836     }
837 
838     @Override
839     public <E extends Enum<E>> EnumSet<E> getEnumSetInt(int index, Class<E> enumClass) {
840         return buf.getEnumSetInt(index, enumClass);
841     }
842 
843     @Override
844     public <E extends Enum<E>> EnumSet<E> getEnumSetLong(Class<E> enumClass) {
845         return buf.getEnumSetLong(enumClass);
846     }
847 
848     @Override
849     public <E extends Enum<E>> EnumSet<E> getEnumSetLong(int index, Class<E> enumClass) {
850         return buf.getEnumSetLong(index, enumClass);
851     }
852 
853     @Override
854     public <E extends Enum<E>> IoBuffer putEnumSet(Set<E> set) {
855         buf.putEnumSet(set);
856         return this;
857     }
858 
859     @Override
860     public <E extends Enum<E>> IoBuffer putEnumSet(int index, Set<E> set) {
861         buf.putEnumSet(index, set);
862         return this;
863     }
864 
865     @Override
866     public <E extends Enum<E>> IoBuffer putEnumSetShort(Set<E> set) {
867         buf.putEnumSetShort(set);
868         return this;
869     }
870 
871     @Override
872     public <E extends Enum<E>> IoBuffer putEnumSetShort(int index, Set<E> set) {
873         buf.putEnumSetShort(index, set);
874         return this;
875     }
876 
877     @Override
878     public <E extends Enum<E>> IoBuffer putEnumSetInt(Set<E> set) {
879         buf.putEnumSetInt(set);
880         return this;
881     }
882 
883     @Override
884     public <E extends Enum<E>> IoBuffer putEnumSetInt(int index, Set<E> set) {
885         buf.putEnumSetInt(index, set);
886         return this;
887     }
888 
889     @Override
890     public <E extends Enum<E>> IoBuffer putEnumSetLong(Set<E> set) {
891         buf.putEnumSetLong(set);
892         return this;
893     }
894 
895     @Override
896     public <E extends Enum<E>> IoBuffer putEnumSetLong(int index, Set<E> set) {
897         buf.putEnumSetLong(index, set);
898         return this;
899     }
900 }