Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/io/ObjectOutputStream.c
38829 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni_util.h"26#include "jdk_util.h"2728#include "java_lang_Float.h"29#include "java_lang_Double.h"30#include "java_io_ObjectOutputStream.h"3132/*33* Class: java_io_ObjectOutputStream34* Method: floatsToBytes35* Signature: ([FI[BII)V36*37* Convert nfloats float values to their byte representations. Float values38* are read from array src starting at offset srcpos and written to array39* dst starting at offset dstpos.40*/41JNIEXPORT void JNICALL42Java_java_io_ObjectOutputStream_floatsToBytes(JNIEnv *env,43jclass this,44jfloatArray src,45jint srcpos,46jbyteArray dst,47jint dstpos,48jint nfloats)49{50union {51int i;52float f;53} u;54jfloat *floats;55jbyte *bytes;56jsize srcend;57jint ival;58float fval;5960if (nfloats == 0)61return;6263/* fetch source array */64if (src == NULL) {65JNU_ThrowNullPointerException(env, NULL);66return;67}68floats = (*env)->GetPrimitiveArrayCritical(env, src, NULL);69if (floats == NULL) /* exception thrown */70return;7172/* fetch dest array */73if (dst == NULL) {74(*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);75JNU_ThrowNullPointerException(env, NULL);76return;77}78bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);79if (bytes == NULL) { /* exception thrown */80(*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);81return;82}8384/* do conversion */85srcend = srcpos + nfloats;86for ( ; srcpos < srcend; srcpos++) {87fval = (float) floats[srcpos];88if (ISNANF(fval)) { /* collapse NaNs */89ival = 0x7fc00000;90} else {91u.f = fval;92ival = (jint) u.i;93}94bytes[dstpos++] = (ival >> 24) & 0xFF;95bytes[dstpos++] = (ival >> 16) & 0xFF;96bytes[dstpos++] = (ival >> 8) & 0xFF;97bytes[dstpos++] = (ival >> 0) & 0xFF;98}99100(*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);101(*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);102}103104/*105* Class: java_io_ObjectOutputStream106* Method: doublesToBytes107* Signature: ([DI[BII)V108*109* Convert ndoubles double values to their byte representations. Double110* values are read from array src starting at offset srcpos and written to111* array dst starting at offset dstpos.112*/113JNIEXPORT void JNICALL114Java_java_io_ObjectOutputStream_doublesToBytes(JNIEnv *env,115jclass this,116jdoubleArray src,117jint srcpos,118jbyteArray dst,119jint dstpos,120jint ndoubles)121{122union {123jlong l;124double d;125} u;126jdouble *doubles;127jbyte *bytes;128jsize srcend;129jdouble dval;130jlong lval;131132if (ndoubles == 0)133return;134135/* fetch source array */136if (src == NULL) {137JNU_ThrowNullPointerException(env, NULL);138return;139}140doubles = (*env)->GetPrimitiveArrayCritical(env, src, NULL);141if (doubles == NULL) /* exception thrown */142return;143144/* fetch dest array */145if (dst == NULL) {146(*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);147JNU_ThrowNullPointerException(env, NULL);148return;149}150bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);151if (bytes == NULL) { /* exception thrown */152(*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);153return;154}155156/* do conversion */157srcend = srcpos + ndoubles;158for ( ; srcpos < srcend; srcpos++) {159dval = doubles[srcpos];160if (ISNAND((double) dval)) { /* collapse NaNs */161lval = jint_to_jlong(0x7ff80000);162lval = jlong_shl(lval, 32);163} else {164jdouble_to_jlong_bits(&dval);165u.d = (double) dval;166lval = u.l;167}168bytes[dstpos++] = (lval >> 56) & 0xFF;169bytes[dstpos++] = (lval >> 48) & 0xFF;170bytes[dstpos++] = (lval >> 40) & 0xFF;171bytes[dstpos++] = (lval >> 32) & 0xFF;172bytes[dstpos++] = (lval >> 24) & 0xFF;173bytes[dstpos++] = (lval >> 16) & 0xFF;174bytes[dstpos++] = (lval >> 8) & 0xFF;175bytes[dstpos++] = (lval >> 0) & 0xFF;176}177178(*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);179(*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);180}181182183