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