Class StreamMessage

  • All Implemented Interfaces:
    jakarta.jms.Message, jakarta.jms.StreamMessage

    public final class StreamMessage
    extends Message
    implements jakarta.jms.StreamMessage
    Implements the jakarta.jms.StreamMessage interface.
    • Constructor Detail

      • StreamMessage

        StreamMessage()
               throws jakarta.jms.JMSException
        Instantiates a bright new StreamMessage.
        Throws:
        jakarta.jms.JMSException - In case of an error while creating the output stream.
      • StreamMessage

        StreamMessage​(Session session,
                      Message momMsg)
               throws jakarta.jms.JMSException
        Instantiates a StreamMessage wrapping a consumed MOM message containing a stream of bytes.
        Parameters:
        session - The consuming session.
        momMsg - The MOM message to wrap.
        Throws:
        jakarta.jms.JMSException - In case of an error while creating the input stream.
    • Method Detail

      • getBody

        public <T> T getBody​(Class<T> c)
                      throws jakarta.jms.JMSException
        Description copied from class: Message
        Returns the message body as an object of the specified type. This method may be called on any type of message except for StreamMessage. The message body must be capable of being assigned to the specified type. This means that the specified class or interface must be either the same as, or a superclass or superinterface of, the class of the message body.
        Specified by:
        getBody in interface jakarta.jms.Message
        Overrides:
        getBody in class Message
        Parameters:
        c - The type to which the message body will be assigned. If the message has no body then any type may be specified and null is returned.
        If the message is a TextMessage then this parameter must be set to String.class or another type to which a String is assignable.
        If the message is a ObjectMessage then parameter must be set to java.io.Serializable.class or another type to which the body is assignable.
        If the message is a MapMessage then this parameter must be set to java.util.Map.class (or java.lang.Object.class).
        If the message is a BytesMessage then this parameter must be set to byte[].class (or java.lang.Object.class). This method will reset the BytesMessage before and after use.
        If the message is a TextMessage, ObjectMessage, MapMessage or BytesMessage and the message has no body, then the above does not apply and this parameter may be set to any type; the returned value will always be null.
        If the message is a Message (but not one of its subtypes) then this parameter may be set to any type; the returned value will always be null.
        Returns:
        the message body
        Throws:
        jakarta.jms.MessageFormatException - if the message is a StreamMessage, if the message body cannot be assigned to the specified type, or if the message is an ObjectMessage and object deserialization fails.
        jakarta.jms.JMSException - if the JMS provider fails to get the message body due to some internal error.
      • isBodyAssignableTo

        public boolean isBodyAssignableTo​(Class c)
                                   throws jakarta.jms.JMSException
        Description copied from class: Message
        API 2.0 method
        Specified by:
        isBodyAssignableTo in interface jakarta.jms.Message
        Overrides:
        isBodyAssignableTo in class Message
        Parameters:
        c - the specified type Check if the message body is capable of being assigned to specified type
        Returns:
        true if Message is TextMessage or MapMessage, or BytesMessage, or ObjectMessage when it's deserializable false otherwise
        Throws:
        jakarta.jms.JMSException - if fail to return a value due to some internal error
      • getEffectiveBody

        protected <T> T getEffectiveBody​(Class<T> c)
                                  throws jakarta.jms.JMSException
        Get message body
        Overrides:
        getEffectiveBody in class Message
        Parameters:
        c - - The type to which the message body will be assigned.
        Returns:
        message body
        Throws:
        jakarta.jms.JMSException - if the JMS provider fails to return a value due to some internal error.
      • clearBody

        public void clearBody()
                       throws jakarta.jms.JMSException
        API method. Clears out the message body.

        Calling this method leaves the message body in the same state as an empty body in a newly created message.

        Specified by:
        clearBody in interface jakarta.jms.Message
        Overrides:
        clearBody in class Message
        Throws:
        jakarta.jms.JMSException - In case of an error while closing the input or output streams.
      • prepareWrite

        private void prepareWrite()
                           throws jakarta.jms.JMSException
        Internal method called before each writing operation.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is READ only.
        jakarta.jms.JMSException - If the stream could not be prepared for the writing operation.
      • writeBoolean

        public void writeBoolean​(boolean value)
                          throws jakarta.jms.JMSException
        API method. Writes a boolean to the stream message as a 1-byte value. The value true is written as the value 1; the value false is written as the value 0.
        Specified by:
        writeBoolean in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeByte

        public void writeByte​(byte value)
                       throws jakarta.jms.JMSException
        API method. Writes a byte to the stream message.
        Specified by:
        writeByte in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeBytes

        public void writeBytes​(byte[] value)
                        throws jakarta.jms.JMSException
        API method. Writes a byte array to the StreamMessage.

        Each byte array is written to the message as a separate byte array field. Consecutively written byte array fields are treated as distinct fields when the fields are read.

        Specified by:
        writeBytes in interface jakarta.jms.StreamMessage
        Parameters:
        value - the byte array to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeBytes

        public void writeBytes​(byte[] value,
                               int offset,
                               int length)
                        throws jakarta.jms.JMSException
        API method. Writes a portion of a byte array to the StreamMessage.

        The portion of the byte array is written to the message as a separate byte array field. Consecutively written byte array fields are treated as distinct fields when the fields are read.

        Specified by:
        writeBytes in interface jakarta.jms.StreamMessage
        Parameters:
        value - the byte array to be written.
        offset - the initial offset within the byte array
        length - the number of bytes to use
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeChar

        public void writeChar​(char value)
                       throws jakarta.jms.JMSException
        API method. Writes a char to the stream message.
        Specified by:
        writeChar in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeDouble

        public void writeDouble​(double value)
                         throws jakarta.jms.JMSException
        API method. Writes a double to the stream message.
        Specified by:
        writeDouble in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeFloat

        public void writeFloat​(float value)
                        throws jakarta.jms.JMSException
        API method. Writes a float to the stream message.
        Specified by:
        writeFloat in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeInt

        public void writeInt​(int value)
                      throws jakarta.jms.JMSException
        API method. Writes an int to the stream message.
        Specified by:
        writeInt in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeLong

        public void writeLong​(long value)
                       throws jakarta.jms.JMSException
        API method. Writes a long to the stream message.
        Specified by:
        writeLong in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeShort

        public void writeShort​(short value)
                        throws jakarta.jms.JMSException
        API method. Writes a short to the stream message.
        Specified by:
        writeShort in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeString

        public void writeString​(String value)
                         throws jakarta.jms.JMSException
        API method. Writes a String to the stream message.
        Specified by:
        writeString in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • writeObject

        public void writeObject​(Object value)
                         throws jakarta.jms.JMSException
        API method. Writes an object to the stream message. This method works only for the objectified primitive object types (Integer, Double, LongĀ ...), String objects, and byte arrays.
        Specified by:
        writeObject in interface jakarta.jms.StreamMessage
        Parameters:
        value - the value to be written.
        Throws:
        jakarta.jms.MessageNotWriteableException - If the message body is read-only.
        jakarta.jms.MessageFormatException - If the value type is invalid.
        jakarta.jms.JMSException - If the value could not be written on the stream.
      • readBoolean

        public boolean readBoolean()
                            throws jakarta.jms.JMSException
        API method. Reads a boolean from the StreamMessage.
        Specified by:
        readBoolean in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readByte

        public byte readByte()
                      throws jakarta.jms.JMSException
        API method. Reads a byte from the StreamMessage.
        Specified by:
        readByte in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readShort

        public short readShort()
                        throws jakarta.jms.JMSException
        API method. Reads a short from the StreamMessage.
        Specified by:
        readShort in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readChar

        public char readChar()
                      throws jakarta.jms.JMSException
        API method. Reads a char from the StreamMessage.
        Specified by:
        readChar in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readInt

        public int readInt()
                    throws jakarta.jms.JMSException
        API method. Reads an int from the StreamMessage.
        Specified by:
        readInt in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readLong

        public long readLong()
                      throws jakarta.jms.JMSException
        API method. Reads a long from the StreamMessage.
        Specified by:
        readLong in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readFloat

        public float readFloat()
                        throws jakarta.jms.JMSException
        API method. Reads a float from the StreamMessage.
        Specified by:
        readFloat in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readDouble

        public double readDouble()
                          throws jakarta.jms.JMSException
        API method. Reads a double from the StreamMessage.
        Specified by:
        readDouble in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readBytes

        public int readBytes​(byte[] bytes)
                      throws jakarta.jms.JMSException
        API method. Reads a byte array from the StreamMessage into the specified buffer.

        To read the field value, readBytes should be successively called until it returns a value less than the length of the read buffer. The value of the bytes in the buffer following the last byte read is undefined. If readBytes returns a value equal to the length of the buffer, a subsequent readBytes call must be made. If there are no more bytes to be read, this call returns -1.

        If the byte array field value is null, readBytes returns -1.

        If the byte array field value is empty, readBytes returns 0.

        Once the first readBytes call on a byte[] field value has been made, the full value of the field must be read before it is valid to read the next field. An attempt to read the next field before that has been done will throw a MessageFormatException.

        To read the byte field value into a new byte[] object, use the readObject method.

        Specified by:
        readBytes in interface jakarta.jms.StreamMessage
        Parameters:
        bytes - the buffer into which the data is read.
        Returns:
        the total number of bytes read into the buffer, or -1 if there is no more data because the end of the byte field has been reached.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readString

        public String readString()
                          throws jakarta.jms.JMSException
        API method. Reads a String from the StreamMessage.
        Specified by:
        readString in interface jakarta.jms.StreamMessage
        Returns:
        a Unicode string from the SreamMessage.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the expected type is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • readObject

        public Object readObject()
                          throws jakarta.jms.JMSException
        API method. Reads an objectified primitive type from the StreamMessage.
        Specified by:
        readObject in interface jakarta.jms.StreamMessage
        Returns:
        the value read.
        Throws:
        jakarta.jms.MessageNotReadableException - If the message body is write-only.
        jakarta.jms.MessageFormatException - If reading the body is not possible.
        jakarta.jms.MessageEOFException - Unexpected end of bytes array.
        jakarta.jms.JMSException - internal error
      • reset

        public void reset()
                   throws jakarta.jms.JMSException
        API method. Puts the message body in read-only mode and reset the stream to the beginning.
        Specified by:
        reset in interface jakarta.jms.StreamMessage
        Throws:
        jakarta.jms.JMSException - If an error occurs while closing the output stream.
      • prepare

        protected void prepare()
                        throws jakarta.jms.JMSException
        Method actually preparing the message for sending by transferring the local body into the wrapped MOM message.
        Overrides:
        prepare in class Message
        Throws:
        jakarta.jms.MessageFormatException - If an error occurs while serializing.
        jakarta.jms.JMSException