KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > message > BytesMessageImpl


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms.message;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.DataInputStream JavaDoc;
12 import java.io.DataOutputStream JavaDoc;
13 import java.io.EOFException JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import javax.jms.BytesMessage JavaDoc;
17 import javax.jms.JMSException JavaDoc;
18 import javax.jms.MessageEOFException JavaDoc;
19 import javax.jms.MessageFormatException JavaDoc;
20 import javax.jms.MessageNotReadableException JavaDoc;
21 import javax.jms.MessageNotWriteableException JavaDoc;
22
23 /**
24  * <p/>
25  * A <CODE>StreamMessage</CODE> object is used to send a stream of primitive
26  * types in the Java programming language. It is filled and read sequentially.
27  * It inherits from the <CODE>Message</CODE> interface and adds a stream
28  * message body. Its methods are based largely on those found in <CODE>
29  * java.io.DataInputStream</CODE> and <CODE>java.io.DataOutputStream</CODE>.
30  *
31  * @author <a HREF="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
32  * @version Revision: 1.2 Date: 2002-12-08 19:22:15
33  */

34
35 public class BytesMessageImpl
36         extends JMSMessage
37         implements BytesMessage JavaDoc, Serializable JavaDoc {
38
39     //TODO: л
40
private transient DataOutputStream JavaDoc dos = null;
41     private transient DataInputStream JavaDoc dis = null;
42     private transient ByteArrayOutputStream JavaDoc baos = null;
43     private transient ByteArrayInputStream JavaDoc bais = null;
44     private byte[] buffer = new byte[0];
45
46     /**
47      * Default constructor.
48      */

49     public BytesMessageImpl() {
50         super();
51         baos = new ByteArrayOutputStream JavaDoc();
52         dos = new DataOutputStream JavaDoc(baos);
53     }
54
55     /**
56      * Reads a <code>boolean</code> from the bytes message stream.
57      *
58      * @return the <code>boolean</code> value read
59      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
60      * internal error.
61      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
62      * @throws javax.jms.MessageNotReadableException
63      * if the message is in write-only mode.
64      * @see javax.jms.BytesMessage#readBoolean()
65      */

66     public boolean readBoolean() throws JMSException JavaDoc {
67         if (isBodyModifiable()) {
68             throw new MessageNotReadableException JavaDoc("BytesMesage write_only");
69         }
70
71         try {
72             return getInputStream().readBoolean();
73         } catch (EOFException JavaDoc e) {
74             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
75         } catch (IOException JavaDoc e) {
76             throw new JMSException JavaDoc(e.getMessage());
77         }
78     }
79
80     /**
81      * Reads a signed 8-bit value from the bytes message stream.
82      *
83      * @return the next byte from the bytes message stream as a signed 8-bit
84      * <code>byte</code>
85      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
86      * internal error.
87      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
88      * @throws javax.jms.MessageNotReadableException
89      * if the message is in write-only mode.
90      * @see javax.jms.BytesMessage#readByte()
91      */

92     public byte readByte() throws JMSException JavaDoc {
93         if (isBodyModifiable()) {
94             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
95         }
96
97         try {
98             return this.getInputStream().readByte();
99         } catch (EOFException JavaDoc e) {
100             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
101         } catch (IOException JavaDoc e) {
102             throw new JMSException JavaDoc(e.getMessage());
103         }
104     }
105
106     /**
107      * Reads an unsigned 8-bit number from the bytes message stream.
108      *
109      * @return the next byte from the bytes message stream, interpreted as an
110      * unsigned 8-bit number
111      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
112      * internal error.
113      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
114      * @throws javax.jms.MessageNotReadableException
115      * if the message is in write-only mode.
116      * @see javax.jms.BytesMessage#readUnsignedByte()
117      */

118     public int readUnsignedByte() throws JMSException JavaDoc {
119         if (isBodyModifiable()) {
120             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
121         }
122
123         try {
124             return getInputStream().readUnsignedByte();
125         } catch (EOFException JavaDoc e) {
126             throw new MessageEOFException JavaDoc("BytesMessageimpl end_of_message");
127         } catch (IOException JavaDoc e) {
128             throw new JMSException JavaDoc(e.getMessage());
129         }
130     }
131
132     /**
133      * Reads a signed 16-bit number from the bytes message stream.
134      *
135      * @return the next two bytes from the bytes message stream, interpreted as
136      * a signed 16-bit number
137      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
138      * internal error.
139      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
140      * @throws javax.jms.MessageNotReadableException
141      * if the message is in write-only mode.
142      * @see javax.jms.BytesMessage#readShort()
143      */

144     public short readShort() throws JMSException JavaDoc {
145         if (isBodyModifiable()) {
146             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
147         }
148
149         try {
150             return getInputStream().readShort();
151         } catch (EOFException JavaDoc e) {
152             throw new MessageEOFException JavaDoc("Bytesmessage end_of_message");
153         } catch (IOException JavaDoc e) {
154             throw new JMSException JavaDoc(e.getMessage());
155         }
156     }
157
158     /**
159      * Reads an unsigned 16-bit number from the bytes message stream.
160      *
161      * @return the next two bytes from the bytes message stream, interpreted as
162      * an unsigned 16-bit integer
163      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
164      * internal error.
165      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
166      * @throws javax.jms.MessageNotReadableException
167      * if the message is in write-only mode.
168      * @see javax.jms.BytesMessage#readUnsignedShort()
169      */

170     public int readUnsignedShort() throws JMSException JavaDoc {
171         if (isBodyModifiable()) {
172             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
173         }
174
175         try {
176             return getInputStream().readUnsignedShort();
177         } catch (EOFException JavaDoc e) {
178             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
179         } catch (IOException JavaDoc e) {
180             throw new JMSException JavaDoc(e.getMessage());
181         }
182     }
183
184     /**
185      * Reads a Unicode character value from the bytes message stream.
186      *
187      * @return the next two bytes from the bytes message stream as a Unicode
188      * character
189      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
190      * internal error.
191      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
192      * @throws javax.jms.MessageNotReadableException
193      * if the message is in write-only mode.
194      * @see javax.jms.BytesMessage#readChar()
195      */

196     public char readChar() throws JMSException JavaDoc {
197         if (isBodyModifiable()) {
198             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
199         }
200
201         try {
202             return getInputStream().readChar();
203         } catch (EOFException JavaDoc e) {
204             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
205         } catch (IOException JavaDoc e) {
206             throw new JMSException JavaDoc(e.getMessage());
207         }
208     }
209
210     /**
211      * Reads a signed 32-bit integer from the bytes message stream.
212      *
213      * @return the next four bytes from the bytes message stream, interpreted
214      * as an <code>int</code>
215      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
216      * internal error.
217      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
218      * @throws javax.jms.MessageNotReadableException
219      * if the message is in write-only mode.
220      * @see javax.jms.BytesMessage#readInt()
221      */

222     public int readInt() throws JMSException JavaDoc {
223         if (isBodyModifiable()) {
224             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
225         }
226
227         try {
228             return getInputStream().readInt();
229         } catch (EOFException JavaDoc e1) {
230             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
231         } catch (IOException JavaDoc e) {
232             throw new JMSException JavaDoc(e.getMessage());
233         }
234     }
235
236     /**
237      * Reads a signed 64-bit integer from the bytes message stream.
238      *
239      * @return the next eight bytes from the bytes message stream, interpreted
240      * as a <code>long</code>
241      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
242      * internal error.
243      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
244      * @throws javax.jms.MessageNotReadableException
245      * if the message is in write-only mode.
246      * @see javax.jms.BytesMessage#readLong()
247      */

248     public long readLong() throws JMSException JavaDoc {
249         if (isBodyModifiable()) {
250             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
251         }
252
253         try {
254             return this.getInputStream().readLong();
255         } catch (EOFException JavaDoc e) {
256             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
257         } catch (IOException JavaDoc e) {
258             throw new JMSException JavaDoc(e.getMessage());
259         }
260     }
261
262     /**
263      * Reads a <code>float</code> from the bytes message stream.
264      *
265      * @return the next four bytes from the bytes message stream, interpreted
266      * as a <code>float</code>
267      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
268      * internal error.
269      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
270      * @throws javax.jms.MessageNotReadableException
271      * if the message is in write-only mode.
272      * @see javax.jms.BytesMessage#readFloat()
273      */

274     public float readFloat() throws JMSException JavaDoc {
275         if (isBodyModifiable()) {
276             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
277         }
278
279         try {
280             return getInputStream().readFloat();
281         } catch (EOFException JavaDoc e) {
282             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
283         } catch (IOException JavaDoc e) {
284             throw new JMSException JavaDoc(e.getMessage());
285         }
286     }
287
288     /**
289      * Reads a <code>double</code> from the bytes message stream.
290      *
291      * @return the next eight bytes from the bytes message stream, interpreted
292      * as a <code>double</code>
293      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
294      * internal error.
295      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
296      * @throws javax.jms.MessageNotReadableException
297      * if the message is in write-only mode.
298      * @see javax.jms.BytesMessage#readDouble()
299      */

300     public double readDouble() throws JMSException JavaDoc {
301         if (isBodyModifiable()) {
302             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
303         }
304
305         try {
306             return getInputStream().readDouble();
307         } catch (EOFException JavaDoc e) {
308             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
309         } catch (IOException JavaDoc e) {
310             throw new JMSException JavaDoc(e.getMessage());
311         }
312     }
313
314     /**
315      * Reads a string that has been encoded using a modified UTF-8 format from
316      * the bytes message stream.
317      * <p/>
318      * <P>
319      * For more information on the UTF-8 format, see "File System Safe UCS
320      * Transformation Format (FSS_UTF)", X/Open Preliminary Specification,
321      * X/Open Company Ltd., Document Number: P316. This information also
322      * appears in ISO/IEC 10646, Annex P.
323      *
324      * @return a Unicode string from the bytes message stream
325      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
326      * internal error.
327      * @throws javax.jms.MessageEOFException if unexpected end of bytes stream has been reached.
328      * @throws javax.jms.MessageNotReadableException
329      * if the message is in write-only mode.
330      * @see javax.jms.BytesMessage#readUTF()
331      */

332     public String JavaDoc readUTF() throws JMSException JavaDoc {
333         if (isBodyModifiable()) {
334             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
335         }
336
337         try {
338             return getInputStream().readUTF();
339         } catch (EOFException JavaDoc e) {
340             throw new MessageEOFException JavaDoc("BytesMessage end_of_message");
341         } catch (IOException JavaDoc e) {
342             throw new JMSException JavaDoc(e.getMessage());
343         }
344     }
345
346     /**
347      * Reads a byte array from the bytes message stream.
348      * <p/>
349      * <P>
350      * If the length of array <code>value</code> is less than the number of
351      * bytes remaining to be read from the stream, the array should be filled.
352      * A subsequent call reads the next increment, and so on.
353      * <p/>
354      * <P>
355      * If the number of bytes remaining in the stream is less than the length
356      * of array <code>value</code>, the bytes should be read into the array.
357      * The return value of the total number of bytes read will be less than the
358      * length of the array, indicating that there are no more bytes left to be
359      * read from the stream. The next read of the stream returns -1.
360      *
361      * @param value the buffer into which the data is read
362      * @return the total number of bytes read into the buffer, or -1 if there
363      * is no more data because the end of the stream has been reached
364      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
365      * internal error.
366      * @throws javax.jms.MessageNotReadableException
367      * if the message is in write-only mode.
368      * @see javax.jms.BytesMessage#readBytes(byte[])
369      */

370     public int readBytes(byte[] value) throws JMSException JavaDoc {
371         if (isBodyModifiable()) {
372             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
373         }
374
375         try {
376             return this.getInputStream().read(value);
377         } catch (IOException JavaDoc e) {
378             throw new JMSException JavaDoc(e.getMessage());
379         }
380     }
381
382     /**
383      * Reads a portion of the bytes message stream.
384      * <p/>
385      * <P>
386      * If the length of array <code>value</code> is less than the number of
387      * bytes remaining to be read from the stream, the array should be filled.
388      * A subsequent call reads the next increment, and so on.
389      * <p/>
390      * <P>
391      * If the number of bytes remaining in the stream is less than the length
392      * of array <code>value</code>, the bytes should be read into the array.
393      * The return value of the total number of bytes read will be less than the
394      * length of the array, indicating that there are no more bytes left to be
395      * read from the stream. The next read of the stream returns -1.
396      * <p/>
397      * <p/>
398      * If <code>length</code> is negative, or <code>length</code> is
399      * greater than the length of the array <code>value</code>, then an
400      * <code>IndexOutOfBoundsException</code> is thrown. No bytes will be
401      * read from the stream for this exception case.
402      *
403      * @param value the buffer into which the data is read
404      * @param length the number of bytes to read; must be less than or equal to
405      * <code>value.length</code>
406      * @return the total number of bytes read into the buffer, or -1 if there
407      * is no more data because the end of the stream has been reached
408      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
409      * internal error.
410      * @throws javax.jms.MessageNotReadableException
411      * if the message is in write-only mode.
412      * @see javax.jms.BytesMessage#readBytes(byte[], int)
413      */

414     public int readBytes(byte[] value, int length) throws JMSException JavaDoc {
415         if (isBodyModifiable()) {
416             throw new MessageNotReadableException JavaDoc("BytesMessage write_only");
417         }
418
419         if ((length < 0) || (length > value.length)) {
420             throw new IndexOutOfBoundsException JavaDoc();
421         }
422
423         try {
424             return getInputStream().read(value, 0, length);
425         } catch (IOException JavaDoc e) {
426             throw new JMSException JavaDoc(e.getMessage());
427         }
428     }
429
430     /**
431      * Writes a <code>boolean</code> to the bytes message stream as a 1-byte
432      * value. The value <code>true</code> is written as the value <code>(byte)1</code>;
433      * the value <code>false</code> is written as the value <code>(byte)0</code>.
434      *
435      * @param value the <code>boolean</code> value to be written
436      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
437      * some internal error.
438      * @throws javax.jms.MessageNotWriteableException
439      * if the message is in read-only mode.
440      * @see javax.jms.BytesMessage#writeBoolean(boolean)
441      */

442     public void writeBoolean(boolean value) throws JMSException JavaDoc {
443         if (!isBodyModifiable()) {
444             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
445         }
446
447         try {
448             getOutputStream().writeBoolean(value);
449         } catch (IOException JavaDoc e) {
450             throw new JMSException JavaDoc(e.getMessage());
451         }
452     }
453
454     /**
455      * Writes a <code>byte</code> to the bytes message stream as a 1-byte
456      * value.
457      *
458      * @param value the <code>byte</code> value to be written
459      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
460      * some internal error.
461      * @throws javax.jms.MessageNotWriteableException
462      * if the message is in read-only mode.
463      * @see javax.jms.BytesMessage#writeByte(byte)
464      */

465     public void writeByte(byte value) throws JMSException JavaDoc {
466         if (!isBodyModifiable()) {
467             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
468         }
469
470         try {
471             getOutputStream().writeByte((int) value);
472         } catch (IOException JavaDoc e) {
473             throw new JMSException JavaDoc(e.getMessage());
474         }
475     }
476
477     /**
478      * Writes a <code>short</code> to the bytes message stream as two bytes,
479      * high byte first.
480      *
481      * @param value the <code>short</code> to be written
482      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
483      * some internal error.
484      * @throws javax.jms.MessageNotWriteableException
485      * if the message is in read-only mode.
486      * @see javax.jms.BytesMessage#writeShort(short)
487      */

488     public void writeShort(short value) throws JMSException JavaDoc {
489         if (!isBodyModifiable()) {
490             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
491         }
492
493         try {
494             getOutputStream().writeShort((int) value);
495         } catch (IOException JavaDoc e) {
496             throw new JMSException JavaDoc(e.getMessage());
497         }
498     }
499
500     /**
501      * Writes a <code>char</code> to the bytes message stream as a 2-byte
502      * value, high byte first.
503      *
504      * @param value the <code>char</code> value to be written
505      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
506      * some internal error.
507      * @throws javax.jms.MessageNotWriteableException
508      * if the message is in read-only mode.
509      * @see javax.jms.BytesMessage#writeChar(char)
510      */

511     public void writeChar(char value) throws JMSException JavaDoc {
512         if (!isBodyModifiable()) {
513             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
514         }
515
516         try {
517             this.getOutputStream().writeChar((int) value);
518         } catch (IOException JavaDoc e) {
519             throw new JMSException JavaDoc(e.getMessage());
520         }
521     }
522
523     /**
524      * Writes an <code>int</code> to the bytes message stream as four bytes,
525      * high byte first.
526      *
527      * @param value the <code>int</code> to be written
528      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
529      * some internal error.
530      * @throws javax.jms.MessageNotWriteableException
531      * if the message is in read-only mode.
532      * @see javax.jms.BytesMessage#writeInt(int)
533      */

534     public void writeInt(int value) throws JMSException JavaDoc {
535         if (!isBodyModifiable()) {
536             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
537         }
538
539         try {
540             getOutputStream().writeInt(value);
541         } catch (IOException JavaDoc e) {
542             throw new JMSException JavaDoc(e.getMessage());
543         }
544     }
545
546     /**
547      * Writes a <code>long</code> to the bytes message stream as eight bytes,
548      * high byte first.
549      *
550      * @param value the <code>long</code> to be written
551      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
552      * some internal error.
553      * @throws javax.jms.MessageNotWriteableException
554      * if the message is in read-only mode.
555      * @see javax.jms.BytesMessage#writeLong(long)
556      */

557     public void writeLong(long value) throws JMSException JavaDoc {
558         if (!isBodyModifiable()) {
559             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
560         }
561
562         try {
563             this.getOutputStream().writeLong(value);
564         } catch (IOException JavaDoc e) {
565             throw new JMSException JavaDoc(e.getMessage());
566         }
567     }
568
569     /**
570      * Converts the <code>float</code> argument to an <code>int</code>
571      * using the <code>floatToIntBits</code> method in class <code>Float</code>,
572      * and then writes that <code>int</code> value to the bytes message
573      * stream as a 4-byte quantity, high byte first.
574      *
575      * @param value the <code>float</code> value to be written
576      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
577      * some internal error.
578      * @throws javax.jms.MessageNotWriteableException
579      * if the message is in read-only mode.
580      * @see javax.jms.BytesMessage#writeFloat(float)
581      */

582     public void writeFloat(float value) throws JMSException JavaDoc {
583         if (!isBodyModifiable()) {
584             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
585         }
586
587         try {
588             getOutputStream().writeFloat(value);
589         } catch (IOException JavaDoc e) {
590             throw new JMSException JavaDoc(e.getMessage());
591         }
592     }
593
594     /**
595      * Converts the <code>double</code> argument to a <code>long</code>
596      * using the <code>doubleToLongBits</code> method in class <code>Double</code>,
597      * and then writes that <code>long</code> value to the bytes message
598      * stream as an 8-byte quantity, high byte first.
599      *
600      * @param value the <code>double</code> value to be written
601      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
602      * some internal error.
603      * @throws javax.jms.MessageNotWriteableException
604      * if the message is in read-only mode.
605      * @see javax.jms.BytesMessage#writeDouble(double)
606      */

607     public void writeDouble(double value) throws JMSException JavaDoc {
608         if (!isBodyModifiable()) {
609             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
610         }
611
612         try {
613             getOutputStream().writeDouble(value);
614         } catch (IOException JavaDoc e) {
615             throw new JMSException JavaDoc(e.getMessage());
616         }
617     }
618
619     /**
620      * Writes a string to the bytes message stream using UTF-8 encoding in a
621      * machine-independent manner.
622      * <p/>
623      * <P>
624      * For more information on the UTF-8 format, see "File System Safe UCS
625      * Transformation Format (FSS_UTF)", X/Open Preliminary Specification,
626      * X/Open Company Ltd., Document Number: P316. This information also
627      * appears in ISO/IEC 10646, Annex P.
628      *
629      * @param value the <code>String</code> value to be written
630      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
631      * some internal error.
632      * @throws javax.jms.MessageNotWriteableException
633      * if the message is in read-only mode.
634      * @see javax.jms.BytesMessage#writeUTF(String)
635      */

636     public void writeUTF(String JavaDoc value) throws JMSException JavaDoc {
637         if (!isBodyModifiable()) {
638             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
639         }
640
641         try {
642             getOutputStream().writeUTF(value);
643         } catch (IOException JavaDoc e) {
644             throw new JMSException JavaDoc(e.getMessage());
645         }
646     }
647
648     /**
649      * Writes a byte array to the bytes message stream.
650      *
651      * @param value the byte array to be written
652      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
653      * some internal error.
654      * @throws javax.jms.MessageNotWriteableException
655      * if the message is in read-only mode.
656      * @see javax.jms.BytesMessage#writeBytes(byte[])
657      */

658     public void writeBytes(byte[] value) throws JMSException JavaDoc {
659         if (!isBodyModifiable()) {
660             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
661         }
662
663         try {
664             this.getOutputStream().write(value);
665         } catch (IOException JavaDoc e) {
666             throw new JMSException JavaDoc(e.getMessage());
667         }
668     }
669
670     /**
671      * Writes a portion of a byte array to the bytes message stream.
672      *
673      * @param value the byte array value to be written
674      * @param offset the initial offset within the byte array
675      * @param length the number of bytes to use
676      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
677      * some internal error.
678      * @throws javax.jms.MessageNotWriteableException
679      * if the message is in read-only mode.
680      * @see javax.jms.BytesMessage#writeBytes(byte[], int, int)
681      */

682     public void writeBytes(byte[] value, int offset, int length)
683             throws JMSException JavaDoc {
684         if (!isBodyModifiable()) {
685             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
686         }
687
688         try {
689             getOutputStream().write(value, offset, length);
690         } catch (IOException JavaDoc e) {
691             throw new JMSException JavaDoc(e.getMessage());
692         }
693     }
694
695     /**
696      * Writes an object to the bytes message stream.
697      * <p/>
698      * <P>
699      * This method works only for the objectified primitive object types (
700      * <code>Integer</code>,<code>Double</code>,<code>Long</code>
701      * &nbsp;...), <code>String</code> objects, and byte arrays.
702      *
703      * @param value the object in the Java programming language ("Java object")
704      * to be written; it must not be null
705      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
706      * some internal error.
707      * @throws javax.jms.MessageFormatException
708      * if the object is of an invalid type.
709      * @throws javax.jms.MessageNotWriteableException
710      * if the message is in read-only mode.
711      * @throws NullPointerException if the parameter <code>value</code> is null.
712      * @see javax.jms.BytesMessage#writeObject(Object)
713      */

714     public void writeObject(Object JavaDoc value) throws JMSException JavaDoc {
715         if (!isBodyModifiable()) {
716             throw new MessageNotWriteableException JavaDoc("BytesMessage read_only");
717         }
718
719         if (value instanceof Boolean JavaDoc) {
720             writeBoolean(((Boolean JavaDoc) value).booleanValue());
721         } else if (value instanceof Byte JavaDoc) {
722             writeByte(((Byte JavaDoc) value).byteValue());
723         } else if (value instanceof Character JavaDoc) {
724             writeChar(((Character JavaDoc) value).charValue());
725         } else if (value instanceof Double JavaDoc) {
726             writeDouble(((Double JavaDoc) value).doubleValue());
727         } else if (value instanceof Float JavaDoc) {
728             writeFloat(((Float JavaDoc) value).floatValue());
729         } else if (value instanceof Integer JavaDoc) {
730             writeInt(((Integer JavaDoc) value).intValue());
731         } else if (value instanceof Long JavaDoc) {
732             writeLong(((Long JavaDoc) value).longValue());
733         } else if (value instanceof Short JavaDoc) {
734             writeShort(((Short JavaDoc) value).shortValue());
735         } else if (value instanceof String JavaDoc) {
736             writeUTF((String JavaDoc) value);
737         } else if (value instanceof byte[]) {
738             writeBytes((byte[]) value);
739         } else {
740             throw new MessageFormatException JavaDoc("ByteMessage invalid_type");
741         }
742     }
743
744     /**
745      * Puts the message body in read-only mode and repositions the stream of
746      * bytes to the beginning.
747      *
748      * @throws javax.jms.JMSException if the JMS provider fails to reset the message due to
749      * some internal error.
750      * @throws javax.jms.MessageFormatException
751      * if the message has an invalid format.
752      * @see javax.jms.BytesMessage#reset()
753      */

754     public void reset() throws JMSException JavaDoc {
755         try {
756             if (dos != null) {
757                 dos.flush();
758                 dis =
759                         new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(baos.toByteArray()));
760                 setBodyModifiable(false);
761             }
762         } catch (IOException JavaDoc e) {
763             throw new JMSException JavaDoc("Can't reset message " + e.getMessage());
764         }
765     }
766
767     /**
768      * Sets the object reference in the message to null and sets the message to
769      * modifiable.
770      *
771      * @throws javax.jms.JMSException
772      */

773     public void clearBody() throws JMSException JavaDoc {
774         super.clearBody();
775         buffer = new byte[0];
776         baos = new ByteArrayOutputStream JavaDoc();
777         dos = new DataOutputStream JavaDoc(baos);
778         bais = null;
779         dis = null;
780     }
781
782     /**
783      * Initialise the input stream if it hasn't been intialised
784      *
785      * @return the input stream
786      */

787     private DataInputStream JavaDoc getInputStream() {
788         if (dis == null) {
789             bais = new ByteArrayInputStream JavaDoc(buffer);
790             dis = new DataInputStream JavaDoc(bais);
791         }
792         return dis;
793     }
794
795     /**
796      * Initialise the output stream if it hasn't been intialised
797      *
798      * @return the output stream
799      * @throws java.io.IOException if the output stream can't be created
800      */

801     private DataOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
802         if (dos == null) {
803             baos = new ByteArrayOutputStream JavaDoc();
804             dos = new DataOutputStream JavaDoc(baos);
805             dos.write(buffer);
806         }
807         return dos;
808     }
809
810     public long getBodyLength() throws JMSException JavaDoc {
811         return buffer.length;
812     }
813 }
814
Popular Tags