View Javadoc

1   /*
2    * Copyright 2015 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 io.netty.util.internal.PlatformDependent;
19  
20  import java.nio.ByteOrder;
21  
22  /**
23   * Special {@link SwappedByteBuf} for {@link ByteBuf}s that is using unsafe.
24   */
25  abstract class AbstractUnsafeSwappedByteBuf extends SwappedByteBuf {
26      private final boolean nativeByteOrder;
27      private final AbstractByteBuf wrapped;
28  
29      AbstractUnsafeSwappedByteBuf(AbstractByteBuf buf) {
30          super(buf);
31          assert PlatformDependent.isUnaligned();
32          wrapped = buf;
33          nativeByteOrder = UnsafeByteBufUtil.BIG_ENDIAN_NATIVE_ORDER == (order() == ByteOrder.BIG_ENDIAN);
34      }
35  
36      @Override
37      public final long getLong(int index) {
38          wrapped.checkIndex(index, 8);
39          long v = _getLong(wrapped, index);
40          return nativeByteOrder ? v : Long.reverseBytes(v);
41      }
42  
43      @Override
44      public final float getFloat(int index) {
45          return Float.intBitsToFloat(getInt(index));
46      }
47  
48      @Override
49      public final double getDouble(int index) {
50          return Double.longBitsToDouble(getLong(index));
51      }
52  
53      @Override
54      public final char getChar(int index) {
55          return (char) getShort(index);
56      }
57  
58      @Override
59      public final long getUnsignedInt(int index) {
60          return getInt(index) & 0xFFFFFFFFL;
61      }
62  
63      @Override
64      public final int getInt(int index) {
65          wrapped.checkIndex0(index, 4);
66          int v = _getInt(wrapped, index);
67          return nativeByteOrder ? v : Integer.reverseBytes(v);
68      }
69  
70      @Override
71      public final int getUnsignedShort(int index) {
72          return getShort(index) & 0xFFFF;
73      }
74  
75      @Override
76      public final short getShort(int index) {
77          wrapped.checkIndex0(index, 2);
78          short v = _getShort(wrapped, index);
79          return nativeByteOrder ? v : Short.reverseBytes(v);
80      }
81  
82      @Override
83      public final ByteBuf setShort(int index, int value) {
84          wrapped.checkIndex0(index, 2);
85          _setShort(wrapped, index, nativeByteOrder ? (short) value : Short.reverseBytes((short) value));
86          return this;
87      }
88  
89      @Override
90      public final ByteBuf setInt(int index, int value) {
91          wrapped.checkIndex0(index, 4);
92          _setInt(wrapped, index, nativeByteOrder ? value : Integer.reverseBytes(value));
93          return this;
94      }
95  
96      @Override
97      public final ByteBuf setLong(int index, long value) {
98          wrapped.checkIndex(index, 8);
99          _setLong(wrapped, index, nativeByteOrder ? value : Long.reverseBytes(value));
100         return this;
101     }
102 
103     @Override
104     public final ByteBuf setChar(int index, int value) {
105         setShort(index, value);
106         return this;
107     }
108 
109     @Override
110     public final ByteBuf setFloat(int index, float value) {
111         setInt(index, Float.floatToRawIntBits(value));
112         return this;
113     }
114 
115     @Override
116     public final ByteBuf setDouble(int index, double value) {
117         setLong(index, Double.doubleToRawLongBits(value));
118         return this;
119     }
120 
121     @Override
122     public final ByteBuf writeShort(int value) {
123         wrapped.ensureWritable0(2);
124         _setShort(wrapped, wrapped.writerIndex, nativeByteOrder ? (short) value : Short.reverseBytes((short) value));
125         wrapped.writerIndex += 2;
126         return this;
127     }
128 
129     @Override
130     public final ByteBuf writeInt(int value) {
131         wrapped.ensureWritable0(4);
132         _setInt(wrapped, wrapped.writerIndex, nativeByteOrder ? value : Integer.reverseBytes(value));
133         wrapped.writerIndex += 4;
134         return this;
135     }
136 
137     @Override
138     public final ByteBuf writeLong(long value) {
139         wrapped.ensureWritable0(8);
140         _setLong(wrapped, wrapped.writerIndex, nativeByteOrder ? value : Long.reverseBytes(value));
141         wrapped.writerIndex += 8;
142         return this;
143     }
144 
145     @Override
146     public final ByteBuf writeChar(int value) {
147         writeShort(value);
148         return this;
149     }
150 
151     @Override
152     public final ByteBuf writeFloat(float value) {
153         writeInt(Float.floatToRawIntBits(value));
154         return this;
155     }
156 
157     @Override
158     public final ByteBuf writeDouble(double value) {
159         writeLong(Double.doubleToRawLongBits(value));
160         return this;
161     }
162 
163     protected abstract short _getShort(AbstractByteBuf wrapped, int index);
164     protected abstract int _getInt(AbstractByteBuf wrapped, int index);
165     protected abstract long _getLong(AbstractByteBuf wrapped, int index);
166     protected abstract void _setShort(AbstractByteBuf wrapped, int index, short value);
167     protected abstract void _setInt(AbstractByteBuf wrapped, int index, int value);
168     protected abstract void _setLong(AbstractByteBuf wrapped, int index, long value);
169 }