Byte arrays and general group parameters
The COBOL compiler looks for group parameters in Java-callable
programs
and group arguments in calls to Java™ that might match the
definition of a Java-compatible array type.
For any groups that do not meet the definition of a Java-compatible array type, the COBOL compiler assumes that you want to treat the group as a
flat array of bytes in Java (byte[]
) and that
the Java code will extract data from the byte array or write
data to the byte array in a way that matches the layout of the data in COBOL. For details about Java-compatible array types, see the Table:
Java-compatible array COBOL types in the Enterprise COBOL for z/OS® Language Reference.
For example, consider the following COBOL group:
01 g1.
03 g1-data1 pic s9(9) comp-5 value 12.
03 g1-data2 pic s9(18) comp-5 value 15.
This group will be identified by the compiler as corresponding to an array of bytes in Java. If g1
is used as a parameter in a COBOL
program with the JAVA-CALLABLE
directive, then it is assumed that the incoming data
from Java is in a byte array (byte[]
) and it
is the Java program's responsibility to set up the array
correctly.
Meanwhile, if the COBOL program passes g1
as an argument in a
CALL
to a static Java method, the
corresponding Java method must receive the group as an array
of bytes.
Here is an example of a static Java method that can be
called from COBOL and can receive group argument g1
as a byte array and access the
two subordinate data items of g1
:
import java.nio.*;
public class TestApp
{
public static void myMethod(byte[] g1)
{
ByteBuffer bb = ByteBuffer.wrap(g1, 0, 12);
System.out.println("data1 = " + bb.getInt(0));
System.out.println("data2 = " + bb.getLong(4));
}
}