Revert the removal of java.nio.NIOAccess, which frameworks/base/ uses.
This was removed in https://android-git.corp.google.com/g/45796, but turns
out to be used. I think we can do better in the long term, but right now
this just removes some of the duplication from the original, and adds
comments explaining where it's used.
Bug: 2535509
Change-Id: Ie8e717537b8b8c70c8689f2dbb2c3a38501ecc57
diff --git a/libcore/nio/src/main/java/java/nio/Buffer.java b/libcore/nio/src/main/java/java/nio/Buffer.java
index f6006e7..02e3a14 100644
--- a/libcore/nio/src/main/java/java/nio/Buffer.java
+++ b/libcore/nio/src/main/java/java/nio/Buffer.java
@@ -77,6 +77,14 @@
// BEGIN android-added
/**
+ * The log base 2 of the element size of this buffer. Each typed subclass
+ * (ByteBuffer, CharBuffer, etc.) is responsible for initializing this
+ * value. The value is used by JNI code in frameworks/base/ to avoid the
+ * need for costly 'instanceof' tests.
+ */
+ int _elementSizeShift;
+
+ /**
* For direct buffers, the effective address of the data. This is set
* on first use. If the field is zero, this is either not a direct
* buffer or the field has not been initialized, and you need to issue
diff --git a/libcore/nio/src/main/java/java/nio/ByteBuffer.java b/libcore/nio/src/main/java/java/nio/ByteBuffer.java
index cda213c..71db2d3 100644
--- a/libcore/nio/src/main/java/java/nio/ByteBuffer.java
+++ b/libcore/nio/src/main/java/java/nio/ByteBuffer.java
@@ -126,6 +126,7 @@
*/
ByteBuffer(int capacity) {
super(capacity);
+ _elementSizeShift = 0;
}
/**
diff --git a/libcore/nio/src/main/java/java/nio/CharBuffer.java b/libcore/nio/src/main/java/java/nio/CharBuffer.java
index 6f48101..d743017 100644
--- a/libcore/nio/src/main/java/java/nio/CharBuffer.java
+++ b/libcore/nio/src/main/java/java/nio/CharBuffer.java
@@ -153,6 +153,7 @@
*/
CharBuffer(int capacity) {
super(capacity);
+ _elementSizeShift = 1;
}
public final char[] array() {
diff --git a/libcore/nio/src/main/java/java/nio/DoubleBuffer.java b/libcore/nio/src/main/java/java/nio/DoubleBuffer.java
index 4bac004..5aece85 100644
--- a/libcore/nio/src/main/java/java/nio/DoubleBuffer.java
+++ b/libcore/nio/src/main/java/java/nio/DoubleBuffer.java
@@ -103,6 +103,7 @@
*/
DoubleBuffer(int capacity) {
super(capacity);
+ _elementSizeShift = 3;
}
public final double[] array() {
diff --git a/libcore/nio/src/main/java/java/nio/FloatBuffer.java b/libcore/nio/src/main/java/java/nio/FloatBuffer.java
index eca4cb4..ee1606c 100644
--- a/libcore/nio/src/main/java/java/nio/FloatBuffer.java
+++ b/libcore/nio/src/main/java/java/nio/FloatBuffer.java
@@ -105,6 +105,7 @@
*/
FloatBuffer(int capacity) {
super(capacity);
+ _elementSizeShift = 2;
}
public final float[] array() {
diff --git a/libcore/nio/src/main/java/java/nio/IntBuffer.java b/libcore/nio/src/main/java/java/nio/IntBuffer.java
index 055e0ef..83e8364 100644
--- a/libcore/nio/src/main/java/java/nio/IntBuffer.java
+++ b/libcore/nio/src/main/java/java/nio/IntBuffer.java
@@ -102,6 +102,7 @@
*/
IntBuffer(int capacity) {
super(capacity);
+ _elementSizeShift = 2;
}
public final int[] array() {
diff --git a/libcore/nio/src/main/java/java/nio/LongBuffer.java b/libcore/nio/src/main/java/java/nio/LongBuffer.java
index 68bf9fe..e268b10 100644
--- a/libcore/nio/src/main/java/java/nio/LongBuffer.java
+++ b/libcore/nio/src/main/java/java/nio/LongBuffer.java
@@ -104,6 +104,7 @@
*/
LongBuffer(int capacity) {
super(capacity);
+ _elementSizeShift = 3;
}
public final long[] array() {
diff --git a/libcore/nio/src/main/java/java/nio/NIOAccess.java b/libcore/nio/src/main/java/java/nio/NIOAccess.java
new file mode 100644
index 0000000..431cb8b
--- /dev/null
+++ b/libcore/nio/src/main/java/java/nio/NIOAccess.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio;
+
+import org.apache.harmony.luni.platform.PlatformAddress;
+import org.apache.harmony.nio.internal.DirectBuffer;
+
+/**
+ * This class is used via JNI by code in frameworks/base/.
+ */
+class NIOAccess {
+
+ /**
+ * Returns the underlying native pointer to the data of the given
+ * Buffer starting at the Buffer's current position, or 0 if the
+ * Buffer is not backed by native heap storage. Note that this is
+ * different than what the Harmony implementation calls a "base
+ * address."
+ *
+ * @param Buffer b the Buffer to be queried
+ * @return the native pointer to the Buffer's data at its current
+ * position, or 0 if there is none
+ */
+ static long getBasePointer(Buffer b) {
+ if (b instanceof DirectBuffer) {
+ PlatformAddress address = ((DirectBuffer) b).getEffectiveAddress();
+ if (address == null) {
+ return 0L;
+ }
+ return address.toInt() + (b.position() << b._elementSizeShift);
+ }
+ return 0L;
+ }
+
+ /**
+ * Returns the number of bytes remaining in the given Buffer. That is,
+ * this scales <code>remaining()</code> by the byte-size of elements
+ * of this Buffer.
+ *
+ * @param Buffer b the Buffer to be queried
+ * @return the number of remaining bytes
+ */
+ static int getRemainingBytes(Buffer b) {
+ return (b.limit - b.position) << b._elementSizeShift;
+ }
+
+ /**
+ * Returns the underlying Java array containing the data of the
+ * given Buffer, or null if the Buffer is not backed by a Java array.
+ *
+ * @param Buffer b the Buffer to be queried
+ * @return the Java array containing the Buffer's data, or null if
+ * there is none
+ */
+ static Object getBaseArray(Buffer b) {
+ return b.hasArray() ? b.array() : null;
+ }
+
+ /**
+ * Returns the offset in bytes from the start of the underlying
+ * Java array object containing the data of the given Buffer to
+ * the actual start of the data. This method is only meaningful if
+ * getBaseArray() returns non-null.
+ *
+ * @param Buffer b the Buffer to be queried
+ * @return the data offset in bytes to the start of this Buffer's data
+ */
+ static int getBaseArrayOffset(Buffer b) {
+ return b.hasArray() ? (b.arrayOffset() << b._elementSizeShift) : 0;
+ }
+}
diff --git a/libcore/nio/src/main/java/java/nio/ShortBuffer.java b/libcore/nio/src/main/java/java/nio/ShortBuffer.java
index 01ff83a..596dad2 100644
--- a/libcore/nio/src/main/java/java/nio/ShortBuffer.java
+++ b/libcore/nio/src/main/java/java/nio/ShortBuffer.java
@@ -104,6 +104,7 @@
*/
ShortBuffer(int capacity) {
super(capacity);
+ _elementSizeShift = 1;
}
public final short[] array() {