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 public final class NIOAccess { 33 NIOAccess()34 private NIOAccess() {} 35 36 /** 37 * Returns the underlying native pointer to the data of the given 38 * Buffer starting at the Buffer's current position, or 0 if the 39 * Buffer is not backed by native heap storage. 40 * @hide 41 */ 42 // @VisibleForTesting : was default 43 @UnsupportedAppUsage getBasePointer(Buffer b)44 public static long getBasePointer(Buffer b) { 45 long address = b.address; 46 if (address == 0L) { 47 return 0L; 48 } 49 return address + (b.position << b._elementSizeShift); 50 } 51 52 /** 53 * Returns the underlying Java array containing the data of the 54 * given Buffer, or null if the Buffer is not backed by a Java array. 55 * 56 * @param b {@code Buffer} to get its underlying data array 57 * @return underlying Java array 58 * 59 * @hide 60 */ 61 @UnsupportedAppUsage 62 @SystemApi(client = MODULE_LIBRARIES) getBaseArray(Buffer b)63 public static Object getBaseArray(Buffer b) { 64 return b.hasArray() ? b.array() : null; 65 } 66 67 /** 68 * Returns the offset in bytes from the start of the underlying 69 * Java array object containing the data of the given Buffer to 70 * the actual start of the data. The start of the data takes into 71 * account the Buffer's current position. This method is only 72 * meaningful if {@link #getBaseArray(Buffer)} returns non-null. 73 * 74 * @param b {@code Buffer} to get its underlying data array's base offset 75 * @return underlying Java array's base offset 76 * 77 * @hide 78 */ 79 @UnsupportedAppUsage 80 @SystemApi(client = MODULE_LIBRARIES) getBaseArrayOffset(Buffer b)81 public static int getBaseArrayOffset(Buffer b) { 82 return b.hasArray() ? ((b.arrayOffset() + b.position) << b._elementSizeShift) : 0; 83 } 84 } 85