public final class BytesMessage extends Message implements BytesMessage
javax.jms.BytesMessage
interface.
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.
Modifier and Type | Field and Description |
---|---|
private DataInputStream |
inputStream
The stream for reading the written data.
|
private ByteArrayOutputStream |
outputBuffer
The array in which the written data is buffered.
|
private DataOutputStream |
outputStream
The stream in which body data is written.
|
private boolean |
prepared
true if the message has been sent since its last modif. |
jmsDest, logger, momMsg, propertiesRO, RObody, session
DEFAULT_DELIVERY_MODE, DEFAULT_PRIORITY, DEFAULT_TIME_TO_LIVE
Constructor and Description |
---|
BytesMessage()
Instantiates a bright new
BytesMessage . |
BytesMessage(Session sess,
Message momMsg)
Instantiates a
BytesMessage wrapping a consumed
MOM message containing a bytes array. |
Modifier and Type | Method and Description |
---|---|
void |
clearBody()
API method.
|
<T> T |
getBody(Class<T> c) |
long |
getBodyLength()
API method.
|
(package private) byte[] |
getBytes()
Get message content as byte array
|
protected <T> T |
getEffectiveBody(Class<T> c)
Get message body
|
protected void |
prepare()
Method actually preparing the message for sending by transfering the
local body into the wrapped MOM message.
|
boolean |
readBoolean()
API method.
|
byte |
readByte()
API method.
|
int |
readBytes(byte[] value)
API method.
|
int |
readBytes(byte[] value,
int length)
API method.
|
char |
readChar()
API method.
|
double |
readDouble()
API method.
|
float |
readFloat()
API method.
|
int |
readInt()
API method.
|
long |
readLong()
API method.
|
short |
readShort()
API method.
|
int |
readUnsignedByte()
API method.
|
int |
readUnsignedShort()
API method.
|
String |
readUTF()
API method.
|
void |
reset()
API method.
|
void |
writeBoolean(boolean value)
API method.
|
void |
writeByte(byte value)
API method.
|
void |
writeBytes(byte[] value)
API method.
|
void |
writeBytes(byte[] value,
int offset,
int length)
API method.
|
void |
writeChar(char value)
API method.
|
void |
writeDouble(double value)
API method.
|
void |
writeFloat(float value)
API method.
|
void |
writeInt(int value)
API method.
|
void |
writeLong(long value)
API method.
|
void |
writeObject(Object value)
API method.
|
void |
writeShort(short value)
API method.
|
void |
writeUTF(String value)
API method.
|
acknowledge, clearProperties, convertJMSMessage, getBooleanProperty, getByteProperty, getCompressedMinSize, getCompressionLevel, getDoubleProperty, getFloatProperty, getIntProperty, getJMSCorrelationID, getJMSCorrelationIDAsBytes, getJMSDeliveryMode, getJMSDeliveryTime, getJMSDestination, getJMSExpiration, getJMSMessageID, getJMSPriority, getJMSRedelivered, getJMSReplyTo, getJMSTimestamp, getJMSType, getLongProperty, getMomMsg, getObjectProperty, getProperties, getPropertyNames, 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
acknowledge, clearProperties, getBooleanProperty, getByteProperty, getDoubleProperty, getFloatProperty, getIntProperty, getJMSCorrelationID, getJMSCorrelationIDAsBytes, getJMSDeliveryMode, getJMSDestination, getJMSExpiration, getJMSMessageID, getJMSPriority, getJMSRedelivered, getJMSReplyTo, getJMSTimestamp, getJMSType, getLongProperty, getObjectProperty, getPropertyNames, getShortProperty, getStringProperty, propertyExists, setBooleanProperty, setByteProperty, setDoubleProperty, setFloatProperty, setIntProperty, setJMSCorrelationID, setJMSCorrelationIDAsBytes, setJMSDeliveryMode, setJMSDestination, setJMSExpiration, setJMSMessageID, setJMSPriority, setJMSRedelivered, setJMSReplyTo, setJMSTimestamp, setJMSType, setLongProperty, setObjectProperty, setShortProperty, setStringProperty
private transient ByteArrayOutputStream outputBuffer
private transient DataOutputStream outputStream
private transient DataInputStream inputStream
private transient boolean prepared
true
if the message has been sent since its last modif.BytesMessage()
BytesMessage
.BytesMessage(Session sess, Message momMsg) throws JMSException
BytesMessage
wrapping a consumed
MOM message containing a bytes array.sess
- The consuming session.momMsg
- The MOM message to wrap.IOException
- if an I/O error has occurredJMSException
public long getBodyLength() throws JMSException
getBodyLength
in interface BytesMessage
MessageNotReadableException
- If the message is WRITE-ONLY.JMSException
public void clearBody() throws JMSException
Calling this method leaves the message body in the same state as an empty body in a newly created message.
clearBody
in interface Message
clearBody
in class Message
JMSException
- In case of an error while closing the output or
input streams.public void writeBoolean(boolean value) throws JMSException
writeBoolean
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeByte(byte value) throws JMSException
writeByte
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeBytes(byte[] value) throws JMSException
writeBytes
in interface BytesMessage
value
- the byte array to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeBytes(byte[] value, int offset, int length) throws JMSException
writeBytes
in interface BytesMessage
value
- the byte array to be written.offset
- the initial offset within the byte arraylength
- the number of bytes to useMessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeChar(char value) throws JMSException
writeChar
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeDouble(double value) throws JMSException
writeDouble
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeFloat(float value) throws JMSException
writeFloat
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeInt(int value) throws JMSException
writeInt
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeLong(long value) throws JMSException
writeLong
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeShort(short value) throws JMSException
writeShort
in interface BytesMessage
value
- the value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeUTF(String value) throws JMSException
writeUTF
in interface BytesMessage
value
- the String value to be written.MessageNotWriteableException
- If the message body is read-only.JMSException
- If the value could not be written on the stream.public void writeObject(Object value) throws JMSException
This method works only for the objectified primitive object types (Integer, Double, Long ...), String objects, and byte arrays.
writeObject
in interface BytesMessage
value
- the primitive Java object to be written; it must not be null.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.public boolean readBoolean() throws JMSException
readBoolean
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public byte readByte() throws JMSException
readByte
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public int readUnsignedByte() throws JMSException
readUnsignedByte
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public short readShort() throws JMSException
readShort
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public int readUnsignedShort() throws JMSException
readUnsignedShort
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public char readChar() throws JMSException
readChar
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public int readInt() throws JMSException
readInt
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public long readLong() throws JMSException
readLong
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public float readFloat() throws JMSException
readFloat
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public double readDouble() throws JMSException
readDouble
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public int readBytes(byte[] value) throws JMSException
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.
readBytes
in interface BytesMessage
value
- the buffer into which the data is read.MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public int readBytes(byte[] value, int length) throws JMSException
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.
readBytes
in interface BytesMessage
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.MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public String readUTF() throws JMSException
readUTF
in interface BytesMessage
MessageNotReadableException
- If the message body is write-only.JMSException
- If an exception occurs while reading the bytes.public void reset() throws JMSException
reset
in interface BytesMessage
JMSException
- If an error occurs while closing the output
stream.protected void prepare() throws JMSException
prepare
in class Message
MessageFormatException
- If an error occurs while serializing.JMSException
byte[] getBytes() throws JMSException
JMSException
- If an error occurs while closing the output stream.public <T> T getBody(Class<T> c) throws JMSException
getBody
in class Message
JMSException
protected <T> T getEffectiveBody(Class<T> c) throws JMSException
Message
getEffectiveBody
in class Message
JMSException
- if the JMS provider fails to return a value due to some internal error.Copyright © 2015 ScalAgent D.T.. All Rights Reserved.