JDK-8245121 : (bf) XBuffer.put(Xbuffer src) can give unexpected result when storage overlaps
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 15
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2020-05-15
  • Updated: 2020-06-06
  • Resolved: 2020-05-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15
15 b26Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
When the backing memory of two different buffers is the same then the result can be surprising if put(XBuffer) defaults to the ���loopy��� implementation

     while (src.hasRemaining())
         dst.put(src.get()); 

For example, the output of the following test class is:

0 1 2 3 0 1 2 3 0 1 2 3 12 13 14 15 

instead of, as might be expected:

0 1 2 3 0 1 2 3 4 5 6 7 12 13 14 15

It would be better if this operation behaved like array copy and have a result as if the content of the argument buffer were first copied to a separate location and then copied into the target.

import java.nio.*;

public class OverlapTest {
    public static void main(String[] args) throws Throwable {
        byte[] array = new byte[32];
        for (int i = 0; i < array.length; i++) {
            if (i % 2 == 1)
                array[i] = (byte)(i/2);;
        }
        ByteBuffer buf = ByteBuffer.wrap(array);

        ShortBuffer shortBuf = buf.asShortBuffer();
        int cap = shortBuf.capacity();
        for (int i = 0; i < cap; i++) {
            System.out.format("%d ", shortBuf.get(i));
        }

        System.out.println();

        ShortBuffer lower = shortBuf.slice(0, cap/2);
        ShortBuffer middle = shortBuf.slice(cap/4, cap/2);
        middle.put(lower);
        for (int i = 0; i < cap; i++) {
            System.out.format("%d ", shortBuf.get(i));
        }
        System.out.println();
    }
}
Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/99076dfecd1b User: bpb Date: 2020-05-29 21:28:32 +0000
29-05-2020