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 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 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 int _getUnsignedMedium(int index) {
53          return UnsafeByteBufUtil.getUnsignedMedium(memory, idx(index));
54      }
55  
56      @Override
57      protected int _getInt(int index) {
58          return UnsafeByteBufUtil.getInt(memory, idx(index));
59      }
60  
61      @Override
62      protected long _getLong(int index) {
63          return UnsafeByteBufUtil.getLong(memory, idx(index));
64      }
65  
66      @Override
67      protected void _setByte(int index, int value) {
68          UnsafeByteBufUtil.setByte(memory, idx(index), value);
69      }
70  
71      @Override
72      protected void _setShort(int index, int value) {
73          UnsafeByteBufUtil.setShort(memory, idx(index), value);
74      }
75  
76      @Override
77      protected void _setMedium(int index, int value) {
78          UnsafeByteBufUtil.setMedium(memory, idx(index), value);
79      }
80  
81      @Override
82      protected void _setInt(int index, int value) {
83          UnsafeByteBufUtil.setInt(memory, idx(index), value);
84      }
85  
86      @Override
87      protected void _setLong(int index, long value) {
88          UnsafeByteBufUtil.setLong(memory, idx(index), value);
89      }
90  
91      @Override
92      protected Recycler<?> recycler() {
93          return RECYCLER;
94      }
95  
96      @Override
97      public ByteBuf setZero(int index, int length) {
98          if (PlatformDependent.javaVersion() >= 7) {
99              checkIndex(index, length);
100             // Only do on java7+ as the needed Unsafe call was only added there.
101             UnsafeByteBufUtil.setZero(memory, idx(index), length);
102             return this;
103         }
104         return super.setZero(index, length);
105     }
106 
107     @Override
108     public ByteBuf writeZero(int length) {
109         if (PlatformDependent.javaVersion() >= 7) {
110             // Only do on java7+ as the needed Unsafe call was only added there.
111             ensureWritable(length);
112             int wIndex = writerIndex;
113             UnsafeByteBufUtil.setZero(memory, idx(wIndex), length);
114             writerIndex = wIndex + length;
115             return this;
116         }
117         return super.writeZero(length);
118     }
119 
120     @Override
121     @Deprecated
122     protected SwappedByteBuf newSwappedByteBuf() {
123         if (PlatformDependent.isUnaligned()) {
124             // Only use if unaligned access is supported otherwise there is no gain.
125             return new UnsafeHeapSwappedByteBuf(this);
126         }
127         return super.newSwappedByteBuf();
128     }
129 }