Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8065570

(bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN

    Details

    • Subcomponent:
    • Resolved In Build:
      b74
    • CPU:
      x86_64
    • OS:
      windows_7
    • Verification:
      Verified

      Backports

        Description

        A DESCRIPTION OF THE REQUEST :
        The slice() method on bytebuffer is meant to retain existing state. However, it loses the byteOrder and the sliced bytebuffer always comes back as BIG_ENDIAN. Here's an example:

            public static void main(String[] args) {
                ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
                foo.order(LITTLE_ENDIAN);
                System.out.println(foo.getInt(0)); //returns 4
                ByteBuffer bar = foo.slice();
                System.out.println(bar.getInt(0)); //returns 67108864
            }

        JUSTIFICATION :
        The API is unclear, callers are expected to re-set the byte order on every slice. I believe this makes it almost a bug rather than a feature request.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        byteBuffer.slice() retains the byteOrder, like:

            public static void main(String[] args) {
                ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
                foo.order(LITTLE_ENDIAN);
                System.out.println(foo.getInt(0)); //returns 4
                ByteBuffer bar = foo.slice();
                System.out.println(bar.getInt(0)); //returns 4
            }

        ACTUAL -
        byteBuffer.slice() does not retain the byteOrder, like:

            public static void main(String[] args) {
                ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
                foo.order(LITTLE_ENDIAN);
                System.out.println(foo.getInt(0)); //returns 4
                ByteBuffer bar = foo.slice();
                System.out.println(bar.getInt(0)); //returns 67108864
            }

        ---------- BEGIN SOURCE ----------
            public static void main(String[] args) {
                ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
                foo.order(LITTLE_ENDIAN);
                System.out.println(foo.getInt(0)); //returns 4
                ByteBuffer bar = foo.slice();
                System.out.println(bar.getInt(0)); //returns 67108864 - should return 4.
            }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        The caller needs to re-set the byteorder manually, like:

            public static void main(String[] args) {
                ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
                foo.order(LITTLE_ENDIAN);
                System.out.println(foo.getInt(0)); //returns 4
                ByteBuffer bar = foo.slice();
                bar.order(foo.order()); //Re-set byteorder to foo
                System.out.println(bar.getInt(0)); //returns 4
            }

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  bpb Brian Burkhalter
                  Reporter:
                  webbuggrp Webbug Group
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  8 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: