1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.jms;
  6. /** A <CODE>StreamMessage</CODE> object is used to send a stream of primitive
  7. * types in the Java programming language. It is filled and read sequentially.
  8. * It inherits from the <CODE>Message</CODE> interface
  9. * and adds a stream message body. Its methods are based largely on those
  10. * found in <CODE>java.io.DataInputStream</CODE> and
  11. * <CODE>java.io.DataOutputStream</CODE>.
  12. *
  13. * <P>The primitive types can be read or written explicitly using methods
  14. * for each type. They may also be read or written generically as objects.
  15. * For instance, a call to <CODE>StreamMessage.writeInt(6)</CODE> is
  16. * equivalent to <CODE>StreamMessage.writeObject(new Integer(6))</CODE>.
  17. * Both forms are provided, because the explicit form is convenient for
  18. * static programming, and the object form is needed when types are not known
  19. * at compile time.
  20. *
  21. * <P>When the message is first created, and when <CODE>clearBody</CODE>
  22. * is called, the body of the message is in write-only mode. After the
  23. * first call to <CODE>reset</CODE> has been made, the message body is in
  24. * read-only mode.
  25. * After a message has been sent, the client that sent it can retain and
  26. * modify it without affecting the message that has been sent. The same message
  27. * object can be sent multiple times.
  28. * When a message has been received, the provider has called
  29. * <CODE>reset</CODE> so that the message body is in read-only mode for the client.
  30. *
  31. * <P>If <CODE>clearBody</CODE> is called on a message in read-only mode,
  32. * the message body is cleared and the message body is in write-only mode.
  33. *
  34. * <P>If a client attempts to read a message in write-only mode, a
  35. * <CODE>MessageNotReadableException</CODE> is thrown.
  36. *
  37. * <P>If a client attempts to write a message in read-only mode, a
  38. * <CODE>MessageNotWriteableException</CODE> is thrown.
  39. *
  40. * <P><CODE>StreamMessage</CODE> objects support the following conversion
  41. * table. The marked cases must be supported. The unmarked cases must throw a
  42. * <CODE>JMSException</CODE>. The <CODE>String</CODE>-to-primitive conversions
  43. * may throw a runtime exception if the primitive's <CODE>valueOf()</CODE>
  44. * method does not accept it as a valid <CODE>String</CODE> representation of
  45. * the primitive.
  46. *
  47. * <P>A value written as the row type can be read as the column type.
  48. *
  49. * <PRE>
  50. * | | boolean byte short char int long float double String byte[]
  51. * |----------------------------------------------------------------------
  52. * |boolean | X X
  53. * |byte | X X X X X
  54. * |short | X X X X
  55. * |char | X X
  56. * |int | X X X
  57. * |long | X X
  58. * |float | X X X
  59. * |double | X X
  60. * |String | X X X X X X X X
  61. * |byte[] | X
  62. * |----------------------------------------------------------------------
  63. * </PRE>
  64. *
  65. * <P>Attempting to read a null value as a primitive type must be treated
  66. * as calling the primitive's corresponding <code>valueOf(String)</code>
  67. * conversion method with a null value. Since <code>char</code> does not
  68. * support a <code>String</code> conversion, attempting to read a null value
  69. * as a <code>char</code> must throw a <code>NullPointerException</code>.
  70. *
  71. * @version 1.0 - 6 August 1998
  72. * @author Mark Hapner
  73. * @author Rich Burridge
  74. *
  75. * @see javax.jms.Session#createStreamMessage()
  76. * @see javax.jms.BytesMessage
  77. * @see javax.jms.MapMessage
  78. * @see javax.jms.Message
  79. * @see javax.jms.ObjectMessage
  80. * @see javax.jms.TextMessage
  81. */
  82. public interface StreamMessage extends Message {
  83. /** Reads a <code>boolean</code> from the stream message.
  84. *
  85. * @return the <code>boolean</code> value read
  86. *
  87. * @exception JMSException if the JMS provider fails to read the message
  88. * due to some internal error.
  89. * @exception MessageEOFException if unexpected end of message stream has
  90. * been reached.
  91. * @exception MessageFormatException if this type conversion is invalid.
  92. * @exception MessageNotReadableException if the message is in write-only
  93. * mode.
  94. */
  95. boolean
  96. readBoolean() throws JMSException;
  97. /** Reads a <code>byte</code> value from the stream message.
  98. *
  99. * @return the next byte from the stream message as a 8-bit
  100. * <code>byte</code>
  101. *
  102. * @exception JMSException if the JMS provider fails to read the message
  103. * due to some internal error.
  104. * @exception MessageEOFException if unexpected end of message stream has
  105. * been reached.
  106. * @exception MessageFormatException if this type conversion is invalid.
  107. * @exception MessageNotReadableException if the message is in write-only
  108. * mode.
  109. */
  110. byte
  111. readByte() throws JMSException;
  112. /** Reads a 16-bit integer from the stream message.
  113. *
  114. * @return a 16-bit integer from the stream message
  115. *
  116. * @exception JMSException if the JMS provider fails to read the message
  117. * due to some internal error.
  118. * @exception MessageEOFException if unexpected end of message stream has
  119. * been reached.
  120. * @exception MessageFormatException if this type conversion is invalid.
  121. * @exception MessageNotReadableException if the message is in write-only
  122. * mode.
  123. */
  124. short
  125. readShort() throws JMSException;
  126. /** Reads a Unicode character value from the stream message.
  127. *
  128. * @return a Unicode character from the stream message
  129. *
  130. * @exception JMSException if the JMS provider fails to read the message
  131. * due to some internal error.
  132. * @exception MessageEOFException if unexpected end of message stream has
  133. * been reached.
  134. * @exception MessageFormatException if this type conversion is invalid
  135. * @exception MessageNotReadableException if the message is in write-only
  136. * mode.
  137. */
  138. char
  139. readChar() throws JMSException;
  140. /** Reads a 32-bit integer from the stream message.
  141. *
  142. * @return a 32-bit integer value from the stream message, interpreted
  143. * as an <code>int</code>
  144. *
  145. * @exception JMSException if the JMS provider fails to read the message
  146. * due to some internal error.
  147. * @exception MessageEOFException if unexpected end of message stream has
  148. * been reached.
  149. * @exception MessageFormatException if this type conversion is invalid.
  150. * @exception MessageNotReadableException if the message is in write-only
  151. * mode.
  152. */
  153. int
  154. readInt() throws JMSException;
  155. /** Reads a 64-bit integer from the stream message.
  156. *
  157. * @return a 64-bit integer value from the stream message, interpreted as
  158. * a <code>long</code>
  159. *
  160. * @exception JMSException if the JMS provider fails to read the message
  161. * due to some internal error.
  162. * @exception MessageEOFException if unexpected end of message stream has
  163. * been reached.
  164. * @exception MessageFormatException if this type conversion is invalid.
  165. * @exception MessageNotReadableException if the message is in write-only
  166. * mode.
  167. */
  168. long
  169. readLong() throws JMSException;
  170. /** Reads a <code>float</code> from the stream message.
  171. *
  172. * @return a <code>float</code> value from the stream message
  173. *
  174. * @exception JMSException if the JMS provider fails to read the message
  175. * due to some internal error.
  176. * @exception MessageEOFException if unexpected end of message stream has
  177. * been reached.
  178. * @exception MessageFormatException if this type conversion is invalid.
  179. * @exception MessageNotReadableException if the message is in write-only
  180. * mode.
  181. */
  182. float
  183. readFloat() throws JMSException;
  184. /** Reads a <code>double</code> from the stream message.
  185. *
  186. * @return a <code>double</code> value from the stream message
  187. *
  188. * @exception JMSException if the JMS provider fails to read the message
  189. * due to some internal error.
  190. * @exception MessageEOFException if unexpected end of message stream has
  191. * been reached.
  192. * @exception MessageFormatException if this type conversion is invalid.
  193. * @exception MessageNotReadableException if the message is in write-only
  194. * mode.
  195. */
  196. double
  197. readDouble() throws JMSException;
  198. /** Reads a <CODE>String</CODE> from the stream message.
  199. *
  200. * @return a Unicode string from the stream message
  201. *
  202. * @exception JMSException if the JMS provider fails to read the message
  203. * due to some internal error.
  204. * @exception MessageEOFException if unexpected end of message stream has
  205. * been reached.
  206. * @exception MessageFormatException if this type conversion is invalid.
  207. * @exception MessageNotReadableException if the message is in write-only
  208. * mode.
  209. */
  210. String
  211. readString() throws JMSException;
  212. /** Reads a byte array field from the stream message into the
  213. * specified <CODE>byte[]</CODE> object (the read buffer).
  214. *
  215. * <P>To read the field value, <CODE>readBytes</CODE> should be
  216. * successively called
  217. * until it returns a value less than the length of the read buffer.
  218. * The value of the bytes in the buffer following the last byte
  219. * read is undefined.
  220. *
  221. * <P>If <CODE>readBytes</CODE> returns a value equal to the length of the
  222. * buffer, a subsequent <CODE>readBytes</CODE> call must be made. If there
  223. * are no more bytes to be read, this call returns -1.
  224. *
  225. * <P>If the byte array field value is null, <CODE>readBytes</CODE>
  226. * returns -1.
  227. *
  228. * <P>If the byte array field value is empty, <CODE>readBytes</CODE>
  229. * returns 0.
  230. *
  231. * <P>Once the first <CODE>readBytes</CODE> call on a <CODE>byte[]</CODE>
  232. * field value has been made,
  233. * the full value of the field must be read before it is valid to read
  234. * the next field. An attempt to read the next field before that has
  235. * been done will throw a <CODE>MessageFormatException</CODE>.
  236. *
  237. * <P>To read the byte field value into a new <CODE>byte[]</CODE> object,
  238. * use the <CODE>readObject</CODE> method.
  239. *
  240. * @param value the buffer into which the data is read
  241. *
  242. * @return the total number of bytes read into the buffer, or -1 if
  243. * there is no more data because the end of the byte field has been
  244. * reached
  245. *
  246. * @exception JMSException if the JMS provider fails to read the message
  247. * due to some internal error.
  248. * @exception MessageEOFException if unexpected end of message stream has
  249. * been reached.
  250. * @exception MessageFormatException if this type conversion is invalid.
  251. * @exception MessageNotReadableException if the message is in write-only
  252. * mode.
  253. *
  254. * @see #readObject()
  255. */
  256. int
  257. readBytes(byte[] value) throws JMSException;
  258. /** Reads an object from the stream message.
  259. *
  260. * <P>This method can be used to return, in objectified format,
  261. * an object in the Java programming language ("Java object") that has
  262. * been written to the stream with the equivalent
  263. * <CODE>writeObject</CODE> method call, or its equivalent primitive
  264. * <CODE>write<I>type</I></CODE> method.
  265. *
  266. * <P>Note that byte values are returned as <CODE>byte[]</CODE>, not
  267. * <CODE>Byte[]</CODE>.
  268. *
  269. * <P>An attempt to call <CODE>readObject</CODE> to read a byte field
  270. * value into a new <CODE>byte[]</CODE> object before the full value of the
  271. * byte field has been read will throw a
  272. * <CODE>MessageFormatException</CODE>.
  273. *
  274. * @return a Java object from the stream message, in objectified
  275. * format (for example, if the object was written as an <CODE>int</CODE>,
  276. * an <CODE>Integer</CODE> is returned)
  277. *
  278. * @exception JMSException if the JMS provider fails to read the message
  279. * due to some internal error.
  280. * @exception MessageEOFException if unexpected end of message stream has
  281. * been reached.
  282. * @exception MessageFormatException if this type conversion is invalid.
  283. * @exception MessageNotReadableException if the message is in write-only
  284. * mode.
  285. *
  286. * @see #readBytes(byte[] value)
  287. */
  288. Object
  289. readObject() throws JMSException;
  290. /** Writes a <code>boolean</code> to the stream message.
  291. * The value <code>true</code> is written as the value
  292. * <code>(byte)1</code> the value <code>false</code> is written as
  293. * the value <code>(byte)0</code>.
  294. *
  295. * @param value the <code>boolean</code> value to be written
  296. *
  297. * @exception JMSException if the JMS provider fails to write the message
  298. * due to some internal error.
  299. * @exception MessageNotWriteableException if the message is in read-only
  300. * mode.
  301. */
  302. void
  303. writeBoolean(boolean value)
  304. throws JMSException;
  305. /** Writes a <code>byte</code> to the stream message.
  306. *
  307. * @param value the <code>byte</code> value to be written
  308. *
  309. * @exception JMSException if the JMS provider fails to write the message
  310. * due to some internal error.
  311. * @exception MessageNotWriteableException if the message is in read-only
  312. * mode.
  313. */
  314. void
  315. writeByte(byte value) throws JMSException;
  316. /** Writes a <code>short</code> to the stream message.
  317. *
  318. * @param value the <code>short</code> value to be written
  319. *
  320. * @exception JMSException if the JMS provider fails to write the message
  321. * due to some internal error.
  322. * @exception MessageNotWriteableException if the message is in read-only
  323. * mode.
  324. */
  325. void
  326. writeShort(short value) throws JMSException;
  327. /** Writes a <code>char</code> to the stream message.
  328. *
  329. * @param value the <code>char</code> value to be written
  330. *
  331. * @exception JMSException if the JMS provider fails to write the message
  332. * due to some internal error.
  333. * @exception MessageNotWriteableException if the message is in read-only
  334. * mode.
  335. */
  336. void
  337. writeChar(char value) throws JMSException;
  338. /** Writes an <code>int</code> to the stream message.
  339. *
  340. * @param value the <code>int</code> value to be written
  341. *
  342. * @exception JMSException if the JMS provider fails to write the message
  343. * due to some internal error.
  344. * @exception MessageNotWriteableException if the message is in read-only
  345. * mode.
  346. */
  347. void
  348. writeInt(int value) throws JMSException;
  349. /** Writes a <code>long</code> to the stream message.
  350. *
  351. * @param value the <code>long</code> value to be written
  352. *
  353. * @exception JMSException if the JMS provider fails to write the message
  354. * due to some internal error.
  355. * @exception MessageNotWriteableException if the message is in read-only
  356. * mode.
  357. */
  358. void
  359. writeLong(long value) throws JMSException;
  360. /** Writes a <code>float</code> to the stream message.
  361. *
  362. * @param value the <code>float</code> value to be written
  363. *
  364. * @exception JMSException if the JMS provider fails to write the message
  365. * due to some internal error.
  366. * @exception MessageNotWriteableException if the message is in read-only
  367. * mode.
  368. */
  369. void
  370. writeFloat(float value) throws JMSException;
  371. /** Writes a <code>double</code> to the stream message.
  372. *
  373. * @param value the <code>double</code> value to be written
  374. *
  375. * @exception JMSException if the JMS provider fails to write the message
  376. * due to some internal error.
  377. * @exception MessageNotWriteableException if the message is in read-only
  378. * mode.
  379. */
  380. void
  381. writeDouble(double value) throws JMSException;
  382. /** Writes a <code>String</code> to the stream message.
  383. *
  384. * @param value the <code>String</code> value to be written
  385. *
  386. * @exception JMSException if the JMS provider fails to write the message
  387. * due to some internal error.
  388. * @exception MessageNotWriteableException if the message is in read-only
  389. * mode.
  390. */
  391. void
  392. writeString(String value) throws JMSException;
  393. /** Writes a byte array field to the stream message.
  394. *
  395. * <P>The byte array <code>value</code> is written to the message
  396. * as a byte array field. Consecutively written byte array fields are
  397. * treated as two distinct fields when the fields are read.
  398. *
  399. * @param value the byte array value to be written
  400. *
  401. * @exception JMSException if the JMS provider fails to write the message
  402. * due to some internal error.
  403. * @exception MessageNotWriteableException if the message is in read-only
  404. * mode.
  405. */
  406. void
  407. writeBytes(byte[] value) throws JMSException;
  408. /** Writes a portion of a byte array as a byte array field to the stream
  409. * message.
  410. *
  411. * <P>The a portion of the byte array <code>value</code> is written to the
  412. * message as a byte array field. Consecutively written byte
  413. * array fields are treated as two distinct fields when the fields are
  414. * read.
  415. *
  416. * @param value the byte array value to be written
  417. * @param offset the initial offset within the byte array
  418. * @param length the number of bytes to use
  419. *
  420. * @exception JMSException if the JMS provider fails to write the message
  421. * due to some internal error.
  422. * @exception MessageNotWriteableException if the message is in read-only
  423. * mode.
  424. */
  425. void
  426. writeBytes(byte[] value, int offset, int length)
  427. throws JMSException;
  428. /** Writes an object to the stream message.
  429. *
  430. * <P>This method works only for the objectified primitive
  431. * object types (<code>Integer</code>, <code>Double</code>,
  432. * <code>Long</code> ...), <code>String</code> objects, and byte
  433. * arrays.
  434. *
  435. * @param value the Java object to be written
  436. *
  437. * @exception JMSException if the JMS provider fails to write the message
  438. * due to some internal error.
  439. * @exception MessageFormatException if the object is invalid.
  440. * @exception MessageNotWriteableException if the message is in read-only
  441. * mode.
  442. */
  443. void
  444. writeObject(Object value) throws JMSException;
  445. /** Puts the message body in read-only mode and repositions the stream
  446. * to the beginning.
  447. *
  448. * @exception JMSException if the JMS provider fails to reset the message
  449. * due to some internal error.
  450. * @exception MessageFormatException if the message has an invalid
  451. * format.
  452. */
  453. void
  454. reset() throws JMSException;
  455. }