Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/io/ObjectInputStream.c
38829 views
/*1* Copyright (c) 1996, 2011, 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.h"26#include "jvm.h"27#include "jni_util.h"28#include "jlong.h"2930#include "java_lang_Float.h"31#include "java_lang_Double.h"32#include "java_io_ObjectInputStream.h"333435/*36* Class: java_io_ObjectInputStream37* Method: bytesToFloats38* Signature: ([BI[FII)V39*40* Reconstitutes nfloats float values from their byte representations. Byte41* values are read from array src starting at offset srcpos; the resulting42* float values are written to array dst starting at dstpos.43*/44JNIEXPORT void JNICALL45Java_java_io_ObjectInputStream_bytesToFloats(JNIEnv *env,46jclass this,47jbyteArray src,48jint srcpos,49jfloatArray dst,50jint dstpos,51jint nfloats)52{53union {54int i;55float f;56} u;57jfloat *floats;58jbyte *bytes;59jsize dstend;60jint ival;6162if (nfloats == 0)63return;6465/* fetch source array */66if (src == NULL) {67JNU_ThrowNullPointerException(env, NULL);68return;69}70bytes = (*env)->GetPrimitiveArrayCritical(env, src, NULL);71if (bytes == NULL) /* exception thrown */72return;7374/* fetch dest array */75if (dst == NULL) {76(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);77JNU_ThrowNullPointerException(env, NULL);78return;79}80floats = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);81if (floats == NULL) { /* exception thrown */82(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);83return;84}8586/* do conversion */87dstend = dstpos + nfloats;88for ( ; dstpos < dstend; dstpos++) {89ival = ((bytes[srcpos + 0] & 0xFF) << 24) +90((bytes[srcpos + 1] & 0xFF) << 16) +91((bytes[srcpos + 2] & 0xFF) << 8) +92((bytes[srcpos + 3] & 0xFF) << 0);93u.i = (long) ival;94floats[dstpos] = (jfloat) u.f;95srcpos += 4;96}9798(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);99(*env)->ReleasePrimitiveArrayCritical(env, dst, floats, 0);100}101102/*103* Class: java_io_ObjectInputStream104* Method: bytesToDoubles105* Signature: ([BI[DII)V106*107* Reconstitutes ndoubles double values from their byte representations.108* Byte values are read from array src starting at offset srcpos; the109* resulting double values are written to array dst starting at dstpos.110*/111JNIEXPORT void JNICALL112Java_java_io_ObjectInputStream_bytesToDoubles(JNIEnv *env,113jclass this,114jbyteArray src,115jint srcpos,116jdoubleArray dst,117jint dstpos,118jint ndoubles)119120{121union {122jlong l;123double d;124} u;125jdouble *doubles;126jbyte *bytes;127jsize dstend;128jlong lval;129130if (ndoubles == 0)131return;132133/* fetch source array */134if (src == NULL) {135JNU_ThrowNullPointerException(env, NULL);136return;137}138bytes = (*env)->GetPrimitiveArrayCritical(env, src, NULL);139if (bytes == NULL) /* exception thrown */140return;141142/* fetch dest array */143if (dst == NULL) {144(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);145JNU_ThrowNullPointerException(env, NULL);146return;147}148doubles = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);149if (doubles == NULL) { /* exception thrown */150(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);151return;152}153154/* do conversion */155dstend = dstpos + ndoubles;156for ( ; dstpos < dstend; dstpos++) {157lval = (((jlong) bytes[srcpos + 0] & 0xFF) << 56) +158(((jlong) bytes[srcpos + 1] & 0xFF) << 48) +159(((jlong) bytes[srcpos + 2] & 0xFF) << 40) +160(((jlong) bytes[srcpos + 3] & 0xFF) << 32) +161(((jlong) bytes[srcpos + 4] & 0xFF) << 24) +162(((jlong) bytes[srcpos + 5] & 0xFF) << 16) +163(((jlong) bytes[srcpos + 6] & 0xFF) << 8) +164(((jlong) bytes[srcpos + 7] & 0xFF) << 0);165jlong_to_jdouble_bits(&lval);166u.l = lval;167doubles[dstpos] = (jdouble) u.d;168srcpos += 8;169}170171(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);172(*env)->ReleasePrimitiveArrayCritical(env, dst, doubles, 0);173}174175176177