Class BytesMessage
- java.lang.Object
-
- org.objectweb.joram.client.jms.Message
-
- org.objectweb.joram.client.jms.BytesMessage
-
- All Implemented Interfaces:
BytesMessage,Message
public final class BytesMessage extends Message implements BytesMessage
Implements thejavax.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.-
Fields inherited from class org.objectweb.joram.client.jms.Message
jmsDest, logger, momMsg, propertiesRO, RObody, session
-
Fields inherited from interface javax.jms.Message
DEFAULT_DELIVERY_DELAY, DEFAULT_DELIVERY_MODE, DEFAULT_PRIORITY, DEFAULT_TIME_TO_LIVE
-
-
Constructor Summary
Constructors Constructor Description BytesMessage()Instantiates a bright newBytesMessage.BytesMessage(Session sess, org.objectweb.joram.shared.messages.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.objectweb.joram.client.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 javax.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, org.objectweb.joram.shared.messages.Message momMsg) throws JMSException
Instantiates aBytesMessagewrapping a consumed MOM message containing a bytes array.- Parameters:
sess- The consuming session.momMsg- The MOM message to wrap.- Throws:
JMSException- if an error has occurred
-
-
Method Detail
-
getBodyLength
public long getBodyLength() throws 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 interfaceBytesMessage- Returns:
- the number of bytes in the message's body.
- Throws:
MessageNotReadableException- If the message is WRITE-ONLY.JMSException
-
clearBody
public void clearBody() throws 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.
- Specified by:
clearBodyin interfaceMessage- Overrides:
clearBodyin classMessage- Throws:
JMSException- In case of an error while closing the output or input streams.
-
writeBoolean
public void writeBoolean(boolean value) throws 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 interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeByte
public void writeByte(byte value) throws JMSExceptionAPI method. Writes a byte to the bytes message stream.- Specified by:
writeBytein interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeBytes
public void writeBytes(byte[] value) throws JMSExceptionAPI method. Writes a byte array to the bytes message stream.- Specified by:
writeBytesin interfaceBytesMessage- Parameters:
value- the byte array to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeBytes
public void writeBytes(byte[] value, int offset, int length) throws JMSExceptionAPI method. Writes a portion of a byte array to the bytes message stream.- Specified by:
writeBytesin interfaceBytesMessage- Parameters:
value- the byte array to be written.offset- the initial offset within the byte arraylength- the number of bytes to use- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeChar
public void writeChar(char value) throws JMSExceptionAPI method. Writes a char to the bytes message stream.- Specified by:
writeCharin interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeDouble
public void writeDouble(double value) throws JMSExceptionAPI method. Writes a double to the bytes message stream.- Specified by:
writeDoublein interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeFloat
public void writeFloat(float value) throws JMSExceptionAPI method. Writes a float to the bytes message stream.- Specified by:
writeFloatin interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeInt
public void writeInt(int value) throws JMSExceptionAPI method. Writes an int to the bytes message stream.- Specified by:
writeIntin interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeLong
public void writeLong(long value) throws JMSExceptionAPI method. Writes a long to the bytes message stream.- Specified by:
writeLongin interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeShort
public void writeShort(short value) throws JMSExceptionAPI method. Writes a short to the bytes message stream.- Specified by:
writeShortin interfaceBytesMessage- Parameters:
value- the value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeUTF
public void writeUTF(String value) throws JMSException
API method. Writes a string to the bytes message stream using UTF-8 encoding in a machine-independent manner.- Specified by:
writeUTFin interfaceBytesMessage- Parameters:
value- the String value to be written.- Throws:
MessageNotWriteableException- If the message body is read-only.JMSException- If the value could not be written on the stream.
-
writeObject
public void writeObject(Object value) throws 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 interfaceBytesMessage- Parameters:
value- the primitive Java object to be written; it must not be null.- Throws:
MessageNotWriteableException- If the message body is read-only.MessageFormatException- If the value type is invalid.JMSException- If the value could not be written on the stream.
-
readBoolean
public boolean readBoolean() throws JMSExceptionAPI method. Reads a boolean from the bytes message stream.- Specified by:
readBooleanin interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readByte
public byte readByte() throws JMSExceptionAPI method. Reads a byte from the bytes message stream.- Specified by:
readBytein interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readUnsignedByte
public int readUnsignedByte() throws JMSExceptionAPI method. Reads an unsigned byte from the bytes message stream.- Specified by:
readUnsignedBytein interfaceBytesMessage- Returns:
- the next byte from the bytes message stream, interpreted as an unsigned 8-bit number.
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readShort
public short readShort() throws JMSExceptionAPI method. Reads a short from the bytes message stream.- Specified by:
readShortin interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readUnsignedShort
public int readUnsignedShort() throws JMSExceptionAPI method. Reads an unsigned short from the bytes message stream.- Specified by:
readUnsignedShortin interfaceBytesMessage- Returns:
- the next two bytes from the bytes message stream, interpreted as an unsigned 16-bit integer.
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readChar
public char readChar() throws JMSExceptionAPI method. Reads a char from the bytes message stream.- Specified by:
readCharin interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readInt
public int readInt() throws JMSExceptionAPI method. Reads an int from the bytes message stream.- Specified by:
readIntin interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readLong
public long readLong() throws JMSExceptionAPI method. Reads a long from the bytes message stream.- Specified by:
readLongin interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readFloat
public float readFloat() throws JMSExceptionAPI method. Reads a float from the bytes message stream.- Specified by:
readFloatin interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readDouble
public double readDouble() throws JMSExceptionAPI method. Reads a double from the bytes message stream.- Specified by:
readDoublein interfaceBytesMessage- Returns:
- the value read
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readBytes
public int readBytes(byte[] value) throws 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 interfaceBytesMessage- 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:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readBytes
public int readBytes(byte[] value, int length) throws 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 interfaceBytesMessage- 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:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
readUTF
public String readUTF() throws JMSException
API method. Reads a string that has been encoded using a modified UTF-8 format from the bytes message stream.- Specified by:
readUTFin interfaceBytesMessage- Returns:
- a Unicode string from the bytes message stream.
- Throws:
MessageNotReadableException- If the message body is write-only.JMSException- If an exception occurs while reading the bytes.
-
reset
public void reset() throws JMSExceptionAPI method. Puts the message body in read-only mode and repositions the stream of bytes to the beginning.- Specified by:
resetin interfaceBytesMessage- Throws:
JMSException- If an error occurs while closing the output stream.
-
prepare
protected void prepare() throws JMSExceptionMethod actually preparing the message for sending by transfering the local body into the wrapped MOM message.- Overrides:
preparein classMessage- Throws:
MessageFormatException- If an error occurs while serializing.JMSException
-
getBytes
byte[] getBytes() throws JMSExceptionGet message content as byte array- Returns:
- byte[] the message content as byte array
- Throws:
JMSException- If an error occurs while closing the output stream.
-
getBody
public <T> T getBody(Class<T> c) throws 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 interfaceMessage- 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:
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.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 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:
JMSException- if the JMS provider fails to return a value due to some internal error.
-
-