Class MQHeaderIterator

java.lang.Object
com.ibm.mq.jmqi.JmqiObject
com.ibm.mq.headers.MQHeaderIterator
All Implemented Interfaces:
Iterator

public class MQHeaderIterator extends com.ibm.mq.jmqi.JmqiObject implements Iterator
An MQHeaderIterator returns headers one by one from a message. This class provides an easy way for an application to access header content in a message.

Examples

Print out all the headers in a message

  DataInput message = ...
  MQHeaderIterator it = new MQHeaderIterator (message);
  
  while (it.hasNext ())
  {
      MQHeader header = it.nextHeader ();
  
      System.out.println ("Header (type " + header.type + "): " + header); 
  }

Skip over all headers in a message

  DataInput message = ...
  MQHeaderIterator it = new MQHeaderIterator (message);
  it.skipHeaders ();

The message read cursor is now positioned immediately after the last header.

Extract the body of the message as a String, skipping any headers present

  DataInput message = ...;
  String body = new MQHeaderIterator (message).getBodyAsText ();

The getBodyAsBytes method is similar but returns the message body as a byte array.

For the ability to search directly for particular headers, remove headers from a message, and other goodies, see MQHeaderList.

See Also:
  • Constructor Details

    • MQHeaderIterator

      public MQHeaderIterator(DataInput message)
      Constructs an MQHeaderIterator around a message. Header content will be read from the current position of the message read cursor. The initial format, encoding and CCSID used to interpret the content are taken from the MQMD fields in the message.
      Parameters:
      message - the message.
    • MQHeaderIterator

      public MQHeaderIterator(DataInput message, String format, int encoding, int characterSet)
      Constructs an MQHeaderIterator around a message or byte stream. Header content will be read from the current position of the message read cursor or position in the byte stream. The initial format encoding and CCSID used to interpret the content are taken from the arguments.
      Parameters:
      message - the message.
      format - the message format (see CMQC.MQFMT_* for values)
      encoding - the numeric encoding. see (CMQC.MQENC_* for values)
      characterSet - the Coded Character Set Identifier.
  • Method Details

    • hasNext

      public boolean hasNext()
      Indicates whether there is another Header
      Specified by:
      hasNext in interface Iterator
      Returns:
      true or false as appropriate
    • next

      public Object next()
      get the next Header
      Specified by:
      next in interface Iterator
      Returns:
      the next header
    • nextHeader

      public MQHeader nextHeader()
      Synonym for the next method, but typed to return MQHeader.
      Returns:
      the next header
    • skipHeaders

      public DataInput skipHeaders() throws MQDataException, IOException
      Skips all headers remaining from the current message position.
      Returns:
      the original message or byte stream positioned immediately following the last header.
      Throws:
      MQDataException
      IOException
    • getBody

      public Object getBody() throws MQDataException, IOException
      Skips any remaining headers.
      Returns:
      the message body as either a String or a byte array according to the format of the last header. If the format is MQFMT_STRING, the body is a String; otherwise it is a byte array.
      Throws:
      MQDataException
      IOException
    • getBodyAsBytes

      public byte[] getBodyAsBytes() throws MQDataException, IOException
      Skips any remaining headers
      Returns:
      the message body as a byte array. This method returns a byte array even if the last format is MQFMT_STRING.
      Throws:
      MQDataException
      IOException
    • getBodyAsText

      public String getBodyAsText() throws MQDataException, IOException
      Skips any remaining headers
      Returns:
      the message body as a String. This method returns a String regardless of the format field of the last header. The byte content of the message body is converted into a String using the CCSID indicated by the last header (or by the MQMD, if there are no headers, or no headers with chaining information).
      Throws:
      MQDataException
      IOException