This topic describes IBM® Toolbox for Java™ conversion classes for composite types.
The following example shows conversion from a Java structure to a byte array and back again. The example assumes that the same data format is used for both sending and receiving data.
// Create a structure of data types that corresponds to a structure
// that contains: - a four-byte number
// - four bytes of pad
// - an eight-byte number
// - 40 characters
AS400DataType[] myStruct =
{
new AS400Bin4(),
new AS400ByteArray(4),
new AS400Float8(),
new AS400Text(40)
};
// Create a conversion object using the structure.
AS400Structure myConverter = new AS400Structure(myStruct);
// Create the Java object that holds the data to send to the server.
Object[] myData =
{
new Integer(88), // the four-byte number
new byte[0], // the pad (let the conversion object 0 pad)
new Double(23.45), // the eight-byte floating point number
"This is my structure" // the character string
};
// Convert from Java object to byte array.
byte[] myAS400Data = myConverter.toBytes(myData);
// ... send the byte array to the server. Get data back from the
// server. The returned data will also be a byte array.
// Convert the returned data from IBM i to Java format.
Object[] myRoundTripData = (Object[])myConverter.toObject(myAS400Data,0);
// Pull the third object out of the structure. This is the double.
Double doubleObject = (Double) myRoundTripData[2];
// Extract the simple Java type from the Java object.
double d = doubleObject.doubleValue();