The body of a bytes message simply consists of a stream of uninterpreted bytes; its interpretation is entirely a matter of agreement between sender and receiver. This type of message is intended primarily for encoding message formats required by other existing message systems; Message Queue clients should generally use one of the other, more specific message types instead.
Composing a bytes message is similar to composing a stream message (see Composing Stream Messages). You create the message with the session method createBytesMessage, then use the methods shown in Table 2–13 to encode primitive values into the message’s byte stream: for example,
BytesMessage outMsg = mySession.createBytesMessage(); outMsg.writeUTF("The Meaning of Life"); outMsg.writeInt(42);Table 2–13 Bytes Message Composition Methods
Name |
Description |
---|---|
Write integer to message stream |
|
Write byte value to message stream |
|
Write byte array to message stream |
|
Write short integer to message stream |
|
Write long integer to message stream |
|
Write floating-point value to message stream |
|
Write double-precision value to message stream |
|
Write boolean value to message stream |
|
Write character to message stream |
|
Write UTF-8 string to message stream |
|
Write value of object to message stream |
|
Reset message stream |
As with stream and map messages, you can use the generic object-based method writeObject to handle values whose type is unknown at compilation time: for example, the statements
Integer meaningOfLife = new Integer(42); outMsg.writeObject(meaningOfLife);
are equivalent to
outMsg.writeInt(42);
outMsg.reset();
puts the message body in read-only mode and repositions the byte stream to the beginning, ready to read (see Processing Messages). Attempting to write further content to a message in this state will cause an exception (MessageNotWriteableException). The inherited Message method clearBody can be used to delete the entire message body and make it writeable again.