src/java.base/share/native/libjava/ObjectOutputStream.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/native/libjava/ObjectOutputStream.c@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 "jni_util.h"
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 5506
diff changeset
    27
#include "jdk_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "java_lang_Float.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "java_lang_Double.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "java_io_ObjectOutputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Class:     java_io_ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Method:    floatsToBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Signature: ([FI[BII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Convert nfloats float values to their byte representations.  Float values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * are read from array src starting at offset srcpos and written to array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * dst starting at offset dstpos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
Java_java_io_ObjectOutputStream_floatsToBytes(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                              jclass this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                                              jfloatArray src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                              jint srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                              jbyteArray dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                              jint dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                              jint nfloats)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        float f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    } u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    jfloat *floats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    jbyte *bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    jsize srcend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    jint ival;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    float fval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    if (nfloats == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* fetch source array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (src == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    floats = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (floats == NULL)         /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* fetch dest array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    if (dst == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (bytes == NULL) {        /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* do conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    srcend = srcpos + nfloats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    for ( ; srcpos < srcend; srcpos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        fval = (float) floats[srcpos];
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 5506
diff changeset
    89
        if (ISNANF(fval)) {          /* collapse NaNs */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            ival = 0x7fc00000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            u.f = fval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            ival = (jint) u.i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        bytes[dstpos++] = (ival >> 24) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        bytes[dstpos++] = (ival >> 16) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        bytes[dstpos++] = (ival >> 8) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        bytes[dstpos++] = (ival >> 0) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Class:     java_io_ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Method:    doublesToBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Signature: ([DI[BII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Convert ndoubles double values to their byte representations.  Double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * values are read from array src starting at offset srcpos and written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * array dst starting at offset dstpos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
Java_java_io_ObjectOutputStream_doublesToBytes(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                               jclass this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                               jdoubleArray src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                               jint srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                               jbyteArray dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                               jint dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                               jint ndoubles)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        jlong l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        double d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    } u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    jdouble *doubles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    jbyte *bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    jsize srcend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    jdouble dval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    jlong lval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    if (ndoubles == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /* fetch source array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    if (src == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    doubles = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    if (doubles == NULL)                /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /* fetch dest array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    if (dst == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    if (bytes == NULL) {        /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /* do conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    srcend = srcpos + ndoubles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    for ( ; srcpos < srcend; srcpos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        dval = doubles[srcpos];
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 5506
diff changeset
   161
        if (ISNAND((double) dval)) {         /* collapse NaNs */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            lval = jint_to_jlong(0x7ff80000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            lval = jlong_shl(lval, 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            jdouble_to_jlong_bits(&dval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            u.d = (double) dval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            lval = u.l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        bytes[dstpos++] = (lval >> 56) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        bytes[dstpos++] = (lval >> 48) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        bytes[dstpos++] = (lval >> 40) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        bytes[dstpos++] = (lval >> 32) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        bytes[dstpos++] = (lval >> 24) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        bytes[dstpos++] = (lval >> 16) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        bytes[dstpos++] = (lval >> 8) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        bytes[dstpos++] = (lval >> 0) & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}