summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--luni/src/main/java/java/nio/DirectByteBuffer.java20
-rw-r--r--luni/src/main/java/java/nio/MemoryBlock.java9
-rw-r--r--luni/src/test/java/libcore/java/nio/BufferTest.java35
3 files changed, 55 insertions, 9 deletions
diff --git a/luni/src/main/java/java/nio/DirectByteBuffer.java b/luni/src/main/java/java/nio/DirectByteBuffer.java
index d9e8afd..f79be43 100644
--- a/luni/src/main/java/java/nio/DirectByteBuffer.java
+++ b/luni/src/main/java/java/nio/DirectByteBuffer.java
@@ -205,18 +205,20 @@ abstract class DirectByteBuffer extends BaseByteBuffer {
block.free();
}
- @Override
- final protected byte[] protectedArray() {
- throw new UnsupportedOperationException();
+ @Override final protected byte[] protectedArray() {
+ byte[] array = this.block.array();
+ if (array == null) {
+ throw new UnsupportedOperationException();
+ }
+ return array;
}
- @Override
- final protected int protectedArrayOffset() {
- throw new UnsupportedOperationException();
+ @Override final protected int protectedArrayOffset() {
+ protectedArray(); // Check we have an array.
+ return offset;
}
- @Override
- final protected boolean protectedHasArray() {
- return false;
+ @Override final protected boolean protectedHasArray() {
+ return protectedArray() != null;
}
}
diff --git a/luni/src/main/java/java/nio/MemoryBlock.java b/luni/src/main/java/java/nio/MemoryBlock.java
index 76a7b36..a19e5a0 100644
--- a/luni/src/main/java/java/nio/MemoryBlock.java
+++ b/luni/src/main/java/java/nio/MemoryBlock.java
@@ -58,6 +58,10 @@ class MemoryBlock {
this.array = array;
}
+ @Override public byte[] array() {
+ return array;
+ }
+
@Override public void free() {
array = null;
address = 0;
@@ -103,6 +107,11 @@ class MemoryBlock {
this.size = size;
}
+ // Used to support array/arrayOffset/hasArray for direct buffers.
+ public byte[] array() {
+ return null;
+ }
+
public void free() {
}
diff --git a/luni/src/test/java/libcore/java/nio/BufferTest.java b/luni/src/test/java/libcore/java/nio/BufferTest.java
index ec85235..fd9c0ed 100644
--- a/luni/src/test/java/libcore/java/nio/BufferTest.java
+++ b/luni/src/test/java/libcore/java/nio/BufferTest.java
@@ -416,4 +416,39 @@ public class BufferTest extends TestCase {
b.putShort((short) 0);
assertEquals(2, b.position());
}
+
+ // This test will fail on the RI. Our direct buffers are cooler than theirs.
+ // http://b/3384431
+ public void testDirectByteBufferHasArray() throws Exception {
+ ByteBuffer b = ByteBuffer.allocateDirect(10);
+ assertTrue(b.isDirect());
+ // Check the buffer has an array of the right size.
+ assertTrue(b.hasArray());
+ assertEquals(0, b.arrayOffset());
+ byte[] array = b.array();
+ assertEquals(10, array.length);
+ // Check that writes to the array show up in the buffer.
+ assertEquals(0, b.get(0));
+ array[0] = 1;
+ assertEquals(1, b.get(0));
+ // Check that writes to the buffer show up in the array.
+ assertEquals(1, array[0]);
+ b.put(0, (byte) 0);
+ assertEquals(0, array[0]);
+ }
+
+ public void testSliceOffset() throws Exception {
+ // Slicing changes the array offset.
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ buffer.get();
+ ByteBuffer slice = buffer.slice();
+ assertEquals(0, buffer.arrayOffset());
+ assertEquals(1, slice.arrayOffset());
+
+ ByteBuffer directBuffer = ByteBuffer.allocateDirect(10);
+ directBuffer.get();
+ ByteBuffer directSlice = directBuffer.slice();
+ assertEquals(0, directBuffer.arrayOffset());
+ assertEquals(1, directSlice.arrayOffset());
+ }
}