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