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

(bf) View buffers on the same data contain independent position, limit, remaining

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 1.4.2
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description



      Name: rmT116609 Date: 08/21/2003


      A DESCRIPTION OF THE REQUEST :
      Basically this is a plea. I found 2 posts people asking the same thing in the forums with no answers. I posted to revive them and see if anybody had answers.
      http://forum.java.sun.com/thread.jsp?forum=4&thread=240316

      http://forum.java.sun.com/thread.jsp?forum=17&thread=155735&start=0&range=15#1962302

      I would expect you could use different views and the ByteBuff
      if you use the CharBuffer and IntBuffer view together though, they overwrite each other.


      Also, thinking as an Architect and the model/view/controller or document/model patterns, these views should change the model and they don't(as the javadoc DOES state that they don't).

      If I do CharBuffer.put(array), the position in ByteBuffer does not change. At the same time, I cannot convert the CharBuffer to an IntBuffer so I can go on. I also can' convert the CharBuffer to a ByteBuffer meaning I can't feed it to a socketChannel.

      IMPORTANT: I MUST put in code that knows the characters are 2 bytes(If java ever changed to 3 bytes per char, I would be screwed).

      This ugly code had to be added.
      ByteBuffer.position(2*number of characters added);

      I am desparately looking for another way to do it.

      I only come up with 2.
      1. Have asByteBuffer, asIntBuffer on all the other types of Views
      2. Move the position of the ByteBuffer whenever I put chars in the CharBuffer or IntBuffer(or any of the view buffers, after all, it is model/view but the view was not changing the model.


      JUSTIFICATION :
      Because otherwise people including me are putting hacks in their code to get around this such as
      byteBuffer.position(2*numOfChars);

      notice the 2 is there because there are 2 bytes per char

      OR this
      byteBuffer.position(4*numIntsPutInIntBufferView);


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      one of 2 things
      1. move the position in the backing buffer.
      2. create asByteBuffer, asCharBuffer, asIntBuffer, asShortBuffer, etc. in all the other View buffers especially the asByteBuffer so we can flip() and feed the changes to a socket.
      ACTUAL -
      described already.

      ---------- BEGIN SOURCE ----------
      execute this and watch the integer overwrite the char array that was added.
      Also, notice the comment of
      //24 because all chars are 2 bytes so 10*2 +4=24
      Have to determine byte sizes for ints, chars and things to work with this.



      ByteBuffer b = ByteBuffer.allocate(30);
      char[] inputArray = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
      char[] array = new char[] { 'a', 'b', 'c' };
      b.putInt(10);
      CharBuffer charBuf = b.asCharBuffer();


      log.info("1byteBuf pos="+b.position()+" lim="+b.limit()+" remain="+b.remaining());
      log.info("1charBuf pos="+charBuf.position()+" lim="+charBuf.limit()+" remain="+charBuf.remaining());

      charBuf.put(inputArray);

      IntBuffer intBuf = b.asIntBuffer();
      intBuf.put(98);

      log.info("2byteBuf pos="+b.position()+" lim="+b.limit()+" remain="+b.remaining());
      log.info("2charBuf pos="+charBuf.position()+" lim="+charBuf.limit()+" remain="+charBuf.remaining());

      ///////////////
      assertEquals("This is to discover 1.5 fixed something...\n"
      +"buffer pos now correct. should be 24, not 4, but it is 4 in 1.4.2", 4, b.position());
      //24 because all chars are 2 bytes so 10*2 +4=24. The 4 bytes are the integer
      b.position(24);
      ///////////////


      b.flip();
      String s = "";
      log.info("pos="+b.position()+" lim="+b.limit()+" remain="+b.remaining());

      for(int i = b.position(); i < b.limit()-1; i++) {
      char c = b.getChar(i);
      log.info("i="+i+" c='"+c+"'");
      s += c;
      }

      log.info("buffer='"+s+"'");
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      KNOW THE SIZE OF CHARS, INTS, SHORTS and hardcode those sizes in your code. When java changes the size of ints to 8 on those next super computers, the code will break(Notice we already went from 2 bytes to 4!!!!
      (Incident Review ID: 200063)
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            iris Iris Clark
            Reporter:
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: