1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package java.nio;
18 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 
21 import android.annotation.SystemApi;
22 
23 import android.compat.annotation.UnsupportedAppUsage;
24 
25 /**
26  * This class is used via JNI by code in frameworks/base/ and
27  * by the JniConstants cache in libnativehelper/.
28  * @hide
29  */
30 // @VisibleForTesting : was default
31 @SystemApi(client = MODULE_LIBRARIES)
32 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
33 public final class NIOAccess {
34 
NIOAccess()35     private NIOAccess() {}
36 
37     /**
38      * Returns the underlying native pointer to the data of the given
39      * Buffer starting at the Buffer's current position, or 0 if the
40      * Buffer is not backed by native heap storage.
41      * @hide
42      */
43     // @VisibleForTesting : was default
44     @UnsupportedAppUsage
getBasePointer(Buffer b)45     public static long getBasePointer(Buffer b) {
46         long address = b.address;
47         if (address == 0L) {
48             return 0L;
49         }
50         return address + (b.position << b._elementSizeShift);
51     }
52 
53     /**
54      * Returns the underlying Java array containing the data of the
55      * given Buffer, or null if the Buffer is not backed by a Java array.
56      *
57      * @param b  {@code Buffer} to get its underlying data array
58      * @return   underlying Java array
59      *
60      * @hide
61      */
62     @UnsupportedAppUsage
63     @SystemApi(client = MODULE_LIBRARIES)
64     @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
getBaseArray(Buffer b)65     public static Object getBaseArray(Buffer b) {
66         return b.hasArray() ? b.array() : null;
67     }
68 
69     /**
70      * Returns the offset in bytes from the start of the underlying
71      * Java array object containing the data of the given Buffer to
72      * the actual start of the data. The start of the data takes into
73      * account the Buffer's current position. This method is only
74      * meaningful if {@link #getBaseArray(Buffer)} returns non-null.
75      *
76      * @param b {@code Buffer} to get its underlying data array's base offset
77      * @return  underlying Java array's base offset
78      *
79      * @hide
80      */
81     @UnsupportedAppUsage
82     @SystemApi(client = MODULE_LIBRARIES)
83     @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
getBaseArrayOffset(Buffer b)84     public static int getBaseArrayOffset(Buffer b) {
85         return b.hasArray() ? ((b.arrayOffset() + b.position) << b._elementSizeShift) : 0;
86     }
87 }
88