com.ibm.mq.headers

Class MQHeaderIterator

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


    public class MQHeaderIterator
    extends com.ibm.mq.jmqi.JmqiObject
    implements java.util.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:
    MQHeaderList
    • Field Summary

      • Fields inherited from class com.ibm.mq.jmqi.JmqiObject

        COMP_JM, COMP_JN, COMP_JO
    • Constructor Summary

      Constructors 
      Constructor and Description
      MQHeaderIterator(java.io.DataInput message)
      Constructs an MQHeaderIterator around a message.
      MQHeaderIterator(java.io.DataInput message, java.lang.String format, int encoding, int characterSet)
      Constructs an MQHeaderIterator around a message or byte stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      java.lang.Object getBody()
      Skips any remaining headers.
      byte[] getBodyAsBytes()
      Skips any remaining headers
      java.lang.String getBodyAsText()
      Skips any remaining headers
      boolean hasNext()
      Indicates whether there is another Header
      java.lang.Object next()
      get the next Header
      MQHeader nextHeader()
      Synonym for the next method, but typed to return MQHeader.
      java.io.DataInput skipHeaders()
      Skips all headers remaining from the current message position.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining, remove
    • Constructor Detail

      • MQHeaderIterator

        public MQHeaderIterator(java.io.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(java.io.DataInput message,
                                java.lang.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 Detail

      • hasNext

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

        public java.lang.Object next()
        get the next Header
        Specified by:
        next in interface java.util.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 java.io.DataInput skipHeaders()
                                      throws MQDataException,
                                             java.io.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
        java.io.IOException
      • getBody

        public java.lang.Object getBody()
                                 throws MQDataException,
                                        java.io.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
        java.io.IOException
      • getBodyAsBytes

        public byte[] getBodyAsBytes()
                              throws MQDataException,
                                     java.io.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
        java.io.IOException
      • getBodyAsText

        public java.lang.String getBodyAsText()
                                       throws MQDataException,
                                              java.io.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
        java.io.IOException
(c) Copyright IBM Corp. 2008, 2016. All Rights Reserved.