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.Recycler;
19  import io.netty.util.Recycler.Handle;
20  import io.netty.util.internal.PlatformDependent;
21  
22  final class PooledUnsafeHeapByteBuf extends PooledHeapByteBuf {
23  
24      private static final Recycler<PooledUnsafeHeapByteBuf> RECYCLER = new Recycler<PooledUnsafeHeapByteBuf>() {
25          @Override
26          protected PooledUnsafeHeapByteBuf newObject(Handle<PooledUnsafeHeapByteBuf> handle) {
27              return new PooledUnsafeHeapByteBuf(handle, 0);
28          }
29      };
30  
31      static PooledUnsafeHeapByteBuf newUnsafeInstance(int maxCapacity) {
32          PooledUnsafeHeapByteBuf buf = RECYCLER.get();
33          buf.reuse(maxCapacity);
34          return buf;
35      }
36  
37      private PooledUnsafeHeapByteBuf(Handle<PooledUnsafeHeapByteBuf> recyclerHandle, int maxCapacity) {
38          super(recyclerHandle, maxCapacity);
39      }
40  
41      @Override
42      protected byte _getByte(int index) {
43          return UnsafeByteBufUtil.getByte(memory, idx(index));
44      }
45  
46      @Override
47      protected short _getShort(int index) {
48          return UnsafeByteBufUtil.getShort(memory, idx(index));
49      }
50  
51      @Override
52      protected short _getShortLE(int index) {
53          return UnsafeByteBufUtil.getShortLE(memory, idx(index));
54      }
55  
56      @Override
57      protected int _getUnsignedMedium(int index) {
58          return UnsafeByteBufUtil.getUnsignedMedium(memory, idx(index));
59      }
60  
61      @Override
62      protected int _getUnsignedMediumLE(int index) {
63          return UnsafeByteBufUtil.getUnsignedMediumLE(memory, idx(index));
64      }
65  
66      @Override
67      protected int _getInt(int index) {
68          return UnsafeByteBufUtil.getInt(memory, idx(index));
69      }
70  
71      @Override
72      protected int _getIntLE(int index) {
73          return UnsafeByteBufUtil.getIntLE(memory, idx(index));
74      }
75  
76      @Override
77      protected long _getLong(int index) {
78          return UnsafeByteBufUtil.getLong(memory, idx(index));
79      }
80  
81      @Override
82      protected long _getLongLE(int index) {
83          return UnsafeByteBufUtil.getLongLE(memory, idx(index));
84      }
85  
86      @Override
87      protected void _setByte(int index, int value) {
88          UnsafeByteBufUtil.setByte(memory, idx(index), value);
89      }
90  
91      @Override
92      protected void _setShort(int index, int value) {
93          UnsafeByteBufUtil.setShort(memory, idx(index), value);
94      }
95  
96      @Override
97      protected void _setShortLE(int index, int value) {
98          UnsafeByteBufUtil.setShortLE(memory, idx(index), value);
99      }
100 
101     @Override
102     protected void _setMedium(int index, int value) {
103         UnsafeByteBufUtil.setMedium(memory, idx(index), value);
104     }
105 
106     @Override
107     protected void _setMediumLE(int index, int value) {
108         UnsafeByteBufUtil.setMediumLE(memory, idx(index), value);
109     }
110 
111     @Override
112     protected void _setInt(int index, int value) {
113         UnsafeByteBufUtil.setInt(memory, idx(index), value);
114     }
115 
116     @Override
117     protected void _setIntLE(int index, int value) {
118         UnsafeByteBufUtil.setIntLE(memory, idx(index), value);
119     }
120 
121     @Override
122     protected void _setLong(int index, long value) {
123         UnsafeByteBufUtil.setLong(memory, idx(index), value);
124     }
125 
126     @Override
127     protected void _setLongLE(int index, long value) {
128         UnsafeByteBufUtil.setLongLE(memory, idx(index), value);
129     }
130 
131     @Override
132     public ByteBuf setZero(int index, int length) {
133         if (PlatformDependent.javaVersion() >= 7) {
134             checkIndex(index, length);
135             // Only do on java7+ as the needed Unsafe call was only added there.
136             UnsafeByteBufUtil.setZero(memory, idx(index), length);
137             return this;
138         }
139         return super.setZero(index, length);
140     }
141 
142     @Override
143     public ByteBuf writeZero(int length) {
144         if (PlatformDependent.javaVersion() >= 7) {
145             // Only do on java7+ as the needed Unsafe call was only added there.
146             ensureWritable(length);
147             int wIndex = writerIndex;
148             UnsafeByteBufUtil.setZero(memory, idx(wIndex), length);
149             writerIndex = wIndex + length;
150             return this;
151         }
152         return super.writeZero(length);
153     }
154 
155     @Override
156     @Deprecated
157     protected SwappedByteBuf newSwappedByteBuf() {
158         if (PlatformDependent.isUnaligned()) {
159             // Only use if unaligned access is supported otherwise there is no gain.
160             return new UnsafeHeapSwappedByteBuf(this);
161         }
162         return super.newSwappedByteBuf();
163     }
164 }