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

ByteBuffer.get(byte[]...) fails to get the buffer content "intermittently"

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P1
    • 9
    • 9
    • hotspot
    • None
    • 9

    Description

      During the investigation of JDK-8080803, it is noticed that the possible trigger might be the ByteBuffer.get(byte[]) fails to get the corresponding bytes from the bytebuffer, the returned byte[] does not contain the any content ("zero" in our test case).

      I'm not sure if they are related, the attached test case always fails (ByteBuffer.get(byte[]) fails to get the content) after 10k+ iteration on my local 32-bit linux build (very old machine/os). Recently I have to update the hotspot source code (2 64-bit constants) to let the hotspot build to finish, as the compiler complains the constant is out of the range of a "long" ...

      http://cr.openjdk.java.net/~sherman/iso2022/hotspot

      ----------------------------------------

      import java.nio.*;
      import java.nio.charset.*;

      public class Foo {

          public static void main(String[] args) throws Throwable {

              int N = Integer.parseInt(args[0]);
              CharsetEncoder enc = Charset.forName("EUC_KR").newEncoder();
              byte[] bytes = new byte[8];
              for (int i = 0; i < N; i++) {
                  if (encode(enc, '\u4e00', bytes) == 2) {
                      if (bytes[0] == 0 && bytes[1] == 0) {
                          throw new RuntimeException("FAILED: b[0]=" + bytes[0] + ",b[1]=" + bytes[1]);
                      } else {
                          System.out.printf("[%d] 0x%x%x%n", i, bytes[0] & 0xff, bytes[1]&0xff);
                      }
                  }
              }
          }

          private static int encode(CharsetEncoder enc, char unicode, byte ebyte[]) {
              int index = 0;
              char convChar[] = {unicode};
              byte convByte[] = new byte[4];
              int converted;

              try{
                  CharBuffer cc = CharBuffer.wrap(convChar);
                  ByteBuffer bb = ByteBuffer.allocate(4);
                  enc.encode(cc, bb, true);
                  bb.flip();
                  converted = bb.remaining();
                  bb.get(convByte,0,converted);
              } catch(Exception e) {
                  return -1;
              }
              if (converted == 2) {
                  ebyte[index++] = (byte)(convByte[0] & 0x7f);
                  ebyte[index++] = (byte)(convByte[1] & 0x7f);
              }
              return converted;
          }
      }

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              sherman Xueming Shen
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: