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

GetStringUTFChars/NewStringUTF don't work in accordance with each other

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.1beta3
    • CPU:
      sparc
    • OS:
      solaris_2.5
    • Verification:
      Not verified

      Description



      Name: laC46010 Date: 01/23/97


      A Java string is passed to a native method, converted to UTF string with
      GetStringUTFChars() and returned back to Java using NewStringUTF()
      so that it should be equal to the original Java string.
      However the test below shows that it's not true in some cases.

      To run the test, set environment variables JH (Java home) and CC (C compiler)
      and run "jnirun" script.

      >jnirun
      Bytes in Array from String: 5a c0 80 59 c2 80 58 db af 57 df bf 56 e0 a0 80 55 e5 95 95 54 ef bf bf 53
      Invalid value for nstu00101fromString
      Chars of the First string:
       5a 0 59 80 58 6ef 57 7ff 56 800 55 5555 54 ffff 53
      Chars of the Second string:
       5a ffc0 ff80 59 ffc2 ff80 58 ffdb ffaf 57 ffdf ffbf 56 ffe0 ffa0 ff80 55 ffe5 ff95 ff95 54 ffef ffbf ffbf 53

      ::::::::::::::
      jnirun
      ::::::::::::::
      #!/usr/bin/csh

      #setenv JH /export/ld32/dest/jdk1.1beta3/solaris
      #setenv CC /export/ld4/set/dist/sparc-S2/SC4.2/bin/cc

      $JH/bin/javac -d . *[1-9].java
      $CC -KPIC -G -I$JH/include -I$JH/include/solaris -o libtest.so *.c
      setenv LD_LIBRARY_PATH ".:$LD_LIBRARY_PATH"
      setenv CLASSPATH .
      $JH/bin/java javasoft.sqe.tests.vm.nstu1.nstu1.nstu00101
      ::::::::::::::
      nstu00101.c
      ::::::::::::::
      /* File: %Z%%M% %I% %E%
         Copyright %G% Sun Microsystems, Inc. All Rights Reserved
      */

      #include <stdio.h>
      #include "native.h"

      #ifdef __cplusplus
      extern "C" {
      #endif

      #ifdef __cplusplus
      #define JNI_ENV_ARG(x,y) y
      #define JNI_ENV_PTR(x) x
      #else
      #define JNI_ENV_ARG(x,y) x,y
      #define JNI_ENV_PTR(x) (*x)
      #endif

      jbyte * carr;
      int alen, acount, acounts;

      JNIEXPORT jstring JNICALL
      Java_javasoft_sqe_tests_vm_nstu1_nstu1_nstu00101_nstu00101fromString
      (JNIEnv *env, jobject obj, jstring val, jsize len) {
      jboolean isCopy;
      int i;
      const jbyte* larr = JNI_ENV_PTR(env) -> GetStringUTFChars(JNI_ENV_ARG(env, val),&isCopy);
      printf("Bytes in Array from String:");
      for (i=0;i < 25;i++) { /* Achtung!!! 25 is a real length of UTF-8 string in bytes */
      printf(" %x",*(larr+i)&0xff);
      }
      printf("\n");
      return (JNI_ENV_PTR(env) -> NewStringUTF(JNI_ENV_ARG(env, larr),25));
      }

      #ifdef __cplusplus
      }
      #endif
      ::::::::::::::
      nstu00101.java
      ::::::::::::::
      //File: %Z%%M% %I% %E%
      //Copyright %G% Sun Microsystems, Inc. All Rights Reserved

      package javasoft.sqe.tests.vm.nstu1.nstu1;
      import java.io.PrintStream;


      public class nstu00101 {

        public static void main(String argv[])
        {
           System.exit(run() + 95/*STATUS_TEMP*/);
        }

        public native String nstu00101fromString(String val, int len);

        static {
          System.loadLibrary("test");
        }
        static void printStrHex (String ss) {
          int i;
          int val;
          for (i=0; i < ss.length(); i ++) {
            val = ss.charAt(i);
            System.out.print(" ");
            System.out.print(Integer.toHexString(val));
          }
          System.out.println(" ");
        }
        
        static String str = new String("Z\u0000Y\u0080X\u06efW\u07ffV\u0800U\u5555T\uFFFFS");
        
        public static int run() {
          nstu00101 tob = new nstu00101();
          String lstr = new String("");
          int i;
          int res = 0;
          try {
            lstr = tob.nstu00101fromString(str,str.length());
          } catch (Throwable e) {
            res = 2;
            System.out.println("Exception caught: " + e);
          }
          if (lstr == null) {
            res = 2;
            System.out.println("null value for nstu00101fromString");
          }
          if (!str.equals(lstr)) {
            res = 2;
            System.out.println("Invalid value for nstu00101fromString");
            System.out.println("Chars of the First string:");
            printStrHex (str);
            System.out.println("Chars of the Second string:");
            printStrHex (lstr);
          }
          return res;
        }

      }

      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            sliangsunw Sheng Liang (Inactive)
            Reporter:
            leosunw Leo Leo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: