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 /** 20 * This class is used via JNI by code in frameworks/base/. 21 */ 22 final class NIOAccess { 23 24 /** 25 * Returns the underlying native pointer to the data of the given 26 * Buffer starting at the Buffer's current position, or 0 if the 27 * Buffer is not backed by native heap storage. Note that this is 28 * different than what the Harmony implementation calls a "base 29 * address." 30 * 31 * @param Buffer b the Buffer to be queried 32 * @return the native pointer to the Buffer's data at its current 33 * position, or 0 if there is none 34 */ 35 static long getBasePointer(Buffer b) { 36 int address = b.effectiveDirectAddress; 37 if (address == 0) { 38 return 0L; 39 } 40 return address + (b.position << b._elementSizeShift); 41 } 42 43 /** 44 * Returns the underlying Java array containing the data of the 45 * given Buffer, or null if the Buffer is not backed by a Java array. 46 * 47 * @param Buffer b the Buffer to be queried 48 * @return the Java array containing the Buffer's data, or null if 49 * there is none 50 */ 51 static Object getBaseArray(Buffer b) { 52 return b.hasArray() ? b.array() : null; 53 } 54 55 /** 56 * Returns the offset in bytes from the start of the underlying 57 * Java array object containing the data of the given Buffer to 58 * the actual start of the data. This method is only meaningful if 59 * getBaseArray() returns non-null. 60 * 61 * @param Buffer b the Buffer to be queried 62 * @return the data offset in bytes to the start of this Buffer's data 63 */ 64 static int getBaseArrayOffset(Buffer b) { 65 return b.hasArray() ? (b.arrayOffset() << b._elementSizeShift) : 0; 66 } 67 } 68