jdk/src/java.base/share/native/libjava/MessageUtils.c
author lana
Thu, 07 Jan 2016 09:33:57 -0800
changeset 34907 d391c2d25bea
parent 25859 3317bb8137f4
permissions -rw-r--r--
Added tag jdk-9+100 for changeset 3359dd18cde0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22266
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3060
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3060
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3060
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3060
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3060
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <jni.h>
22275
de7efc0bb1a5 8031708: Windows x86 build failure: JNU_ThrowOutOfMemoryError undefined
chegar
parents: 22266
diff changeset
    28
#include <jni_util.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <jlong.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <jvm.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "sun_misc_MessageUtils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
printToFile(JNIEnv *env, jstring s, FILE *file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    char *sConverted;
22266
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    39
    int length = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    const jchar *sAsArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
3060
4477a1762fa5 6852607: MessageUtils JVM crash
langel
parents: 2
diff changeset
    43
    if (s == NULL) {
4477a1762fa5 6852607: MessageUtils JVM crash
langel
parents: 2
diff changeset
    44
      s = (*env)->NewStringUTF(env, "null");
4477a1762fa5 6852607: MessageUtils JVM crash
langel
parents: 2
diff changeset
    45
      if (s == NULL) return;
4477a1762fa5 6852607: MessageUtils JVM crash
langel
parents: 2
diff changeset
    46
    }
4477a1762fa5 6852607: MessageUtils JVM crash
langel
parents: 2
diff changeset
    47
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    sAsArray = (*env)->GetStringChars(env, s, NULL);
22266
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    49
    if (!sAsArray)
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    50
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    length = (*env)->GetStringLength(env, s);
22266
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    52
    if (length == 0) {
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    53
        (*env)->ReleaseStringChars(env, s, sAsArray);
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    54
        return;
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    55
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    sConverted = (char *) malloc(length + 1);
22266
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    57
    if (!sConverted) {
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    58
        (*env)->ReleaseStringChars(env, s, sAsArray);
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    59
        JNU_ThrowOutOfMemoryError(env, NULL);
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    60
        return;
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    61
    }
d70900404b67 8029007: Check src/share/native/sun/misc code for JNI pending exceptions
dxu
parents: 5506
diff changeset
    62
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    for(i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        sConverted[i] = (0x7f & sAsArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    sConverted[length] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    jio_fprintf(file, "%s", sConverted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    (*env)->ReleaseStringChars(env, s, sAsArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    free(sConverted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
Java_sun_misc_MessageUtils_toStderr(JNIEnv *env, jclass cls, jstring s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    printToFile(env, s, stderr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
Java_sun_misc_MessageUtils_toStdout(JNIEnv *env, jclass cls, jstring s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    printToFile(env, s, stdout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
}