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  /**
21   * Big endian Java heap buffer implementation. It is recommended to use
22   * {@link UnpooledByteBufAllocator#heapBuffer(int, int)}, {@link Unpooled#buffer(int)} and
23   * {@link Unpooled#wrappedBuffer(byte[])} instead of calling the constructor explicitly.
24   */
25  public class UnpooledUnsafeHeapByteBuf extends UnpooledHeapByteBuf {
26  
27      /**
28       * Creates a new heap buffer with a newly allocated byte array.
29       *
30       * @param initialCapacity the initial capacity of the underlying byte array
31       * @param maxCapacity the max capacity of the underlying byte array
32       */
33      public UnpooledUnsafeHeapByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
34          super(alloc, initialCapacity, maxCapacity);
35      }
36  
37      @Override
38      protected byte[] allocateArray(int initialCapacity) {
39          return PlatformDependent.allocateUninitializedArray(initialCapacity);
40      }
41  
42      @Override
43      public byte getByte(int index) {
44          checkIndex(index);
45          return _getByte(index);
46      }
47  
48      @Override
49      protected byte _getByte(int index) {
50          return UnsafeByteBufUtil.getByte(array, index);
51      }
52  
53      @Override
54      public short getShort(int index) {
55          checkIndex(index, 2);
56          return _getShort(index);
57      }
58  
59      @Override
60      protected short _getShort(int index) {
61          return UnsafeByteBufUtil.getShort(array, index);
62      }
63  
64      @Override
65      public short getShortLE(int index) {
66          checkIndex(index, 2);
67          return _getShortLE(index);
68      }
69  
70      @Override
71      protected short _getShortLE(int index) {
72          return UnsafeByteBufUtil.getShortLE(array, index);
73      }
74  
75      @Override
76      public int getUnsignedMedium(int index) {
77          checkIndex(index, 3);
78          return _getUnsignedMedium(index);
79      }
80  
81      @Override
82      protected int _getUnsignedMedium(int index) {
83          return UnsafeByteBufUtil.getUnsignedMedium(array, index);
84      }
85  
86      @Override
87      public int getUnsignedMediumLE(int index) {
88          checkIndex(index, 3);
89          return _getUnsignedMediumLE(index);
90      }
91  
92      @Override
93      protected int _getUnsignedMediumLE(int index) {
94          return UnsafeByteBufUtil.getUnsignedMediumLE(array, index);
95      }
96  
97      @Override
98      public int getInt(int index) {
99          checkIndex(index, 4);
100         return _getInt(index);
101     }
102 
103     @Override
104     protected int _getInt(int index) {
105         return UnsafeByteBufUtil.getInt(array, index);
106     }
107 
108     @Override
109     public int getIntLE(int index) {
110         checkIndex(index, 4);
111         return _getIntLE(index);
112     }
113 
114     @Override
115     protected int _getIntLE(int index) {
116         return UnsafeByteBufUtil.getIntLE(array, index);
117     }
118 
119     @Override
120     public long getLong(int index) {
121         checkIndex(index, 8);
122         return _getLong(index);
123     }
124 
125     @Override
126     protected long _getLong(int index) {
127         return UnsafeByteBufUtil.getLong(array, index);
128     }
129 
130     @Override
131     public long getLongLE(int index) {
132         checkIndex(index, 8);
133         return _getLongLE(index);
134     }
135 
136     @Override
137     protected long _getLongLE(int index) {
138         return UnsafeByteBufUtil.getLongLE(array, index);
139     }
140 
141     @Override
142     public ByteBuf setByte(int index, int value) {
143         checkIndex(index);
144         _setByte(index, value);
145         return this;
146     }
147 
148     @Override
149     protected void _setByte(int index, int value) {
150         UnsafeByteBufUtil.setByte(array, index, value);
151     }
152 
153     @Override
154     public ByteBuf setShort(int index, int value) {
155         checkIndex(index, 2);
156         _setShort(index, value);
157         return this;
158     }
159 
160     @Override
161     protected void _setShort(int index, int value) {
162         UnsafeByteBufUtil.setShort(array, index, value);
163     }
164 
165     @Override
166     public ByteBuf setShortLE(int index, int value) {
167         checkIndex(index, 2);
168         _setShortLE(index, value);
169         return this;
170     }
171 
172     @Override
173     protected void _setShortLE(int index, int value) {
174         UnsafeByteBufUtil.setShortLE(array, index, value);
175     }
176 
177     @Override
178     public ByteBuf setMedium(int index, int   value) {
179         checkIndex(index, 3);
180         _setMedium(index, value);
181         return this;
182     }
183 
184     @Override
185     protected void _setMedium(int index, int value) {
186         UnsafeByteBufUtil.setMedium(array, index, value);
187     }
188 
189     @Override
190     public ByteBuf setMediumLE(int index, int   value) {
191         checkIndex(index, 3);
192         _setMediumLE(index, value);
193         return this;
194     }
195 
196     @Override
197     protected void _setMediumLE(int index, int value) {
198         UnsafeByteBufUtil.setMediumLE(array, index, value);
199     }
200 
201     @Override
202     public ByteBuf setInt(int index, int   value) {
203         checkIndex(index, 4);
204         _setInt(index, value);
205         return this;
206     }
207 
208     @Override
209     protected void _setInt(int index, int value) {
210         UnsafeByteBufUtil.setInt(array, index, value);
211     }
212 
213     @Override
214     public ByteBuf setIntLE(int index, int   value) {
215         checkIndex(index, 4);
216         _setIntLE(index, value);
217         return this;
218     }
219 
220     @Override
221     protected void _setIntLE(int index, int value) {
222         UnsafeByteBufUtil.setIntLE(array, index, value);
223     }
224 
225     @Override
226     public ByteBuf setLong(int index, long  value) {
227         checkIndex(index, 8);
228         _setLong(index, value);
229         return this;
230     }
231 
232     @Override
233     protected void _setLong(int index, long value) {
234         UnsafeByteBufUtil.setLong(array, index, value);
235     }
236 
237     @Override
238     public ByteBuf setLongLE(int index, long  value) {
239         checkIndex(index, 8);
240         _setLongLE(index, value);
241         return this;
242     }
243 
244     @Override
245     protected void _setLongLE(int index, long value) {
246         UnsafeByteBufUtil.setLongLE(array, index, value);
247     }
248 
249     @Override
250     public ByteBuf setZero(int index, int length) {
251         checkIndex(index, length);
252         UnsafeByteBufUtil.setZero(array, index, length);
253         return this;
254     }
255 
256     @Override
257     public ByteBuf writeZero(int length) {
258         ensureWritable(length);
259         int wIndex = writerIndex;
260         UnsafeByteBufUtil.setZero(array, wIndex, length);
261         writerIndex = wIndex + length;
262         return this;
263     }
264 
265     @Override
266     @Deprecated
267     protected SwappedByteBuf newSwappedByteBuf() {
268         if (PlatformDependent.isUnaligned()) {
269             // Only use if unaligned access is supported otherwise there is no gain.
270             return new UnsafeHeapSwappedByteBuf(this);
271         }
272         return super.newSwappedByteBuf();
273     }
274 }