查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2   * Copyright 2014 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  
17  package io.netty.buffer;
18  
19  import io.netty.util.internal.PlatformDependent;
20  
21  /**
22   * Special {@link SwappedByteBuf} for {@link ByteBuf}s that are backed by a {@code memoryAddress}.
23   */
24  final class UnsafeDirectSwappedByteBuf extends AbstractUnsafeSwappedByteBuf {
25  
26      UnsafeDirectSwappedByteBuf(AbstractByteBuf buf) {
27          super(buf);
28      }
29  
30      private static long addr(AbstractByteBuf wrapped, int index) {
31          // We need to call wrapped.memoryAddress() everytime and NOT cache it as it may change if the buffer expand.
32          // See:
33          // - https://github.com/netty/netty/issues/2587
34          // - https://github.com/netty/netty/issues/2580
35          return wrapped.memoryAddress() + index;
36      }
37  
38      @Override
39      protected long _getLong(AbstractByteBuf wrapped, int index) {
40          return PlatformDependent.getLong(addr(wrapped, index));
41      }
42  
43      @Override
44      protected int _getInt(AbstractByteBuf wrapped, int index) {
45          return PlatformDependent.getInt(addr(wrapped, index));
46      }
47  
48      @Override
49      protected short _getShort(AbstractByteBuf wrapped, int index) {
50          return PlatformDependent.getShort(addr(wrapped, index));
51      }
52  
53      @Override
54      protected void _setShort(AbstractByteBuf wrapped, int index, short value) {
55          PlatformDependent.putShort(addr(wrapped, index), value);
56      }
57  
58      @Override
59      protected void _setInt(AbstractByteBuf wrapped, int index, int value) {
60          PlatformDependent.putInt(addr(wrapped, index), value);
61      }
62  
63      @Override
64      protected void _setLong(AbstractByteBuf wrapped, int index, long value) {
65          PlatformDependent.putLong(addr(wrapped, index), value);
66      }
67  }