Passing byte array back to Java

In Java when I am calling the getBytes(bytes) I do not know the size of the byte array. Only in the C++ code I will know how long the Byte Array is going to be.

In C++ I just want to copy "cbytes" into "jbytes" such that I will be able to make use of it in Java.

Java Code:

-

private static native int getBytes(byte[] bytes);

.

.

byte[] bytes = null;

int status = getBytes(bytes)

..

..

C++ Code

JNIEXPORT jint JNICALL Java_getBytes

(JNIEnv *, jclass, jbyteArray jbytes)

{

char * cbytes = getCBytes();

//

return 0;

}

Could some one let me know how this can be achieved.

Thanks

[725 byte] By [satyapal] at [2007-9-26 10:08:08]
# 1

This program includes a workaround for a bug in JDK versions 1.1 and 1.2 beta 2, that is, bug #4040920 in the JDC Bug Parade database. Normally, when you read from a low-level file input stream, you read chunks of, say, 1024 bytes at a time, and you don't worry whether that many bytes are actually left to read. This approach fails with the input stream returned by getInputStream for a given Zip file entry, and the workaround is to never try to read more bytes from the entry than are actually there.

kksenji at 2007-7-1 22:04:25 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Actually I am using JDK1.3.1 on Sun OS 5.8.

All I want to do is allocate and assign the "X" number of bytes in the C/C++ code and pass them back to Java Code through the parameter "jbytes" in the C++ method

"JNIEXPORT jint JNICALL Java_getBytes (JNIEnv *, jclass, jbyteArray jbytes)

"

Again I do not have any way of knowing the length of jbytes until actually the C++ code is executed.

Appreciate your reply..

satyapal at 2007-7-1 22:04:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Why don't you construct a new byte array and pass it back as the value of the method?
bschauwe at 2007-7-1 22:04:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

Well, that's what I am doing right now as a work around. i.e

jbyteArray jbytes = (jbyteArray)env->NewByteArray(count);

env->SetByteArrayRegion(jbytes,0,(count -1),(jbyte *)cbytes);

return jbytes;

And it works fine.

Its just that I am curious if its possible to do the way I wanted to do and also I want to return the status (of method call) instead of byte array.

Any clues ?

satyapal at 2007-7-1 22:04:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

1. I'm not saying you can't return a "status" as the value of a method call, but in my opinion a more "java-like" way to handle something wrong in a method is by throwing an exception, or through returning a null.

2. Java does not allow output parameters; they are only input parameters.

3. BUT: You could pass in an object, and then use it's methods to change data local to the object. For example, you might pass in a ByteArrayHolder object and call its takeNewArray(array) method.

bschauwe at 2007-7-1 22:04:26 > top of Java-index,Java HotSpot Virtual Machine,Specifications...