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 int getUnsignedMedium(int index) {
61          checkIndex(index, 3);
62          return _getUnsignedMedium(index);
63      }
64  
65      @Override
66      protected int _getUnsignedMedium(int index) {
67          return UnsafeByteBufUtil.getUnsignedMedium(array, index);
68      }
69  
70      @Override
71      public int getInt(int index) {
72          checkIndex(index, 4);
73          return _getInt(index);
74      }
75  
76      @Override
77      protected int _getInt(int index) {
78          return UnsafeByteBufUtil.getInt(array, index);
79      }
80  
81      @Override
82      public long getLong(int index) {
83          checkIndex(index, 8);
84          return _getLong(index);
85      }
86  
87      @Override
88      protected long _getLong(int index) {
89          return UnsafeByteBufUtil.getLong(array, index);
90      }
91  
92      @Override
93      public ByteBuf setByte(int index, int value) {
94          checkIndex(index);
95          _setByte(index, value);
96          return this;
97      }
98  
99      @Override
100     protected void _setByte(int index, int value) {
101         UnsafeByteBufUtil.setByte(array, index, value);
102     }
103 
104     @Override
105     public ByteBuf setShort(int index, int value) {
106         checkIndex(index, 2);
107         _setShort(index, value);
108         return this;
109     }
110 
111     @Override
112     protected void _setShort(int index, int value) {
113         UnsafeByteBufUtil.setShort(array, index, value);
114     }
115 
116     @Override
117     public ByteBuf setMedium(int index, int   value) {
118         checkIndex(index, 3);
119         _setMedium(index, value);
120         return this;
121     }
122 
123     @Override
124     protected void _setMedium(int index, int value) {
125         UnsafeByteBufUtil.setMedium(array, index, value);
126     }
127 
128     @Override
129     public ByteBuf setInt(int index, int   value) {
130         checkIndex(index, 4);
131         _setInt(index, value);
132         return this;
133     }
134 
135     @Override
136     protected void _setInt(int index, int value) {
137         UnsafeByteBufUtil.setInt(array, index, value);
138     }
139 
140     @Override
141     public ByteBuf setLong(int index, long  value) {
142         checkIndex(index, 8);
143         _setLong(index, value);
144         return this;
145     }
146 
147     @Override
148     protected void _setLong(int index, long value) {
149         UnsafeByteBufUtil.setLong(array, index, value);
150     }
151 
152     @Override
153     public ByteBuf setZero(int index, int length) {
154         if (PlatformDependent.javaVersion() >= 7) {
155             // Only do on java7+ as the needed Unsafe call was only added there.
156             checkIndex(index, length);
157             UnsafeByteBufUtil.setZero(array, index, length);
158             return this;
159         }
160         return super.setZero(index, length);
161     }
162 
163     @Override
164     public ByteBuf writeZero(int length) {
165         if (PlatformDependent.javaVersion() >= 7) {
166             // Only do on java7+ as the needed Unsafe call was only added there.
167             ensureWritable(length);
168             int wIndex = writerIndex;
169             UnsafeByteBufUtil.setZero(array, wIndex, length);
170             writerIndex = wIndex + length;
171             return this;
172         }
173         return super.writeZero(length);
174     }
175 
176     @Override
177     @Deprecated
178     protected SwappedByteBuf newSwappedByteBuf() {
179         if (PlatformDependent.isUnaligned()) {
180             // Only use if unaligned access is supported otherwise there is no gain.
181             return new UnsafeHeapSwappedByteBuf(this);
182         }
183         return super.newSwappedByteBuf();
184     }
185 }