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
17package java.nio;
18
19/**
20 * This class is used via JNI by code in frameworks/base/.
21 * @hide
22 */
23// @VisibleForTesting : was default
24public final class NIOAccess {
25
26    /**
27     * Returns the underlying native pointer to the data of the given
28     * Buffer starting at the Buffer's current position, or 0 if the
29     * Buffer is not backed by native heap storage.
30     * @hide
31     */
32    // @VisibleForTesting : was default
33    public static long getBasePointer(Buffer b) {
34        long address = b.address;
35        if (address == 0L) {
36            return 0L;
37        }
38        return address + (b.position << b._elementSizeShift);
39    }
40
41    /**
42     * Returns the underlying Java array containing the data of the
43     * given Buffer, or null if the Buffer is not backed by a Java array.
44     */
45    static Object getBaseArray(Buffer b) {
46        return b.hasArray() ? b.array() : null;
47    }
48
49    /**
50     * Returns the offset in bytes from the start of the underlying
51     * Java array object containing the data of the given Buffer to
52     * the actual start of the data. The start of the data takes into
53     * account the Buffer's current position. This method is only
54     * meaningful if getBaseArray() returns non-null.
55     */
56    static int getBaseArrayOffset(Buffer b) {
57        return b.hasArray() ? ((b.arrayOffset() + b.position) << b._elementSizeShift) : 0;
58    }
59}
60