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