src/share/native/sun/misc/MessageUtils.c

Print this page


   1 /*
   2  * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <jni.h>
  28 #include <jlong.h>
  29 #include <stdio.h>
  30 #include <jvm.h>
  31 
  32 #include "sun_misc_MessageUtils.h"
  33 
  34 static void
  35 printToFile(JNIEnv *env, jstring s, FILE *file)
  36 {
  37     char *sConverted;
  38     int length;
  39     int i;
  40     const jchar *sAsArray;
  41 
  42     if (s == NULL) {
  43       s = (*env)->NewStringUTF(env, "null");
  44       if (s == NULL) return;
  45     }
  46 
  47     sAsArray = (*env)->GetStringChars(env, s, NULL);


  48     length = (*env)->GetStringLength(env, s);




  49     sConverted = (char *) malloc(length + 1);





  50     for(i = 0; i < length; i++) {
  51         sConverted[i] = (0x7f & sAsArray[i]);
  52     }
  53     sConverted[length] = '\0';
  54     jio_fprintf(file, "%s", sConverted);
  55     (*env)->ReleaseStringChars(env, s, sAsArray);
  56     free(sConverted);
  57 }
  58 
  59 JNIEXPORT void JNICALL
  60 Java_sun_misc_MessageUtils_toStderr(JNIEnv *env, jclass cls, jstring s)
  61 {
  62     printToFile(env, s, stderr);
  63 }
  64 
  65 JNIEXPORT void JNICALL
  66 Java_sun_misc_MessageUtils_toStdout(JNIEnv *env, jclass cls, jstring s)
  67 {
  68     printToFile(env, s, stdout);
  69 }
   1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <jni.h>
  28 #include <jlong.h>
  29 #include <stdio.h>
  30 #include <jvm.h>
  31 
  32 #include "sun_misc_MessageUtils.h"
  33 
  34 static void
  35 printToFile(JNIEnv *env, jstring s, FILE *file)
  36 {
  37     char *sConverted;
  38     int length = 0;
  39     int i;
  40     const jchar *sAsArray;
  41 
  42     if (s == NULL) {
  43       s = (*env)->NewStringUTF(env, "null");
  44       if (s == NULL) return;
  45     }
  46 
  47     sAsArray = (*env)->GetStringChars(env, s, NULL);
  48     if (!sAsArray)
  49         return;
  50     length = (*env)->GetStringLength(env, s);
  51     if (length <= 0) {
  52         (*env)->ReleaseStringChars(env, s, sAsArray);
  53         return;
  54     }
  55     sConverted = (char *) malloc(length + 1);
  56     if (!sConverted) {
  57         (*env)->ReleaseStringChars(env, s, sAsArray);
  58         return;
  59     }
  60 
  61     for(i = 0; i < length; i++) {
  62         sConverted[i] = (0x7f & sAsArray[i]);
  63     }
  64     sConverted[length] = '\0';
  65     jio_fprintf(file, "%s", sConverted);
  66     (*env)->ReleaseStringChars(env, s, sAsArray);
  67     free(sConverted);
  68 }
  69 
  70 JNIEXPORT void JNICALL
  71 Java_sun_misc_MessageUtils_toStderr(JNIEnv *env, jclass cls, jstring s)
  72 {
  73     printToFile(env, s, stderr);
  74 }
  75 
  76 JNIEXPORT void JNICALL
  77 Java_sun_misc_MessageUtils_toStdout(JNIEnv *env, jclass cls, jstring s)
  78 {
  79     printToFile(env, s, stdout);
  80 }