O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
Table 3.1 shows the conversion rules for each type. The left column shows the type<br />
written, and the right column shows how that type may be read. A JMSException is thrown<br />
by the accessor methods to indicate that the original type could not be converted to the<br />
type requested. This is the exception that would be thrown if you attempted to read long as<br />
a short.<br />
write( ) read( )<br />
boolean boolean, String<br />
Table 3.1. Type Conversion Rules<br />
byte byte, short, int, long, String<br />
short short, int, long, String<br />
char char, String<br />
Long long, String<br />
Int int, long, String<br />
float float, double, String<br />
double double, String<br />
String String, boolean, byte, short, int, long, float, double<br />
byte [] byte []<br />
String values can be converted to any primitive data type if they are formatted correctly. If<br />
the String value cannot be converted to the primitive type requested, a<br />
java.lang.NumberFormatException is thrown. However, most primitive values can be<br />
accessed as a String using the readString( ) method. The only exceptions to this rule are<br />
char values and byte arrays, which cannot be read as String values.<br />
The writeObject( ) method follows the rules outlined for the similar method in the<br />
Bytes<strong>Message</strong> class. Primitive wrappers are converted to their primitive counterparts. The<br />
readObject( ) method returns the appropriate object wrapper for primitive values, or a<br />
String or a byte array, depending on the type that was written to the stream. For example,<br />
if a value was written as a primitive int, it can be read as a java.lang.Integer object.<br />
The Stream<strong>Message</strong> also allows null values to be written to the stream. If a JMS client<br />
attempts to read a null value using the readObject( ) method, null is returned. The rest of<br />
the primitive accessor methods attempt to convert the null value to the requested type<br />
using the valueOf( ) operations. The readBoolean( ) method returns false for null<br />
values, while the other primitive property methods throw the<br />
java.lang.NumberFormatException. The readString( ) method returns null or possibly an<br />
empty String ("") depending on the implementation. The readChar( ) method throws a<br />
NullPointerException.<br />
If an exception is thrown while reading the Stream<strong>Message</strong>, the pointer in the stream is<br />
reset to the position it had just prior to the read operation that caused the exception. This<br />
allows the JMS client to recover gracefully from exceptions without losing the pointer's<br />
position in the stream.<br />
48