Class BytesMessage
- java.lang.Object
-
- org.ow2.joram.jakarta.jms.Message
-
- org.ow2.joram.jakarta.jms.BytesMessage
-
- All Implemented Interfaces:
jakarta.jms.BytesMessage,jakarta.jms.Message
public final class BytesMessage extends Message implements jakarta.jms.BytesMessage
Implements thejakarta.jms.BytesMessageinterface.A BytesMessage object is used to send a message containing a stream of uninterpreted bytes. It inherits from the Message interface and adds a bytes message body. The BytesMessage methods are based largely on those found in java.io.DataInputStream and java.io.DataOutputStream.
The primitive types can be written explicitly using methods for each type. They may also be written generically as objects. For instance, a call to BytesMessage.writeInt(6) is equivalent to BytesMessage.writeObject(new Integer(6)).
When the message is first created, and when clearBody is called, the body of the message is in write-only mode. After the first call to reset has been made, the message body is in read-only mode. After a message has been sent, the client that sent it can retain and modify it without affecting the message that has been sent. The same message object can be sent multiple times. When a message has been received, the provider has called reset so that the message body 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.
-
-
Field Summary
Fields Modifier and Type Field Description private DataInputStreaminputStreamThe stream for reading the written data.private ByteArrayOutputStreamoutputBufferThe array in which the written data is buffered.private DataOutputStreamoutputStreamThe stream in which body data is written.private booleanpreparedtrueif the message has been sent since its last modif.
-
Constructor Summary
Constructors Constructor Description BytesMessage()Instantiates a bright newBytesMessage.BytesMessage(Session sess, Message momMsg)Instantiates aBytesMessagewrapping a consumed MOM message containing a bytes array.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearBody()API method.<T> TgetBody(Class<T> c)Returns the message body as an object of the specified type.longgetBodyLength()API method.(package private) byte[]getBytes()Get message content as byte arrayprotected <T> TgetEffectiveBody(Class<T> c)Get message bodyprotected voidprepare()Method actually preparing the message for sending by transfering the local body into the wrapped MOM message.booleanreadBoolean()API method.bytereadByte()API method.intreadBytes(byte[] value)API method.intreadBytes(byte[] value, int length)API method.charreadChar()API method.doublereadDouble()API method.floatreadFloat()API method.intreadInt()API method.longreadLong()API method.shortreadShort()API method.intreadUnsignedByte()API method.intreadUnsignedShort()API method.StringreadUTF()API method.voidreset()API method.voidwriteBoolean(boolean value)API method.voidwriteByte(byte value)API method.voidwriteBytes(byte[] value)API method.voidwriteBytes(byte[] value, int offset, int length)API method.voidwriteChar(char value)API method.voidwriteDouble(double value)API method.voidwriteFloat(float value)API method.voidwriteInt(int value)API method.voidwriteLong(long value)API method.voidwriteObject(Object value)API method.voidwriteShort(short value)API method.voidwriteUTF(String value)API method.-
Methods inherited from class org.ow2.joram.jakarta.jms.Message
acknowledge, clearProperties, convertJMSMessage, dumpBody, dumpBody, getBooleanProperty, getByteProperty, getCompressedMinSize, getCompressionLevel, getDoubleProperty, getFloatProperty, getIntProperty, getJMSCorrelationID, getJMSCorrelationIDAsBytes, getJMSDeliveryMode, getJMSDeliveryTime, getJMSDestination, getJMSExpiration, getJMSMessageID, getJMSPriority, getJMSRedelivered, getJMSReplyTo, getJMSTimestamp, getJMSType, getLongProperty, getMomMsg, getObjectProperty, getProperties, getPropertyNames, getRawBody, getShortProperty, getStringProperty, isBodyAssignableTo, isCompressed, prepareJMSMessage, propertyExists, resetPropertiesRO, setBooleanProperty, setByteProperty, setCompressedMinSize, setCompressionLevel, setDoubleProperty, setFloatProperty, setIntProperty, setJMSCorrelationID, setJMSCorrelationIDAsBytes, setJMSDeliveryMode, setJMSDeliveryTime, setJMSDestination, setJMSExpiration, setJMSMessageID, setJMSPriority, setJMSRedelivered, setJMSReplyTo, setJMSTimestamp, setJMSType, setLongProperty, setObjectProperty, setShortProperty, setStringProperty, toString, toString, wrapMomMessage
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jakarta.jms.Message
acknowledge, clearProperties, 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
-
-
-
-
Field Detail
-
outputBuffer
private transient ByteArrayOutputStream outputBuffer
The array in which the written data is buffered.
-
outputStream
private transient DataOutputStream outputStream
The stream in which body data is written.
-
inputStream
private transient DataInputStream inputStream
The stream for reading the written data.
-
prepared
private transient boolean prepared
trueif the message has been sent since its last modif.
-
-
Constructor Detail
-
BytesMessage
BytesMessage()
Instantiates a bright newBytesMessage.
-
BytesMessage
BytesMessage(Session sess, Message momMsg) throws jakarta.jms.JMSException
Instantiates aBytesMessagewrapping a consumed MOM message containing a bytes array.- Parameters:
sess- The consuming session.momMsg- The MOM message to wrap.- Throws:
jakarta.jms.JMSException- if an error has occurred
-
-
Method Detail
-
getBodyLength
public long getBodyLength() throws jakarta.jms.JMSExceptionAPI method. Gets 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 interfacejakarta.jms.BytesMessage- Returns:
- the number of bytes in the message's body.
- Throws:
jakarta.jms.MessageNotReadableException- If the message is WRITE-ONLY.jakarta.jms.JMSException
-
clearBody
public void clearBody() throws jakarta.jms.JMSExceptionAPI method. Clears out the message body.Calling this method leaves the message body in the same state as an empty body in a newly created message.
-
writeBoolean
public void writeBoolean(boolean value) throws jakarta.jms.JMSExceptionAPI method. Writes a boolean to the bytes message stream as a 1-byte value. The value true is written as the value (byte)1; the value false is written as the value (byte)0.- Specified by:
writeBooleanin interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeByte
public void writeByte(byte value) throws jakarta.jms.JMSExceptionAPI method. Writes a byte to the bytes message stream.- Specified by:
writeBytein interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeBytes
public void writeBytes(byte[] value) throws jakarta.jms.JMSExceptionAPI method. Writes a byte array to the bytes message stream.- Specified by:
writeBytesin interfacejakarta.jms.BytesMessage- Parameters:
value- the byte array to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeBytes
public void writeBytes(byte[] value, int offset, int length) throws jakarta.jms.JMSExceptionAPI method. Writes a portion of a byte array to the bytes message stream.- Specified by:
writeBytesin interfacejakarta.jms.BytesMessage- Parameters:
value- the byte array to be written.offset- the initial offset within the byte arraylength- the number of bytes to use- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeChar
public void writeChar(char value) throws jakarta.jms.JMSExceptionAPI method. Writes a char to the bytes message stream.- Specified by:
writeCharin interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeDouble
public void writeDouble(double value) throws jakarta.jms.JMSExceptionAPI method. Writes a double to the bytes message stream.- Specified by:
writeDoublein interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeFloat
public void writeFloat(float value) throws jakarta.jms.JMSExceptionAPI method. Writes a float to the bytes message stream.- Specified by:
writeFloatin interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeInt
public void writeInt(int value) throws jakarta.jms.JMSExceptionAPI method. Writes an int to the bytes message stream.- Specified by:
writeIntin interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeLong
public void writeLong(long value) throws jakarta.jms.JMSExceptionAPI method. Writes a long to the bytes message stream.- Specified by:
writeLongin interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeShort
public void writeShort(short value) throws jakarta.jms.JMSExceptionAPI method. Writes a short to the bytes message stream.- Specified by:
writeShortin interfacejakarta.jms.BytesMessage- Parameters:
value- the value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeUTF
public void writeUTF(String value) throws jakarta.jms.JMSException
API method. Writes a string to the bytes message stream using UTF-8 encoding in a machine-independent manner.- Specified by:
writeUTFin interfacejakarta.jms.BytesMessage- Parameters:
value- the String value to be written.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.JMSException- If the value could not be written on the stream.
-
writeObject
public void writeObject(Object value) throws jakarta.jms.JMSException
API method. Writes an object to the bytes message stream.This method works only for the objectified primitive object types (Integer, Double, Long ...), String objects, and byte arrays.
- Specified by:
writeObjectin interfacejakarta.jms.BytesMessage- Parameters:
value- the primitive Java object to be written; it must not be null.- Throws:
jakarta.jms.MessageNotWriteableException- If the message body is read-only.jakarta.jms.MessageFormatException- If the value type is invalid.jakarta.jms.JMSException- If the value could not be written on the stream.
-
readBoolean
public boolean readBoolean() throws jakarta.jms.JMSExceptionAPI method. Reads a boolean from the bytes message stream.- Specified by:
readBooleanin interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readByte
public byte readByte() throws jakarta.jms.JMSExceptionAPI method. Reads a byte from the bytes message stream.- Specified by:
readBytein interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readUnsignedByte
public int readUnsignedByte() throws jakarta.jms.JMSExceptionAPI method. Reads an unsigned byte from the bytes message stream.- Specified by:
readUnsignedBytein interfacejakarta.jms.BytesMessage- Returns:
- the next byte from the bytes message stream, interpreted as an unsigned 8-bit number.
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readShort
public short readShort() throws jakarta.jms.JMSExceptionAPI method. Reads a short from the bytes message stream.- Specified by:
readShortin interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readUnsignedShort
public int readUnsignedShort() throws jakarta.jms.JMSExceptionAPI method. Reads an unsigned short from the bytes message stream.- Specified by:
readUnsignedShortin interfacejakarta.jms.BytesMessage- Returns:
- the next two bytes from the bytes message stream, interpreted as an unsigned 16-bit integer.
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readChar
public char readChar() throws jakarta.jms.JMSExceptionAPI method. Reads a char from the bytes message stream.- Specified by:
readCharin interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readInt
public int readInt() throws jakarta.jms.JMSExceptionAPI method. Reads an int from the bytes message stream.- Specified by:
readIntin interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readLong
public long readLong() throws jakarta.jms.JMSExceptionAPI method. Reads a long from the bytes message stream.- Specified by:
readLongin interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readFloat
public float readFloat() throws jakarta.jms.JMSExceptionAPI method. Reads a float from the bytes message stream.- Specified by:
readFloatin interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readDouble
public double readDouble() throws jakarta.jms.JMSExceptionAPI method. Reads a double from the bytes message stream.- Specified by:
readDoublein interfacejakarta.jms.BytesMessage- Returns:
- the value read
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readBytes
public int readBytes(byte[] value) throws jakarta.jms.JMSExceptionAPI method. Reads up to value.length bytes from the bytes message stream. A subsequent call reads the next increment, and so on.A return value of the total number of bytes read less than the length of the array indicates that there are no more bytes left to be read from the stream. The next read of the stream returns -1.
- Specified by:
readBytesin interfacejakarta.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:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readBytes
public int readBytes(byte[] value, int length) throws jakarta.jms.JMSExceptionAPI method. Reads up to length bytes of the bytes message stream. A subsequent call reads the next increment, and so on.A return value of the total number of bytes read less than the length parameter indicates that there are no more bytes left to be read from the stream. The next read of the stream returns -1.
If length is negative, or length is greater than the length of the array value, then an IndexOutOfBoundsException is thrown. No bytes will be read from the stream for this exception case.
- Specified by:
readBytesin interfacejakarta.jms.BytesMessage- Parameters:
value- the buffer into which the data is read.length- the number of bytes to read; must be less than or equal to value.length.- 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:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
readUTF
public String readUTF() throws jakarta.jms.JMSException
API method. Reads a string that has been encoded using a modified UTF-8 format from the bytes message stream.- Specified by:
readUTFin interfacejakarta.jms.BytesMessage- Returns:
- a Unicode string from the bytes message stream.
- Throws:
jakarta.jms.MessageNotReadableException- If the message body is write-only.jakarta.jms.JMSException- If an exception occurs while reading the bytes.
-
reset
public void reset() throws jakarta.jms.JMSExceptionAPI method. Puts the message body in read-only mode and repositions the stream of bytes to the beginning.- Specified by:
resetin interfacejakarta.jms.BytesMessage- Throws:
jakarta.jms.JMSException- If an error occurs while closing the output stream.
-
prepare
protected void prepare() throws jakarta.jms.JMSExceptionMethod actually preparing the message for sending by transfering the local body into the wrapped MOM message.
-
getBytes
byte[] getBytes() throws jakarta.jms.JMSExceptionGet message content as byte array- Returns:
- byte[] the message content as byte array
- Throws:
jakarta.jms.JMSException- If an error occurs while closing the output stream.
-
getBody
public <T> T getBody(Class<T> c) throws jakarta.jms.JMSException
Description copied from class:MessageReturns the message body as an object of the specified type. This method may be called on any type of message except for StreamMessage. The message body must be capable of being assigned to the specified type. This means that the specified class or interface must be either the same as, or a superclass or superinterface of, the class of the message body.- Specified by:
getBodyin interfacejakarta.jms.Message- Overrides:
getBodyin classMessage- Parameters:
c- The type to which the message body will be assigned. If the message has no body then any type may be specified and null is returned.
If the message is aTextMessagethen this parameter must be set toString.classor another type to which aStringis assignable.
If the message is aObjectMessagethen parameter must be set tojava.io.Serializable.classor another type to which the body is assignable.
If the message is aMapMessagethen this parameter must be set tojava.util.Map.class(orjava.lang.Object.class).
If the message is aBytesMessagethen this parameter must be set tobyte[].class(orjava.lang.Object.class). This method will reset theBytesMessagebefore and after use.
If the message is aTextMessage,ObjectMessage,MapMessageorBytesMessageand the message has no body, then the above does not apply and this parameter may be set to any type; the returned value will always be null.
If the message is aMessage(but not one of its subtypes) then this parameter may be set to any type; the returned value will always be null.- Returns:
- the message body
- Throws:
jakarta.jms.MessageFormatException- if the message is a StreamMessage, if the message body cannot be assigned to the specified type, or if the message is an ObjectMessage and object deserialization fails.jakarta.jms.JMSException- if the JMS provider fails to get the message body due to some internal error.
-
getEffectiveBody
protected <T> T getEffectiveBody(Class<T> c) throws jakarta.jms.JMSException
Description copied from class:MessageGet message body- Overrides:
getEffectiveBodyin classMessage- Parameters:
c- The type to which the message body will be assigned.- Returns:
- message body
- Throws:
jakarta.jms.JMSException- if the JMS provider fails to return a value due to some internal error.
-
-