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

(bf) ByteBuffer.as-X-Buffer() does not propagate offset

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta3
    • x86
    • windows_2000
    • Verified



      Name: bsC130419 Date: 06/21/2001


      java full version "1.4.0-beta-b65"

      As the following program shows,
        Create a ByteBuffer bb1,
        Create a slice of that buffer not starting at 0 with "ByteBuffer.slice()" bb2
        Create a ShortBuffer view of that with "ByteBuffer.asShortBuffer()" sb2
      This short buffer (sb2) has the correct length for bb2, but the mapping starts
      at the begining of bb1, not the begining of bb2 as it should.
      The output of the program is:

      byte [ 0 4 0 5 0 6 0 7 0 8 0 9 ]
      short [ 0 1 2 3 4 5 ]
      short [ 4 5 6 7 8 9 ]

      (note, the last two lines should have given the same result)
      -------------------------------------------------------------
      import java.nio.*;

      public class BufferTest
      {
      public static void printByteBuffer(ByteBuffer bb)
      {
      System.out.print("byte [");
      for (bb.position(0); bb.position() < bb.limit(); )
      System.out.print(" " + Integer.toHexString(bb.get() & 0xff));
      System.out.println(" ]");
      }

      public static void printShortBuffer(ShortBuffer sb)
      {
      System.out.print("short [");
      for (sb.position(0); sb.position() < sb.limit(); )
      System.out.print(" " + Integer.toHexString(sb.get() &0xffff));
      System.out.println(" ]");
      }

      public static void main(String argv[])
      {
      byte [] ba = new byte[20];
      ByteBuffer bb1 = ByteBuffer.wrap(ba);
      ShortBuffer sb1 = bb1.asShortBuffer();
      for (int i = 0; i < sb1.limit(); ++i)
      sb1.put(i, (short) i);
      bb1.position(8);
      ByteBuffer bb2 = bb1.slice();
      ShortBuffer sb2 = bb2.asShortBuffer();
      printByteBuffer(bb2);
      printShortBuffer(sb2);

      sb1.position(4);
      ShortBuffer sb3 = sb1.slice();
      printShortBuffer(sb3);
      }
      }
      (Review ID: 126790)
      ======================================================================

            iris Iris Clark
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: