The session method createStreamMessage returns a new, empty stream message. You can then use the methods shown in Table 2–10 to write primitive data values into the message body, similarly to writing to a data stream: for example,
StreamMessage outMsg = mySession.createStreamMessage(); outMsg.writeString("The Meaning of Life"); outMsg.writeInt(42);Table 2–10 Stream 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 string to message stream |
|
Write value of object to message stream |
|
Reset message stream |
As a convenience for handling values whose types are not known until execution time, the writeObject method accepts a string or an objectified primitive value of class Integer, Byte, Short, Long, Float, Double , Boolean, or Character and writes the corresponding string or primitive value to the message stream: for example, the statements
Integer meaningOfLife = new Integer(42); outMsg.writeObject(meaningOfLife);
are equivalent to
outMsg.writeInt(42);
This method will throw an exception (MessageFormatException) if the argument given to it is not of class String or one of the objectified primitive classes.
Once you’ve written the entire message contents to the stream, the reset method
outMsg.reset();
puts the message body in read-only mode and repositions the stream to the beginning, ready to read (see Processing Messages). When the message is in this state, any attempt to write to the message stream will throw the exception MessageNotWriteableException. A call to the clearBody method (inherited from the superinterface Message) deletes the entire message body and makes it writeable again.