View Javadoc
1   /*
2    * Copyright 2018 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.channel.unix;
17  
18  import io.netty.util.internal.CleanableDirectBuffer;
19  import io.netty.util.internal.PlatformDependent;
20  import io.netty.util.internal.UnstableApi;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.ByteOrder;
24  
25  @UnstableApi
26  public final class Buffer {
27  
28      private Buffer() { }
29  
30      /**
31       * Free the direct {@link ByteBuffer}.
32       * @deprecated Use {@link #allocateDirectBufferWithNativeOrder(int)} instead.
33       */
34      @Deprecated
35      public static void free(ByteBuffer buffer) {
36          PlatformDependent.freeDirectBuffer(buffer);
37      }
38  
39      /**
40       * Returns a new {@link ByteBuffer} which has the same {@link ByteOrder} as the native order of the machine.
41       * @deprecated Use {@link #allocateDirectBufferWithNativeOrder(int)} instead.
42       */
43      @Deprecated
44      public static ByteBuffer allocateDirectWithNativeOrder(int capacity) {
45          return ByteBuffer.allocateDirect(capacity).order(
46                  PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
47      }
48  
49      /**
50       * Returns a new {@link CleanableDirectBuffer} which has the same {@link ByteOrder} as the native order of the
51       * machine.
52       */
53      public static CleanableDirectBuffer allocateDirectBufferWithNativeOrder(int capacity) {
54          CleanableDirectBuffer cleanableDirectBuffer = PlatformDependent.allocateDirect(capacity);
55          cleanableDirectBuffer.buffer().order(
56                  PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
57          return cleanableDirectBuffer;
58      }
59  
60      /**
61       * Returns the memory address of the given direct {@link ByteBuffer}.
62       */
63      public static long memoryAddress(ByteBuffer buffer) {
64          assert buffer.isDirect();
65          if (PlatformDependent.hasUnsafe()) {
66              return PlatformDependent.directBufferAddress(buffer);
67          }
68          return memoryAddress0(buffer);
69      }
70  
71      /**
72       * Returns the size of a pointer.
73       */
74      public static int addressSize() {
75          if (PlatformDependent.hasUnsafe()) {
76              return PlatformDependent.addressSize();
77          }
78          return addressSize0();
79      }
80  
81      // If Unsafe can not be used we will need to do JNI calls.
82      private static native int addressSize0();
83      private static native long memoryAddress0(ByteBuffer buffer);
84  
85      public static ByteBuffer wrapMemoryAddressWithNativeOrder(long memoryAddress, int capacity) {
86          return wrapMemoryAddress(memoryAddress, capacity).order(ByteOrder.nativeOrder());
87      }
88  
89      public static native ByteBuffer wrapMemoryAddress(long memoryAddress, int capacity);
90  }