arrayreferenceimpl.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 595 行 · 第 1/2 页

C
595
字号
/* * @(#)ArrayReferenceImpl.c	1.28 06/10/25 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  */#include "util.h"#include "ArrayReferenceImpl.h"#include "inStream.h"#include "outStream.h"static jboolean length(PacketInputStream *in, PacketOutputStream *out){    JNIEnv *env = getEnv();    jsize arrayLength;    jarray  array = inStream_readArrayRef(env, in);    if (inStream_error(in)) {        return JNI_TRUE;    }    arrayLength = JNI_FUNC_PTR(env,GetArrayLength)(env, array);    (void)outStream_writeInt(out, arrayLength);    return JNI_TRUE;}static void *newComponents(PacketOutputStream *out, jint length, size_t nbytes){    void *ptr = NULL;        if ( length > 0 ) {        ptr = jvmtiAllocate(length*((jint)nbytes));        if ( ptr == NULL ) {            outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));        } else {            (void)memset(ptr, 0, length*nbytes);        }    }    return ptr;}static voiddeleteComponents(void *ptr){    jvmtiDeallocate(ptr);}static voidwriteBooleanComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jboolean *components;        components = newComponents(out, length, sizeof(jboolean));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetBooleanArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeBoolean(out, components[i]);        }        deleteComponents(components);    }}static voidwriteByteComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jbyte *components;        components = newComponents(out, length, sizeof(jbyte));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetByteArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeByte(out, components[i]);        }        deleteComponents(components);    }}static voidwriteCharComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jchar *components;        components = newComponents(out, length, sizeof(jchar));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetCharArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeChar(out, components[i]);        }        deleteComponents(components);    }}static voidwriteShortComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jshort *components;        components = newComponents(out, length, sizeof(jshort));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetShortArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeShort(out, components[i]);        }        deleteComponents(components);    }}static voidwriteIntComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jint *components;        components = newComponents(out, length, sizeof(jint));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetIntArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeInt(out, components[i]);        }        deleteComponents(components);    }}static voidwriteLongComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jlong *components;        components = newComponents(out, length, sizeof(jlong));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetLongArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeLong(out, components[i]);        }        deleteComponents(components);    }}static voidwriteFloatComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jfloat *components;        components = newComponents(out, length, sizeof(jfloat));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetFloatArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeFloat(out, components[i]);        }        deleteComponents(components);    }}static voidwriteDoubleComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    jdouble *components;        components = newComponents(out, length, sizeof(jdouble));    if (components != NULL) {        jint i;        JNI_FUNC_PTR(env,GetDoubleArrayRegion)(env, array, index, length, components);        for (i = 0; i < length; i++) {            (void)outStream_writeDouble(out, components[i]);        }        deleteComponents(components);    }}static voidwriteObjectComponents(JNIEnv *env, PacketOutputStream *out,                     jarray array, jint index, jint length){    WITH_LOCAL_REFS(env, length) {        int i;        jobject component;                for (i = 0; i < length; i++) {            component = JNI_FUNC_PTR(env,GetObjectArrayElement)(env, array, index + i);            if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {                /* cleared by caller */                break;            }            (void)outStream_writeByte(out, specificTypeKey(env, component));            (void)outStream_writeObjectRef(env, out, component);        }    } END_WITH_LOCAL_REFS(env);}static jboolean getValues(PacketInputStream *in, PacketOutputStream *out){    JNIEnv *env = getEnv();    jint arrayLength;    jarray array;    jint index;    jint length;        array = inStream_readArrayRef(env, in);    if (inStream_error(in)) {        return JNI_TRUE;    }    index = inStream_readInt(in);    if (inStream_error(in)) {        return JNI_TRUE;    }    length = inStream_readInt(in);    if (inStream_error(in)) {        return JNI_TRUE;    }    arrayLength = JNI_FUNC_PTR(env,GetArrayLength)(env, array);    if (length == -1) {        length = arrayLength - index;    }    if ((index < 0) || (index > arrayLength - 1)) {        outStream_setError(out, JDWP_ERROR(INVALID_INDEX));        return JNI_TRUE;    }    if ((length < 0) || (length + index > arrayLength)) {        outStream_setError(out, JDWP_ERROR(INVALID_LENGTH));        return JNI_TRUE;    }    WITH_LOCAL_REFS(env, 1) {        jclass arrayClass;        char *signature = NULL;        char *componentSignature;        jbyte typeKey;        jvmtiError error;                arrayClass = JNI_FUNC_PTR(env,GetObjectClass)(env, array);        error = classSignature(arrayClass, &signature, NULL);        if (error != JVMTI_ERROR_NONE) {            goto err;        }        componentSignature = &signature[1];        typeKey = componentSignature[0];        (void)outStream_writeByte(out, typeKey);        (void)outStream_writeInt(out, length);        if (isObjectTag(typeKey)) {            writeObjectComponents(env, out, array, index, length);        } else {            switch (typeKey) {                case JDWP_TAG(BYTE):                    writeByteComponents(env, out, array, index, length);                    break;                        case JDWP_TAG(CHAR):                    writeCharComponents(env, out, array, index, length);                    break;                        case JDWP_TAG(FLOAT):

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?