Class JMSBytesMessage
- java.lang.Object
-
- com.ibm.jms.JMSMessage
-
- com.ibm.jms.JMSBytesMessage
-
- All Implemented Interfaces:
- JmsMessage, java.io.Serializable, javax.jms.BytesMessage, javax.jms.Message
public class JMSBytesMessage extends JMSMessage implements javax.jms.BytesMessage
JMSBytesMessage is used to send a message containing a stream of uninterpreted bytes. The receiver of the message supplies the interpretation of the bytes.
Its methods are based largely on those found in java.io.DataInputStream and java.io.DataOutputStream.
This message type is for client encoding of existing message formats. If possible, one of the other self-defining message types should be used instead.
Although JMS allows the use of message properties with byte messages, it is typically not done since the inclusion of properties affects the format.
The primitive types can be written explicitly using methods for each type. They may also be written generically as objects. For example, a call to BytesMessage.writeInt(6) is equivalent to BytesMessage.writeObject(new Integer(6)). Both forms are provided because the explicit form is convenient for static programming and the object form is needed when types are not known at compile time.
When the message is first created, or when clearBody() has been called, the body of the message is in write-only mode. After the first call to the reset method has been made, the message is in read-only mode. When a message has been sent, the provider always calls reset to read its content. Likewise, when a message has been received, the provider calls reset so that the message is in read-only mode for the client.
If clearBody() is called on a message in read-only mode, the message body is cleared and the message is in write-only mode.
If a client attempts to read a message in write-only mode, a MessageNotReadableException is thrown.
If a client attempts to write a message in read-only mode, a MessageNotWriteableException is thrown.
JMSBytesMessage can be used by a JMS application to read or write a message that is sent to or from a non-Java application. As this non-Java application might be hosted on a platform with different integer or floating point encoding conventions, the JMSBytesMessage class includes routines to represent its numeric data types in a number of different ways.
The only character set supported by JMSBytesMessage is the Java version of UTF-8. This includes a two-byte length prefix, and is limited to strings less than 65536 bytes in long. Applications wanting to send a string in different character set have a choice of two methods:
- Send the message as a JMSTextMessage - if it is entirely made up of strings.
- Convert the String to a byte array and then write it into JMSBytesMessage using the writeBytes() method.
The type of numeric encoding to be used can be set by the transport code when importing or exporting the message as a byte array. The type of encoding is specified using an int, which is effectively the sum of two separate enums. This follows the convention laid down by IBM MQ in the MQMD.encoding field. For convenience, the constants defined here take precisely the same values as their MQENC counterparts.
To create a bytes message please call
Session.createBytesMessage(). Do not call the default constructor.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.StringsccsidThe SCCSID which is expanded when the file is extracted from CMVC
-
Constructor Summary
Constructors Constructor and Description JMSBytesMessage()Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description longgetBodyLength()Gets the number of bytes of the message body when the message is in read-only mode.booleanreadBoolean()Reads a boolean from the bytes message.bytereadByte()Reads a signed 8-bit value from the bytes message.intreadBytes(byte[] value)Reads a byte array from the bytes message.intreadBytes(byte[] value, int length)Reads a portion of the bytes message.charreadChar()Reads a Unicode character value from the bytes message.doublereadDouble()Reads a double from the bytes message.floatreadFloat()Reads a float from the bytes message.intreadInt()Reads a signed 32-bit integer from the bytes message.longreadLong()Reads a signed 64-bit integer from the bytes message.shortreadShort()Reads a signed 16-bit number from the bytes message.intreadUnsignedByte()Reads an unsigned 8-bit number from the bytes message.intreadUnsignedShort()Reads an unsigned 16-bit number from the bytes message.java.lang.StringreadUTF()Reads a string that has been encoded using a modified UTF-8 format from the bytes message.voidreset()Puts the message in read-only mode, and repositions the stream of bytes to the beginning.voidwriteBoolean(boolean value)Writes a boolean to the bytes message as a 1-byte value.voidwriteByte(byte value)Writes a byte to the bytes message as a 1-byte value.voidwriteBytes(byte[] value)Writes a byte array to the bytes message.voidwriteBytes(byte[] value, int offset, int length)Writes a portion of a byte array to the bytes message.voidwriteChar(char value)Writes a char to the bytes message as a 2-byte value, high byte first.voidwriteDouble(double value)Converts the double argument to a long using the doubleToLongBits() method in class Double, and then writes that long value to the stream message as an 8-byte quantity, high byte first.voidwriteFloat(float value)Converts the float argument to an int using the floatToIntBits() method in class Float, and then writes that int value to the stream message as a 4-byte quantity, high byte first.voidwriteInt(int value)Writes an int to the bytes message as four bytes, high byte first.voidwriteLong(long value)Writes a long to the bytes message as eight bytes, high byte first.voidwriteObject(java.lang.Object value)Writes a Java object to the bytes message.voidwriteShort(short value)Writes a short to the bytes message as two bytes, high byte first.voidwriteUTF(java.lang.String value)Writes a string to the BytesMessage using UTF-8 encoding in a machine-independent manner.-
Methods inherited from class com.ibm.jms.JMSMessage
acknowledge, clearBody, clearProperties, getBody, getBooleanProperty, getByteProperty, getDoubleProperty, getFloatProperty, getIntProperty, getJMSCorrelationID, getJMSCorrelationIDAsBytes, getJMSDeliveryMode, getJMSDeliveryTime, getJMSDestination, getJMSExpiration, getJMSMessageID, getJMSPriority, getJMSRedelivered, getJMSReplyTo, getJMSTimestamp, getJMSType, getLongProperty, getObjectProperty, getPropertyNames, getShortProperty, getStringProperty, isBodyAssignableTo, propertyExists, setBooleanProperty, setByteProperty, setDoubleProperty, setFloatProperty, setIntProperty, setJMSCorrelationID, setJMSCorrelationIDAsBytes, setJMSDeliveryMode, setJMSDeliveryTime, setJMSDestination, setJMSExpiration, setJMSMessageID, setJMSPriority, setJMSRedelivered, setJMSReplyTo, setJMSTimestamp, setJMSType, setLongProperty, setObjectProperty, setShortProperty, setStringProperty, toString, updateFromMessage
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface javax.jms.Message
acknowledge, clearBody, clearProperties, getBody, getBooleanProperty, getByteProperty, getDoubleProperty, getFloatProperty, getIntProperty, getJMSCorrelationID, getJMSCorrelationIDAsBytes, getJMSDeliveryMode, getJMSDeliveryTime, getJMSDestination, getJMSExpiration, getJMSMessageID, getJMSPriority, getJMSRedelivered, getJMSReplyTo, getJMSTimestamp, getJMSType, getLongProperty, getObjectProperty, getPropertyNames, getShortProperty, getStringProperty, isBodyAssignableTo, propertyExists, setBooleanProperty, setByteProperty, setDoubleProperty, setFloatProperty, setIntProperty, setJMSCorrelationID, setJMSCorrelationIDAsBytes, setJMSDeliveryMode, setJMSDeliveryTime, setJMSDestination, setJMSExpiration, setJMSMessageID, setJMSPriority, setJMSRedelivered, setJMSReplyTo, setJMSTimestamp, setJMSType, setLongProperty, setObjectProperty, setShortProperty, setStringProperty
-
Methods inherited from interface com.ibm.msg.client.jms.JmsMessage
getDelegate
-
-
-
-
Field Detail
-
sccsid
public static final java.lang.String sccsid
The SCCSID which is expanded when the file is extracted from CMVC- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JMSBytesMessage
public JMSBytesMessage()
Deprecated.To create a bytes message please call
Session.createBytesMessage().Do not call this default constructor.
-
-
Method Detail
-
getBodyLength
public long getBodyLength() throws javax.jms.JMSExceptionGets the number of bytes of the message body when the message is in read-only mode. The value returned can be used to allocate a byte array. The value returned is the entire length of the message body, regardless of where the pointer for reading the message is currently located.- Specified by:
getBodyLengthin interfacejavax.jms.BytesMessage- Returns:
- number of bytes in the message
- Throws:
javax.jms.JMSException- if the operation failsjavax.jms.MessageNotReadableException
-
readBoolean
public boolean readBoolean() throws javax.jms.JMSExceptionReads a boolean from the bytes message.- Specified by:
readBooleanin interfacejavax.jms.BytesMessage- Returns:
- the boolean value read
- Throws:
javax.jms.JMSException- if the operation fails
-
readByte
public byte readByte() throws javax.jms.JMSExceptionReads a signed 8-bit value from the bytes message.- Specified by:
readBytein interfacejavax.jms.BytesMessage- Returns:
- the next byte from the bytes message as a signed 8-bit byte
- Throws:
javax.jms.JMSException- if the operation fails
-
readBytes
public int readBytes(byte[] value) throws javax.jms.JMSExceptionReads a byte array from the bytes message. If there are sufficient bytes remaining in the stream the entire buffer is filled. If not, the buffer is partially filled.- Specified by:
readBytesin interfacejavax.jms.BytesMessage- Parameters:
value- 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 stream has been reached
- Throws:
javax.jms.JMSException- if the operation fails
-
readBytes
public int readBytes(byte[] value, int length) throws javax.jms.JMSExceptionReads a portion of the bytes message.- Specified by:
readBytesin interfacejavax.jms.BytesMessage- Parameters:
value- the buffer into which the data is read.length- the number of bytes to read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached
- Throws:
javax.jms.JMSException- if the operation fails
-
readChar
public char readChar() throws javax.jms.JMSExceptionReads a Unicode character value from the bytes message.- Specified by:
readCharin interfacejavax.jms.BytesMessage- Returns:
- the next two bytes from the bytes message as a Unicode character
- Throws:
javax.jms.JMSException- if the operation fails
-
readDouble
public double readDouble() throws javax.jms.JMSExceptionReads a double from the bytes message.- Specified by:
readDoublein interfacejavax.jms.BytesMessage- Returns:
- the next eight bytes from the bytes message, interpreted as a double
- Throws:
javax.jms.JMSException- if the operation fails
-
readFloat
public float readFloat() throws javax.jms.JMSExceptionReads a float from the bytes message.- Specified by:
readFloatin interfacejavax.jms.BytesMessage- Returns:
- the next four bytes from the bytes message, interpreted as a float
- Throws:
javax.jms.JMSException- if the operation fails
-
readInt
public int readInt() throws javax.jms.JMSExceptionReads a signed 32-bit integer from the bytes message.- Specified by:
readIntin interfacejavax.jms.BytesMessage- Returns:
- the next four bytes from the bytes message, interpreted as an int.
- Throws:
javax.jms.JMSException- if the operation fails
-
readLong
public long readLong() throws javax.jms.JMSExceptionReads a signed 64-bit integer from the bytes message.- Specified by:
readLongin interfacejavax.jms.BytesMessage- Returns:
- the next eight bytes from the bytes message, interpreted as a long
- Throws:
javax.jms.JMSException- if the operation fails
-
readShort
public short readShort() throws javax.jms.JMSExceptionReads a signed 16-bit number from the bytes message.- Specified by:
readShortin interfacejavax.jms.BytesMessage- Returns:
- the next two bytes from the bytes message, interpreted as a signed 16-bit number.
- Throws:
javax.jms.JMSException- if the operation fails
-
readUnsignedByte
public int readUnsignedByte() throws javax.jms.JMSExceptionReads an unsigned 8-bit number from the bytes message.- Specified by:
readUnsignedBytein interfacejavax.jms.BytesMessage- Returns:
- the next byte from the bytes message, interpreted as an unsigned 8-bit number.
- Throws:
javax.jms.JMSException- if the operation fails
-
readUnsignedShort
public int readUnsignedShort() throws javax.jms.JMSExceptionReads an unsigned 16-bit number from the bytes message.- Specified by:
readUnsignedShortin interfacejavax.jms.BytesMessage- Returns:
- the next two bytes from the bytes message, interpreted as an unsigned 16-bit integer.
- Throws:
javax.jms.JMSException- if the operation fails
-
readUTF
public java.lang.String readUTF() throws javax.jms.JMSExceptionReads a string that has been encoded using a modified UTF-8 format from the bytes message.- Specified by:
readUTFin interfacejavax.jms.BytesMessage- Returns:
- a Unicode string from the bytes message
- Throws:
javax.jms.JMSException- if the operation fails
-
reset
public void reset() throws javax.jms.JMSExceptionPuts the message in read-only mode, and repositions the stream of bytes to the beginning.- Specified by:
resetin interfacejavax.jms.BytesMessage- Throws:
javax.jms.JMSException- if JMS fails to reset the message due to some internal JMS errorjavax.jms.MessageFormatException- if message has an invalid format
-
writeBoolean
public void writeBoolean(boolean value) throws javax.jms.JMSExceptionWrites a boolean to the bytes message as a 1-byte value. The valuetrueis written out as the value (byte)1; the valuefalseis written out as the value (byte)0.- Specified by:
writeBooleanin interfacejavax.jms.BytesMessage- Parameters:
value- the boolean value to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeByte
public void writeByte(byte value) throws javax.jms.JMSExceptionWrites a byte to the bytes message as a 1-byte value.- Specified by:
writeBytein interfacejavax.jms.BytesMessage- Parameters:
value- the byte value to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeBytes
public void writeBytes(byte[] value) throws javax.jms.JMSExceptionWrites a byte array to the bytes message.- Specified by:
writeBytesin interfacejavax.jms.BytesMessage- Parameters:
value- the byte array to be written.- Throws:
javax.jms.JMSException- if the operation fails
-
writeBytes
public void writeBytes(byte[] value, int offset, int length) throws javax.jms.JMSExceptionWrites a portion of a byte array to the bytes message.- Specified by:
writeBytesin interfacejavax.jms.BytesMessage- Parameters:
value- the byte array value to be writtenoffset- the initial offset within the byte arraylength- the number of bytes to use- Throws:
javax.jms.JMSException- if the operation fails
-
writeChar
public void writeChar(char value) throws javax.jms.JMSExceptionWrites a char to the bytes message as a 2-byte value, high byte first.- Specified by:
writeCharin interfacejavax.jms.BytesMessage- Parameters:
value- the char value to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeDouble
public void writeDouble(double value) throws javax.jms.JMSExceptionConverts the double argument to a long using the doubleToLongBits() method in class Double, and then writes that long value to the stream message as an 8-byte quantity, high byte first.- Specified by:
writeDoublein interfacejavax.jms.BytesMessage- Parameters:
value- the double value to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeFloat
public void writeFloat(float value) throws javax.jms.JMSExceptionConverts the float argument to an int using the floatToIntBits() method in class Float, and then writes that int value to the stream message as a 4-byte quantity, high byte first.- Specified by:
writeFloatin interfacejavax.jms.BytesMessage- Parameters:
value- the float value to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeInt
public void writeInt(int value) throws javax.jms.JMSExceptionWrites an int to the bytes message as four bytes, high byte first.- Specified by:
writeIntin interfacejavax.jms.BytesMessage- Parameters:
value- the int to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeLong
public void writeLong(long value) throws javax.jms.JMSExceptionWrites a long to the bytes message as eight bytes, high byte first.- Specified by:
writeLongin interfacejavax.jms.BytesMessage- Parameters:
value- the long to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeObject
public void writeObject(java.lang.Object value) throws javax.jms.JMSExceptionWrites a Java object to the bytes message.Note that this method only works for the primitive wrapper classes (Integer, Double, Long ...), Strings and byte arrays.
- Specified by:
writeObjectin interfacejavax.jms.BytesMessage- Parameters:
value- the Java Object to be written- Throws:
javax.jms.JMSException- if the operation fails
-
writeShort
public void writeShort(short value) throws javax.jms.JMSExceptionWrites a short to the bytes message as two bytes, high byte first.- Specified by:
writeShortin interfacejavax.jms.BytesMessage- Parameters:
value- the short to be written.- Throws:
javax.jms.JMSException- if the operation fails
-
writeUTF
public void writeUTF(java.lang.String value) throws javax.jms.JMSExceptionWrites a string to the BytesMessage using UTF-8 encoding in a machine-independent manner.- Specified by:
writeUTFin interfacejavax.jms.BytesMessage- Parameters:
value- the String value to be written.- Throws:
javax.jms.JMSException- if the operation fails
-
-