History log of /libcore/luni/src/test/java/libcore/java/nio/BufferTest.java
Revision Date Author Comments
1a6436a8de0d3dd8e1ec0c6a3a54529468dc298d 02-Jun-2017 Przemyslaw Szczepaniak <pszczepaniak@google.com> Fix phantom reference based cleanup of JNI-created buffer

This change allows to safely use PhantomReference to detect
if the memory wrapped by buffer created by JNI NewDirectByteBuffer
is no longer referenced (and therfore safe to free/reuse/alter).

Previously it was not possible without reflection because the buffer
could be collected by GC while the MemoryRef that is actually wrapping
the memory could be still used in derived buffer (for example, result of
asReadOnlyBuffer()).

This is mittigated by adding a reference to orinal DirectByteBuffer in
the MemoryRef, forcing GC to keep the buffer alive as long as MemoryRef
is alive.

Test: BufferTest in CtsLibcoreTestCases
Bug: 62133955
Change-Id: I06601d8b6b6157114a448d3623c35fbb9d4633a2
dc5090e980361d9c0831c2a7ecd5341d50fc2b00 22-Feb-2017 Shubham Ajmera <shubhamajmera@google.com> Enable BufferTest#testAccess

The test was disabled because accessible flag was not supported by
OpenJDK buffers. The test pointed out new issues with DirectByteBuffers.

- No exception is expected with #duplicate and #asReadOnlyBuffer even
when isAccessible flag is set to false
- IllegalStateException should be thrown before ReadOnlyException.
- ByteBufferAs*Buffer which wrapped DirectByteBuffer should also
respect the isAccessible flag of the underlying buffer.
- There are two state - isFreed and isAccessible.
The implementation was only depending upon the isAccessible flag.
There is a subtle difference between the two flags, i.e.,
isAccessible can be set and unset, however, freed is non
reversible and can only be set by calling #free.

Bug: 35629232
Test: Run all the harmony and luni Buffer tests on vogar.
Change-Id: I265499ca06826b256ffa426814b6d7af73a378d0
3ed8687e73cf28185d50a644a24a209ed2fa7ff8 30-Nov-2016 Shubham Ajmera <shubhamajmera@google.com> Fix ordering issue in view ByteBuffer

View buffers use byte buffer object to perform all the read/write operations.
While creating one, instead of creating a duplicate of the original byte
buffer object, the view buffers were using it directly. As a result,
when a property on the byte buffer object is changed, all the read
and write operations on the view buffers were affected. To keep their properties
independent of each other, instead of using the byte buffer directly, view
buffers should make a copy of it, and then perform operations.

Test: cts-tradefed run cts -m CtsLibcoreTestCases
Bug: 32655865
(cherry-picked from commit c3005946d4806ca6a975fcfc63bdfd3e54b56b9a)
Change-Id: I531d2642acf606391bb44b7209a72365cb19fede
c3005946d4806ca6a975fcfc63bdfd3e54b56b9a 30-Nov-2016 Shubham Ajmera <shubhamajmera@google.com> Fix ordering issue in view ByteBuffer

View buffers use byte buffer object to perform all the read/write operations.
While creating one, instead of creating a duplicate of the origianl byte
buffer object, the view buffers were using it directly. As a result,
when a property on the byte buffer object is changed, all the read
and write operations on the view buffers were affected. To keep their properties
independent of each other, instead of using the byte buffer directly, view
buffers should make a copy of it, and then perform operations.

Test: cts-tradefed run cts -m CtsLibcoreTestCases
Bug: 32655865

Change-Id: I531d2642acf606391bb44b7209a72365cb19fede
7b54756f6964018c41f0e12a1282d503f38cc683 04-Nov-2016 Shubham Ajmera <shubhamajmera@google.com> Fix byte ordering in ByteBufferAsXBuffer

X: Short, Long, Double, Float, Int, Char

When a readOnlyBuffer is created from ByteBufferAsXBuffer, the order of
underlying bytebuffer is set to default(Big Endian). Therefore, when the
buffer queries the underlying bytebuffer without explicitly setting the
byteOrder, it returns result in the default order, i.e., BigEndian.

Test: cts-tradefed run cts -m CtsLibcoreTestCases \
libcore.java.nio.BufferTest#test_ByteBufferAsXBuffer_ByteOrder

Bug: 32655865

(cherry-picked from commit cdf4dbb3c1ee370333fdfa628def46a4250c292d)

Change-Id: Iaa6fc72feb7d11bfe9c59ed2c7e00a26d31117bd
cdf4dbb3c1ee370333fdfa628def46a4250c292d 04-Nov-2016 Shubham Ajmera <shubhamajmera@google.com> Fix byte ordering in ByteBufferAsXBuffer

X: Short, Long, Double, Float, Int, Char

When a readOnlyBuffer is created from ByteBufferAsXBuffer, the order of
underlying bytebuffer is set to default(Big Endian). Therefore, when the
buffer queries the underlying bytebuffer without explicitly setting the
byteOrder, it returns result in the default order, i.e., BigEndian.

Test: cts-tradefed run cts -m CtsLibcoreTestCases \
libcore.java.nio.BufferTest#test_ByteBufferAsXBuffer_ByteOrder

Bug: 32655865

Change-Id: Iaa6fc72feb7d11bfe9c59ed2c7e00a26d31117bd
e7bb4dadbf37b10c55b134df0734ebf2a9353e43 29-Jan-2016 Narayan Kamath <narayan@google.com> Restore tests for _elementSizeShift.

Change-Id: I1a27430e1d2bc78221acfc947070b8e25d89b9c7
7b67633543b3202edbd60add5161495540b9ac51 17-Dec-2015 Alex Klyubin <klyubin@google.com> Fix GetDirectBufferAddress for DirectByteBuffer.

ART's GetDirectBufferAddress(java.nio.Buffer) implementation returns
the value stored in the Buffer.address field.
java.nio.DirectByteBuffer was not initializing this field correctly
when slicing buffers from non-zero position.

This change fixes the issue and adds tests. This change also removes
the TODO inside DirectByteBuffer.address() because it is now confirmed
that this method is supposed to return exactly the same value as
GetDirectBufferAddress.

Bug: 26233076
Change-Id: I6e7509d443c8cd8ae50eaf1e4ea42d1c04423883
7c00153cbe952a4e7552cdd815c95a90f31ead9b 11-Dec-2015 Shubham Ajmera <shubhamajmera@google.com> Fix isAccessible and alignment test

The test was trying to modify the content of the array after setting the
isaccessible flag to true, however it wasn't handling isReadOnly flag at
the same time.

alignment test is now added in harmony-tests. This will enable the test
to run on the other types of DirectByteBuffers.

Bug: 26140694
Change-Id: Ie4b98a13d9249a3f802ce17f268f188cac22af74
cc060d48b3e7f013f00eebff5d079c59c1811207 07-Dec-2015 Przemyslaw Szczepaniak <pszczepaniak@google.com> Fix ByteBuffer#as*Buffer using parent limit

(Direct|Heap)ByteBuffer#as*Buffer results were affected
by the #limit(int) call on its parent - limit check
was done on each call to the get/put operation. This
is mitigated by creating a #duplicate of parent for
each as*Buffer result.

Bug: 26019694
Bug: 26019022
Bug: 26011790
Change-Id: If564ef9421ea0232b31aac048b3600e667c56c16
0f7f0c999e751ab883a247e1409dcc8730c87b9f 23-Jul-2015 Przemyslaw Szczepaniak <pszczepaniak@google.com> Fixes for libcore.java.nio.BufferTest

- Added missing MappedByteBuffer native methods
- Fixed RandomAccessFile#getChannel permissions
- Disabled ByteBuffer#set/isAccessible parts of
BufferTest
- Marked set/isAccessible for removal (it will
be reimplemented in the framework).

Change-Id: I7d1999b35600ff45dd448a8b3671d9373559c1aa
648446e24ad82c2c660e158be8f32faabf082420 05-May-2015 Piotr Jastrzebski <haaawk@google.com> Use OpenJdk implementation of java.nio.*

- Unsafe: add more supporting methods.
- NIOAccess / NIOUtils : Track OpenJdk implementation.
- Bits : Initialize byte-order inline.
- Buffer : Add elementSizeShift.
- ByteBuffer : Add stub implementations of setAccessible and
isAccessible for use by the frameworks.

Change-Id: I07da77a7b8b9a2edd0410118fb697e5d3aa46fde
a20d8a3e850694e80dfc1cf7bcd440a812a811d0 23-Jul-2014 Mathieu Chartier <mathieuc@google.com> Fix broken nio BufferTest and add alignment test.

Bug: 16486921
Bug: 16449607

(cherry picked from commit 04a5e523e9b13fbc02a62af55b102d093b5cd054)

Change-Id: Ia1049decde8f5960712ab78954077e78d07f47a0
04a5e523e9b13fbc02a62af55b102d093b5cd054 23-Jul-2014 Mathieu Chartier <mathieuc@google.com> Fix broken nio BufferTest and add alignment test.

Bug: 16486921
Bug: 16449607
Change-Id: Ib1871d0fa09bfc3fbf3c041f56ee74fd7a234a5a
e30eaf6894647ecf5e1ec3ec5ac8221e41b4d170 17-May-2014 Lajos Molnar <lajos@google.com> DirectByteBuffer: allow 0-size MappedByteBuffers starting at NULL

Bug: 14297827
Change-Id: I53ae796f9a59c70cc8250477ab51c57ea728e43b
923706a429d78650a2a5f05c169b59174be1ee53 09-May-2014 Lajos Molnar <lajos@google.com> DirectByteBuffer: add setAccessible()

Bug: 14297827
Change-Id: I726f16f9a15f894b99a93c84aca820f5c1b994e5
7e02765b7ada4e85f941f0a6b0fc60a8a76301f3 28-Apr-2014 Lajos Molnar <lajos@google.com> Better protection against accessing freed DirectByteBuffers

Expand preventing access of freed buffers, so duplicated ByteBuffers
also become inaccessible when the original buffer is freed.

Throw IllegalStateException in ByteBuffer.put(ByteBuffer) when either
source or destination buffer is inaccessible.

Bug: 11512044
Change-Id: Idd2f4ef1f3d66b4209441bf4014ccd4aad9d850b
77c1cbc9eb6fac1549b98c553ea2df2beee66422 10-Jan-2014 Calin Juravle <calin@google.com> Invalidate DirectByteBuffers when freed.

Calls to DirectByteBuffer#free() will invalidate the buffer:
- isValid() will return false;
- subsequent operations which touch the inner buffer will
throw IllegalStateException.

Note: NioUtils#freeDirectByteBuffer can also be used to
invalidate the buffer.

Bug: 11512044
Change-Id: I78e00dc12b0dbfbd1be6af28bef2413053fb15cb
2589e301d1fb85960045c9ef29682d7e73b1aee7 05-Nov-2013 Narayan Kamath <narayan@google.com> Make effectiveDirectAddress final.

Also add a unit test to make sure elementSizeShift
is set correctly.

Change-Id: Ia6c797001a768fa1c3136d475f2ac0e047b35fb0
c375a9cfd6ca0f557a4343a14f6c4115a1f20207 29-Mar-2013 Joel Dice <joel.dice@gmail.com> Use long instead of int for address in DirectByteBuffer constructor

This allows the code to be used on 64-bit platforms.

(cherry picked from commit 9ce6a2533b50ffcd24b8b10bae7f234042768819)

Change-Id: Icc5248d8ea276a451b32ecc91b47aeb977e8f21c
9ce6a2533b50ffcd24b8b10bae7f234042768819 29-Mar-2013 Joel Dice <joel.dice@gmail.com> Use long instead of int for address in DirectByteBuffer constructor

This allows the code to be used on 64-bit platforms.

Change-Id: I1c42d48696296f74135cd209b3042b8973779e40
fe5da19e0e366286cd4d95f7628fe9442b9062c8 27-Mar-2013 Elliott Hughes <enh@google.com> Simplify the nio Buffer implementations, and fix MappedByteBuffer.

* Fix the reported bug caused by delegation in the MappedByteBuffer
implementation.

* Collapse the three classes (ReadOnly*, ReadWrite*, and *) used
to implement most buffer types into one class each.

* Rename HeapByteBuffer to ByteArrayBuffer to match its companions.

* Remove BaseByteBuffer.

* Give the classes used to implement asXBuffer sane names (their
existing names were backwards).

Bug: https://code.google.com/p/android/issues/detail?id=53637
Change-Id: I2a7ff4f1d502564389615384324afadabccb04f9
385c6f4303341beb9b091b6d252811b7ca3b9f42 20-Dec-2012 Nick Kralevich <nnk@google.com> FileChannelImpl: Ignore ftruncate failures on non-regular files

Truncate only works on regular files. On other files, such as
UNIX character devices, truncate returns EINVAL, which
causes an IOException to be thrown.

With this change, we now have support for creating a MappedByteBuffer
on UNIX character devices such as /dev/zero, if the underlying
device node supports mmap().

Change-Id: I094371d821b187abe3da32edc411ff76a81b047e
86f5f3e0e2bd808e4c85b0563ae50f17b7d48309 30-Jul-2012 Elliott Hughes <enh@google.com> Repair the damage I did removing "useless" checkIndex calls from the asXBuffer adapter classes.

The checkIndex calls were unnecessary as far as throwing the right Exception
subclass goes, but they're necessary if you want the detail message to use
the units of the wrapping buffer rather than the wrapped buffer. And we want
that. An IndexOutOfBoundsException that says "offset=0, limit=1" is only
going to confuse developers.

(Fixes bug introduced by 26841c0bb838d54fea2e1a06390e413fab561e27.)

Bug: 6085292
Change-Id: I9a1a5991ad383b7712642e40bced9af91440bed2
26841c0bb838d54fea2e1a06390e413fab561e27 26-Jul-2012 Elliott Hughes <enh@google.com> Remove useless checkIndex calls from the asXBuffer adapter classes.

Also add tests that we throw the expected exceptions.

Bug: 6085292
Change-Id: Ibcc44bdb546cba5365cdf557847e068fea77e1ca
bdd17cde8cf675f5d5703aad4dfb565932fb4c50 08-Feb-2012 Elliott Hughes <enh@google.com> Fix an ICS regression with hasArray on a JNI-allocated DirectByteBuffer.

Bug: http://code.google.com/p/android/issues/detail?id=24327
Change-Id: Ib31b60fe01e3d0b5a1ed7125ddf047e09fd64056
20b0416aa49b46b586ecf3d0b33016217d2dca63 29-Apr-2011 Elliott Hughes <enh@google.com> Fix the byte order of non-ByteBuffer duplicate and slice results.

ByteBuffer.duplicate and ByteBuffer.slice always return big-endian buffers.
Any other kind of buffer returns a buffer with the same endian as itself.

We didn't have tests for the latter, and broke this in Honeycomb.

Bug: http://code.google.com/p/android/issues/detail?id=16434
Bug: 4361578

git cherry-pick 87c354d8d6faa77dee1820df2f0ad2666b9f0331

Change-Id: Idf8dc6f47ffdbfe82b83e9ce1186f1717ec0ef8f
42d75577588240e7d79fe52e635f0c1df070672e 06-May-2011 Elliott Hughes <enh@google.com> Add a test for a publicly-reported bug I already fixed.

Another casualty of the honeycomb asXBuffer byte order bug.

Bug: http://code.google.com/p/android/issues/detail?id=16598
Change-Id: I885d9f1651238a5d2aadf3fef7d119775f742fdf
a47f80079c7147f6edc83ae812435326bd82dd40 07-Apr-2011 Elliott Hughes <enh@google.com> Fix asReadOnlyBuffer to conserve the byte order (on everything except ByteBuffer).

And add tests to exercise another wart of non-uniformity on this heinous API.

Bug: http://code.google.com/p/android/issues/detail?id=15994
Bug: 4361578

git cherry-pick 54298028777ffe4d5ec02e7908b333400d4f23a6

Change-Id: Id95256efe98edc0b350351e416e67c8f01ac64b7
eebf863ca5c8a7497f92602fe9ea3d1282c8bded 29-Apr-2011 Elliott Hughes <enh@google.com> Fix the byte order of non-ByteBuffer duplicate and slice results.

ByteBuffer.duplicate and ByteBuffer.slice always return big-endian buffers.
Any other kind of buffer returns a buffer with the same endian as itself.

We didn't have tests for the latter, and broke this in Honeycomb.

Bug: http://code.google.com/p/android/issues/detail?id=16434

git cherry-pick 87c354d8d6faa77dee1820df2f0ad2666b9f0331

Change-Id: I344cccab9a1a5ac4a6d6a70bb09c7da5808929bc
0c0934430f2cdd830b54eacf518355057f3567f3 07-Apr-2011 Elliott Hughes <enh@google.com> Fix asReadOnlyBuffer to conserve the byte order (on everything except ByteBuffer).

And add tests to exercise another wart of non-uniformity on this heinous API.

Bug: http://code.google.com/p/android/issues/detail?id=15994

git cherry-pick 54298028777ffe4d5ec02e7908b333400d4f23a6

Change-Id: I3e1443b4f82b98d71c90b18631d0fbad3e5ae9f8
87c354d8d6faa77dee1820df2f0ad2666b9f0331 29-Apr-2011 Elliott Hughes <enh@google.com> Fix the byte order of non-ByteBuffer duplicate and slice results.

ByteBuffer.duplicate and ByteBuffer.slice always return big-endian buffers.
Any other kind of buffer returns a buffer with the same endian as itself.

We didn't have tests for the latter, and broke this in Honeycomb.

Bug: http://code.google.com/p/android/issues/detail?id=16434
Change-Id: I08e98c4817f6340a0c396de060431185240d3ca3
0568a63ba1086a78ffb4cff68dd2eac4f9908e13 26-Apr-2011 Elliott Hughes <enh@google.com> Fix ByteBuffer.put(ByteBuffer).

To do this tastefully required some cunning in the native code. The new
ScopedBytesRO/ScopedBytesRW classes let us paper over the differences
between byte[] and DirectByteBuffer, which in turn lets us paper over the
differences between the various kinds of ByteBuffer.

I've also rewritten Posix.read/Posix.readDirectBuffer and
Posix.write/Posix.writeDirectBuffer to use the same idea. I haven't rewritten
readv and writev, but can do and probably should.

Bug: http://code.google.com/p/android/issues/detail?id=16184
Change-Id: Ia30d2f2fe1b1716a8f068187df2218b407a55aae
54298028777ffe4d5ec02e7908b333400d4f23a6 07-Apr-2011 Elliott Hughes <enh@google.com> Fix asReadOnlyBuffer to conserve the byte order (on everything except ByteBuffer).

And add tests to exercise another wart of non-uniformity on this heinous API.

Bug: http://code.google.com/p/android/issues/detail?id=15994
Change-Id: I49d2488a68a626e798f9bb39b47e710445b23fab
38f919c84cc2714ae2564ea1a04e2ced9c5aad04 26-Jan-2011 Elliott Hughes <enh@google.com> Our DirectByteBuffer can implement array(), arrayOffset(), and hasArray().

An interesting side-effect of moving direct buffers onto the Java heap.
Sufficiently sophisticated code can now have the best of both worlds.

Bug: 3384431
Change-Id: I741fc457277704016bc773cc2aec22e89045f839
5a1e8cc38e1cc6c3f2fc66f039ca0fc5309a0dc3 16-Dec-2010 Elliott Hughes <enh@google.com> MappedByteBuffer get(byte[]) and get(byte[], int, int) weren't updating 'position'.

The majority of this change is work to extend our tests to cover
MappedByteBuffer too, plus new tests to check that 'position' is
always updated correctly by ByteBuffer's relative get/put methods.

Bug: 3291927
Change-Id: I58217de45ba65675730f30771fba592cb2af28ff
17b2f493468764dc1f237c02d94747d122dbd101 08-Oct-2010 Elliott Hughes <enh@google.com> Fix our byte order behavior for slice and wrap operations.

Our documentation was correct, but our implementation was not. Includes
extensive new tests.

Bug: 3066167
Change-Id: Ie7d6e6432f1d675f20af020cc662dab6eb181a50
8fbc397fc09158bee0bc0cb231c609c4c6e9fc15 18-Sep-2010 Elliott Hughes <enh@google.com> Optimize non-direct (heap) buffer bulk put.

I've also factored out the bounds checking, even though the JIT can't yet
inline it back. I'm unwilling to duplicate all that code again.

This patch also fixes bugs in the previous checkin relating to offsets
and positions. In real life (and, seemingly, in all our tests) these tend
to be 0, making it rare that anyone trips over these bugs. (The first nio
change I made was fixing just such a bug.) I've improved the test I added
last time so that it covers more of these cases.

Here are the "after" performance figures for heap buffer bulk copies:

CharBuffer_getCharArray 5797
CharBuffer_putCharArray 5818
DoubleBuffer_getDoubleArray 13053
DoubleBuffer_putDoubleArray 13026
FloatBuffer_getFloatArray 8216
FloatBuffer_putFloatArray 8283
IntBuffer_getIntArray 8253
IntBuffer_putIntArray 8371
LongBuffer_getLongArray 13053
LongBuffer_putLongArray 13020
ShortBuffer_getShortArray 5796
ShortBuffer_putShortArray 5864

This compares well with with direct buffer performance (just gets shown here):

CharBuffer_getCharArray 4412
DoubleBuffer_getDoubleArray 11594
FloatBuffer_getFloatArray 6787
IntBuffer_getIntArray 6791
LongBuffer_getLongArray 11672
ShortBuffer_getShortArray 4430

Bug: 2985452
Change-Id: I37307f9b304c87a87af5313790444dcdf6174ff1
6944bea4a129dc2d4be687c72f2a9f228ec532bc 17-Sep-2010 Elliott Hughes <enh@google.com> Make nio heap (non-direct) buffer bulk get orders of magnitude faster.

Bug: 2985452
Change-Id: I8bd210d1a623e434121f30eca6e2555e1aea11cd