StreamMessage (Java EE 5)

Java EE API


javax.jms Interface StreamMessage

All Superinterfaces:
Message

public interface StreamMessage
extends Message

Implements: Message

StreamMessage 对象用于发送 Java 编程语言中基本类型的流。按顺序填充和读取。它继承自 Message 接口并添加了流消息正文。其方法主要基于 java.io.DataInputStreamjava.io.DataOutputStream 中的方法。

可以使用用于每种类型的方法显式读取或写入基本类型。它们通常也能作为对象读取或写入。例如,对 StreamMessage.writeInt(6) 的调用等效于 StreamMessage.writeObject(new Integer(6))。这两种形式都提供,因为显式形式对于静态编程很方便,而编译期间类型未知时需要对象形式。

第一次创建消息,并调用 clearBody 时,消息的正文是只写模式。完成了对 reset 的初次调用后,消息正文是只读模式。发送了消息后,发送它的客户端可以保留并修改该消息,而不影响已经发送的消息。同一个消息对象可以发送多次。收到消息后,提供者调用 reset,以便消息正文对于客户端是只读模式。

如果在只读模式中对消息调用 clearBody,则消息正文被清除,并且消息正文处于只写模式。

如果客户端尝试在只写模式下读取消息,则抛出 MessageNotReadableException

如果客户端尝试在只读模式下写入消息,则抛出 MessageNotWriteableException

StreamMessage 对象支持下面的转换表。必须支持标记的情况。未标记的情况必须抛出 JMSException。如果基本类型的 valueOf() 方法不接受它作为有效的基本类型 String 表示形式,则 String 到基本类型的转换可能抛出运行时异常。

作为行类型写入的值可以作为列类型读取。

|        | boolean byte short char int long float double String byte[]
 |----------------------------------------------------------------------
|boolean |    X                                            X
|byte    |          X     X         X   X                  X
|short   |                X         X   X                  X
|char    |                     X                           X
|int     |                          X   X                  X
|long    |                              X                  X
|float   |                                    X     X      X
|double  |                                          X      X
|String  |    X     X     X         X   X     X     X      X
|byte[]  |                                                        X
 |----------------------------------------------------------------------
 

尝试将 null 值作为基本类型读取将视同于调用基本类型相应的具有 null 值的 valueOf(String) 转换方法。由于 char 不支持 String 转换,尝试将 null 值作为 char 读取必然抛出 NullPointerException

英文文档:

A StreamMessage object is used to send a stream of primitive types in the Java programming language. It is filled and read sequentially. It inherits from the Message interface and adds a stream message body. Its methods are based largely on those found in java.io.DataInputStream and java.io.DataOutputStream.

The primitive types can be read or written explicitly using methods for each type. They may also be read or written generically as objects. For instance, a call to StreamMessage.writeInt(6) is equivalent to StreamMessage.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, 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 body 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.

StreamMessage objects support the following conversion table. The marked cases must be supported. The unmarked cases must throw a JMSException. The String-to-primitive conversions may throw a runtime exception if the primitive's valueOf() method does not accept it as a valid String representation of the primitive.

A value written as the row type can be read as the column type.

 |        | boolean byte short char int long float double String byte[]
 |----------------------------------------------------------------------
 |boolean |    X                                            X
 |byte    |          X     X         X   X                  X   
 |short   |                X         X   X                  X   
 |char    |                     X                           X
 |int     |                          X   X                  X   
 |long    |                              X                  X   
 |float   |                                    X     X      X   
 |double  |                                          X      X   
 |String  |    X     X     X         X   X     X     X      X   
 |byte[]  |                                                        X
 |----------------------------------------------------------------------
 

Attempting to read a null value as a primitive type must be treated as calling the primitive's corresponding valueOf(String) conversion method with a null value. Since char does not support a String conversion, attempting to read a null value as a char must throw a NullPointerException.

Version:
1.0 - 6 August 1998
Author:
Mark Hapner, Rich Burridge
See Also:
Session.createStreamMessage(), BytesMessage, MapMessage, Message, ObjectMessage, TextMessage

Field Summary
 
Fields inherited from interface javax.jms.Message
 
Method Summary
 boolean
 byte
 int
 char
 double
 float
 int
 long
 Object
 short
 String
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 
Methods inherited from interface javax.jms.Message
 

Method Detail

public boolean readBoolean() throws JMSException
读取流消息中的 boolean
return 读取的 boolean
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readBoolean

boolean readBoolean()
                    throws JMSException
Reads a boolean from the stream message.

Returns:
the boolean value read
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public byte readByte() throws JMSException
读取流消息中的 byte 值。
return 流消息中的下一个字节,以 8 位 byte 的形式表示
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readByte

byte readByte()
              throws JMSException
Reads a byte value from the stream message.

Returns:
the next byte from the stream message as a 8-bit byte
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public short readShort() throws JMSException
读取流消息中的 16 位整数。
return 流消息中的 16 位整数
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readShort

short readShort()
                throws JMSException
Reads a 16-bit integer from the stream message.

Returns:
a 16-bit integer from the stream message
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public char readChar() throws JMSException
读取流消息中的 Unicode 字符值。
return 流消息中的 Unicode 字符
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readChar

char readChar()
              throws JMSException
Reads a Unicode character value from the stream message.

Returns:
a Unicode character from the stream message
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid
MessageNotReadableException - if the message is in write-only mode.

public int readInt() throws JMSException
读取流消息中的 32 位整数。
return 流消息中的 32 位整数值,解释为一个 int
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readInt

int readInt()
            throws JMSException
Reads a 32-bit integer from the stream message.

Returns:
a 32-bit integer value from the stream message, interpreted as an int
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public long readLong() throws JMSException
读取流消息中的 64 位整数。
return 流消息中的 64 位整数值,解释为一个 long
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readLong

long readLong()
              throws JMSException
Reads a 64-bit integer from the stream message.

Returns:
a 64-bit integer value from the stream message, interpreted as a long
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public float readFloat() throws JMSException
读取流消息中的 float
return 流消息中的 float
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readFloat

float readFloat()
                throws JMSException
Reads a float from the stream message.

Returns:
a float value from the stream message
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public double readDouble() throws JMSException
读取流消息中的 double
return 流消息中的 double
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readDouble

double readDouble()
                  throws JMSException
Reads a double from the stream message.

Returns:
a double value from the stream message
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public String readString() throws JMSException
读取流消息中的 String
return 流消息中的 Unicode 字符串
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
英文文档:

readString

String readString()
                  throws JMSException
Reads a String from the stream message.

Returns:
a Unicode string from the stream message
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.

public int readBytes(byte[] value) throws JMSException
将流消息中的 byte 数组字段读入指定的 byte[] 对象(读缓冲区)。

要读取该字段值,应接连调用 readBytes,直到它返回的值小于读缓冲区的长度为止。缓冲区中读取的最后一个字节之后的字节值是不确定的。

如果 readBytes 返回的值等于缓冲区长度,则必须进行后续 readBytes 调用。如果不再有字节可供读取,则此调用返回 -1。

如果 byte 数组字段值为 null,则 readBytes 返回 -1。

如果 byte 数组字段值为空,则 readBytes 返回 0。

在第一次对 byte[] 字段值进行 readBytes 调用后,必须读取该字段的完整值,然后才能有效读取下一个字段。在该字段完整值读取完成之前尝试读取下一个字段,将抛出 MessageFormatException

要将 byte 字段值读入新的 byte[] 对象,使用 readObject 方法。

value 将数据读入的缓冲区
return 读入缓冲区的总字节数,如果由于已到达 byte 字段末尾而不再有数据,则返回 -1
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
See also readObject()

英文文档:

readBytes

int readBytes(byte[] value)
              throws JMSException
Reads a byte array field from the stream message into the specified byte[] object (the read buffer).

To read the field value, readBytes should be successively called until it returns a value less than the length of the read buffer. The value of the bytes in the buffer following the last byte read is undefined.

If readBytes returns a value equal to the length of the buffer, a subsequent readBytes call must be made. If there are no more bytes to be read, this call returns -1.

If the byte array field value is null, readBytes returns -1.

If the byte array field value is empty, readBytes returns 0.

Once the first readBytes call on a byte[] field value has been made, the full value of the field must be read before it is valid to read the next field. An attempt to read the next field before that has been done will throw a MessageFormatException.

To read the byte field value into a new byte[] object, use the readObject method.

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 byte field has been reached
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.
See Also:
readObject()

public Object readObject() throws JMSException
读取流消息中的对象。

可以使用此方法(以对象化格式)返回 Java 编程语言中的对象(“Java 对象”),该对象已使用对等的 writeObject 方法调用或其对等的基本 writetype 方法写入流中。

注意,byte 值以 byte[] 的形式返回,而不是 Byte[]

在 byte 字段的完整值读取完成之前就尝试调用 readObject 将 byte 字段值读入新 byte[] 对象,将抛出 MessageFormatException

return 流消息中的 Java 对象,以对象化格式返回(例如,如果对象写作 int,则返回 Integer
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法读取消息。
ThrowsMessageEOFException: 如果到达不可预料的消息流末尾。
ThrowsMessageFormatException: 如果此类型转换无效。
ThrowsMessageNotReadableException: 如果消息是只写模式。
See also readBytes(byte[] value)

英文文档:

readObject

Object readObject()
                  throws JMSException
Reads an object from the stream message.

This method can be used to return, in objectified format, an object in the Java programming language ("Java object") that has been written to the stream with the equivalent writeObject method call, or its equivalent primitive writetype method.

Note that byte values are returned as byte[], not Byte[].

An attempt to call readObject to read a byte field value into a new byte[] object before the full value of the byte field has been read will throw a MessageFormatException.

Returns:
a Java object from the stream message, in objectified format (for example, if the object was written as an int, an Integer is returned)
Throws:
JMSException - if the JMS provider fails to read the message due to some internal error.
MessageEOFException - if unexpected end of message stream has been reached.
MessageFormatException - if this type conversion is invalid.
MessageNotReadableException - if the message is in write-only mode.
See Also:
readBytes(byte[] value)

public void writeBoolean(boolean value) throws JMSException
boolean 值写入流消息。值 true 以值 (byte)1 的形式被写入;值 false 以值 (byte)0 的形式被写入。
value 要写入的 boolean
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeBoolean

void writeBoolean(boolean value)
                  throws JMSException
Writes a boolean to the stream message. The value true is written as the value (byte)1; the value false is written as the value (byte)0.

Parameters:
value - the boolean value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeByte(byte value) throws JMSException
byte 值写入流消息。
value 要写入的 byte
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeByte

void writeByte(byte value)
               throws JMSException
Writes a byte to the stream message.

Parameters:
value - the byte value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeShort(short value) throws JMSException
short 值写入流消息。
value 要写入的 short
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeShort

void writeShort(short value)
                throws JMSException
Writes a short to the stream message.

Parameters:
value - the short value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeChar(char value) throws JMSException
char 值写入流消息。
value 要写入的 char
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeChar

void writeChar(char value)
               throws JMSException
Writes a char to the stream message.

Parameters:
value - the char value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeInt(int value) throws JMSException
int 值写入流消息。
value 要写入的 int
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeInt

void writeInt(int value)
              throws JMSException
Writes an int to the stream message.

Parameters:
value - the int value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeLong(long value) throws JMSException
long 值写入流消息。
value 要写入的 long
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeLong

void writeLong(long value)
               throws JMSException
Writes a long to the stream message.

Parameters:
value - the long value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeFloat(float value) throws JMSException
float 值写入流消息。
value 要写入的 float
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeFloat

void writeFloat(float value)
                throws JMSException
Writes a float to the stream message.

Parameters:
value - the float value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeDouble(double value) throws JMSException
double 值写入流消息。
value 要写入的 double
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeDouble

void writeDouble(double value)
                 throws JMSException
Writes a double to the stream message.

Parameters:
value - the double value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeString(String value) throws JMSException
String 值写入流消息。
value 要写入的 String
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。
英文文档:

writeString

void writeString(String value)
                 throws JMSException
Writes a String to the stream message.

Parameters:
value - the String value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeBytes(byte[] value) throws JMSException
将 byte 数组字段写入流消息。

byte 数组 value 以 byte 数组字段的形式写入消息。读取字段时,连续写入的 byte 数组字段被视为两个不同的字段。

value 要写入的 byte 数组
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。

英文文档:

writeBytes

void writeBytes(byte[] value)
                throws JMSException
Writes a byte array field to the stream message.

The byte array value is written to the message as a byte array field. Consecutively written byte array fields are treated as two distinct fields when the fields are read.

Parameters:
value - the byte array value to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeBytes(byte[] value, int offset, int length) throws JMSException
将 byte 数组的一部分以 byte 数组字段的形式写入流消息。

byte 数组 value 的一部分以 byte 数组字段的形式写入消息。读取字段时,连续写入的 byte 数组字段被视为两个不同的字段。

value 要写入的 byte 数组
offset byte 数组中的初始偏移量
length 要使用的字节数
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageNotWriteableException: 如果消息是只读模式。

英文文档:

writeBytes

void writeBytes(byte[] value,
                int offset,
                int length)
                throws JMSException
Writes a portion of a byte array as a byte array field to the stream message.

The a portion of the byte array value is written to the message as a byte array field. Consecutively written byte array fields are treated as two distinct fields when the fields are read.

Parameters:
value - the byte array value to be written
offset - the initial offset within the byte array
length - the number of bytes to use
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageNotWriteableException - if the message is in read-only mode.

public void writeObject(Object value) throws JMSException
将对象写入流消息。

此方法仅用于对象化的基本对象类型(IntegerDoubleLong 等)、String 对象和 byte 数组。

value 要写入的 Java 对象
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法写入消息。
ThrowsMessageFormatException: 如果对象无效。
ThrowsMessageNotWriteableException: 如果消息是只读模式。

英文文档:

writeObject

void writeObject(Object value)
                 throws JMSException
Writes an object to the stream message.

This method works only for the objectified primitive object types (Integer, Double, Long ...), String objects, and byte arrays.

Parameters:
value - the Java object to be written
Throws:
JMSException - if the JMS provider fails to write the message due to some internal error.
MessageFormatException - if the object is invalid.
MessageNotWriteableException - if the message is in read-only mode.

public void reset() throws JMSException
将消息正文置于只读模式并将流重新定位到开始处。
ThrowsJMSException: 如果 JMS 提供者由于某个内部错误无法重置消息。
ThrowsMessageFormatException: 如果消息存在无效的格式。
英文文档:

reset

void reset()
           throws JMSException
Puts the message body in read-only mode and repositions the stream to the beginning.

Throws:
JMSException - if the JMS provider fails to reset the message due to some internal error.
MessageFormatException - if the message has an invalid format.


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!