Reading and writing byte streams other than MQMessage objects using IBM MQ classes for Java
These examples use the header classes to parse and manipulate IBM MQ header content when the data source is not an MQMessage object.
We can use the header classes to parse and manipulate IBM MQ header content even when the data source is something other than an MQMessage object. The MQHeader interface implemented by every header class provides the methods int read (java.io.DataInput message, int encoding, int characterSet) and int write (java.io.DataOutput message, int encoding, int characterSet). The com.ibm.mq.MQMessage class implements the java.io.DataInput and java.io.DataOutput interfaces. This means that we can use the two MQHeader methods to read and write MQMessage content, overriding the encoding and CCSID specified in the message descriptor. This is useful for messages that contain a chain of headers in different encodings. We can also obtain DataInput and DataOutput objects from other data streams, for example file or socket streams, or byte arrays carried in JMS messages. The java.io.DataInputStream classes implement DataInput and the java.io.DataOutputStream classes implement DataOutput. This example reads IBM MQ header content from a byte array:import java.io.*; import com.ibm.mq.headers.*; ... byte [] bytes = ... DataInput in = new DataInputStream (new ByteArrayInputStream (bytes)); MQHeaderIterator it = new MQHeaderIterator (in, CMQC.MQENC_NATIVE, CMQC.MQCCSI_DEFAULT);The line starting MQHeaderIterator could be replaced withMQDLH dlh = new MQDLH (in, CMQC.MQENC_NATIVE, CMQC.MQCCSI_DEFAULT); // or any other header typeThis example writes to a byte array using a DataOutputStream:MQHeader header = ... // Could be any header type ByteArrayOutputStream out = new ByteArrayOutputStream (); header.write (new DataOutputStream (out), CMQC.MQENC_NATIVE, CMQC.MQCCSI_DEFAULT); byte [] bytes = out.toByteArray ();When you work with streams in this way, be careful to use the correct values for the encoding and characterSet arguments. When reading headers, specify the encoding and CCSID with which the byte content was originally written. When writing headers, specify the encoding and CCSID that we want to produce. The data conversion is performed automatically by the header classes. Parent topic: Handling IBM MQ message headers with IBM MQ classes for Java