/*
 * Class:     java_io_ObjectInputStream
 * Method:    bytesToDoubles
 * Signature: ([BI[DII)V
 * 
 * Reconstitutes ndoubles double values from their byte representations.
 * Byte values are read from array src starting at offset srcpos; the
 * resulting double values are written to array dst starting at dstpos.
 */
JNIEXPORT void JNICALL 
Java_java_io_ObjectInputStream_bytesToDoubles(JNIEnv *env, 
					      jclass thisObj, 
					      jbyteArray src, 
					      jint srcpos, 
					      jdoubleArray dst, 
					      jint dstpos, 
					      jint ndoubles)

{
    union {
	jlong l;
	double d;
    } u;
    jdouble *doubles;
    jbyte *bytes;
    jsize dstend;
    jlong lval;

    if (ndoubles == 0)
	return;
    
    /* fetch source array */
    if (src == NULL) {
	JNU_ThrowNullPointerException(env, NULL);
	return;
    }
    bytes = (jbyte *)(*env)->GetPrimitiveArrayCritical(env, src, NULL);
    if (bytes == NULL)		/* exception thrown */
	return;
    
    /* fetch dest array */
    if (dst == NULL) {
	(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
	JNU_ThrowNullPointerException(env, NULL);
	return;
    }
    doubles = (jdouble *)(*env)->GetPrimitiveArrayCritical(env, dst, NULL);
    if (doubles == NULL) {	/* exception thrown */
	(*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
	return;
    }
    
    /* do conversion */
    dstend = dstpos + ndoubles;
    for ( ; dstpos < dstend; dstpos++) {
	lval = READ_JLONG_FROM_BUF(bytes, srcpos);
	jlong_to_jdouble_bits(&lval);
	u.l = lval;
	doubles[dstpos] = (jdouble) u.d;
	srcpos += 8;
    }
    
    (*env)->ReleasePrimitiveArrayCritical(env, src, bytes, JNI_ABORT);
    (*env)->ReleasePrimitiveArrayCritical(env, dst, doubles, 0);
}
Example #2
0
/* 
 * Find the double float corresponding to a given bit pattern
 */
JNIEXPORT jdouble JNICALL
Java_java_lang_Double_longBitsToDouble(JNIEnv *env, jclass unused, jlong v)
{
    union {
	jlong l;
	double d;
    } u;
    jlong_to_jdouble_bits(&v);
    u.l = v;
    return (jdouble)u.d;
}