jdk/src/java.base/share/native/libjava/MessageUtils.c
changeset 35669 9ff296647fde
parent 35668 21c2a1c93810
parent 34949 304424bbae03
child 35670 320e424b168c
equal deleted inserted replaced
35668:21c2a1c93810 35669:9ff296647fde
     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 <jni_util.h>
       
    29 #include <jlong.h>
       
    30 #include <stdio.h>
       
    31 #include <jvm.h>
       
    32 
       
    33 #include "sun_misc_MessageUtils.h"
       
    34 
       
    35 static void
       
    36 printToFile(JNIEnv *env, jstring s, FILE *file)
       
    37 {
       
    38     char *sConverted;
       
    39     int length = 0;
       
    40     int i;
       
    41     const jchar *sAsArray;
       
    42 
       
    43     if (s == NULL) {
       
    44       s = (*env)->NewStringUTF(env, "null");
       
    45       if (s == NULL) return;
       
    46     }
       
    47 
       
    48     sAsArray = (*env)->GetStringChars(env, s, NULL);
       
    49     if (!sAsArray)
       
    50         return;
       
    51     length = (*env)->GetStringLength(env, s);
       
    52     if (length == 0) {
       
    53         (*env)->ReleaseStringChars(env, s, sAsArray);
       
    54         return;
       
    55     }
       
    56     sConverted = (char *) malloc(length + 1);
       
    57     if (!sConverted) {
       
    58         (*env)->ReleaseStringChars(env, s, sAsArray);
       
    59         JNU_ThrowOutOfMemoryError(env, NULL);
       
    60         return;
       
    61     }
       
    62 
       
    63     for(i = 0; i < length; i++) {
       
    64         sConverted[i] = (0x7f & sAsArray[i]);
       
    65     }
       
    66     sConverted[length] = '\0';
       
    67     jio_fprintf(file, "%s", sConverted);
       
    68     (*env)->ReleaseStringChars(env, s, sAsArray);
       
    69     free(sConverted);
       
    70 }
       
    71 
       
    72 JNIEXPORT void JNICALL
       
    73 Java_sun_misc_MessageUtils_toStderr(JNIEnv *env, jclass cls, jstring s)
       
    74 {
       
    75     printToFile(env, s, stderr);
       
    76 }
       
    77 
       
    78 JNIEXPORT void JNICALL
       
    79 Java_sun_misc_MessageUtils_toStdout(JNIEnv *env, jclass cls, jstring s)
       
    80 {
       
    81     printToFile(env, s, stdout);
       
    82 }