Sun Java System Message Queue 4.1 Developer's Guide for Java Clients

Composing Bytes Messages

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 

writeInt

Write integer to message stream 

writeByte

Write byte value to message stream 

writeBytes

Write byte array to message stream 

writeShort

Write short integer to message stream 

writeLong

Write long integer to message stream 

writeFloat

Write floating-point value to message stream 

writeDouble

Write double-precision value to message stream 

writeBoolean

Write boolean value to message stream 

writeChar

Write character to message stream 

writeUTF

Write UTF-8 string to message stream 

writeObject

Write value of object to message stream 

reset

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);

The message’s reset method

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.