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