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