View Javadoc

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