src/java.base/share/native/libjava/ObjectInputStream.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/native/libjava/ObjectInputStream.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
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10797
diff changeset
     2
 * Copyright (c) 1996, 2011, 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.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "java_lang_Float.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "java_lang_Double.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "java_io_ObjectInputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Class:     java_io_ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Method:    bytesToFloats
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Signature: ([BI[FII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Reconstitutes nfloats float values from their byte representations.  Byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * values are read from array src starting at offset srcpos; the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * float values are written to array dst starting at dstpos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
Java_java_io_ObjectInputStream_bytesToFloats(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                             jclass this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                             jbyteArray src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                             jint srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                             jfloatArray dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                             jint dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                             jint nfloats)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        float f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    } u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    jfloat *floats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    jbyte *bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    jsize dstend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    jint ival;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    if (nfloats == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* fetch source array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    if (src == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    bytes = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    if (bytes == NULL)          /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /* fetch dest array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    if (dst == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        (*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    floats = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (floats == NULL) {       /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        (*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* do conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    dstend = dstpos + nfloats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    for ( ; dstpos < dstend; dstpos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        ival = ((bytes[srcpos + 0] & 0xFF) << 24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
               ((bytes[srcpos + 1] & 0xFF) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
               ((bytes[srcpos + 2] & 0xFF) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
               ((bytes[srcpos + 3] & 0xFF) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        u.i = (long) ival;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        floats[dstpos] = (jfloat) u.f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        srcpos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    (*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    (*env)->ReleasePrimitiveArrayCritical(env, dst, floats, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Class:     java_io_ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Method:    bytesToDoubles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Signature: ([BI[DII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Reconstitutes ndoubles double values from their byte representations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Byte values are read from array src starting at offset srcpos; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * resulting double values are written to array dst starting at dstpos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
Java_java_io_ObjectInputStream_bytesToDoubles(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                              jclass this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                              jbyteArray src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                              jint srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                              jdoubleArray dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                              jint dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                              jint ndoubles)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    union {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        jlong l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        double d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    } u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    jdouble *doubles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    jbyte *bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    jsize dstend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    jlong lval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (ndoubles == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /* fetch source array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    if (src == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    bytes = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    if (bytes == NULL)          /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /* fetch dest array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if (dst == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        (*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        JNU_ThrowNullPointerException(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    doubles = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if (doubles == NULL) {      /* exception thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        (*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /* do conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    dstend = dstpos + ndoubles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    for ( ; dstpos < dstend; dstpos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        lval = (((jlong) bytes[srcpos + 0] & 0xFF) << 56) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
               (((jlong) bytes[srcpos + 1] & 0xFF) << 48) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
               (((jlong) bytes[srcpos + 2] & 0xFF) << 40) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
               (((jlong) bytes[srcpos + 3] & 0xFF) << 32) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
               (((jlong) bytes[srcpos + 4] & 0xFF) << 24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
               (((jlong) bytes[srcpos + 5] & 0xFF) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
               (((jlong) bytes[srcpos + 6] & 0xFF) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
               (((jlong) bytes[srcpos + 7] & 0xFF) << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        jlong_to_jdouble_bits(&lval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        u.l = lval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        doubles[dstpos] = (jdouble) u.d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        srcpos += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    (*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    (*env)->ReleasePrimitiveArrayCritical(env, dst, doubles, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175