KickJava   Java API By Example, From Geeks To Geeks.

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


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.JMSException JavaDoc;
17 import javax.jms.MessageEOFException JavaDoc;
18 import javax.jms.MessageFormatException JavaDoc;
19 import javax.jms.MessageNotReadableException JavaDoc;
20 import javax.jms.MessageNotWriteableException JavaDoc;
21 import javax.jms.StreamMessage JavaDoc;
22
23
24 /**
25  * <p/>
26  * A <CODE>BytesMessage</CODE> object is used to send a message containing a
27  * stream of uninterpreted bytes. It inherits from the <CODE>Message</CODE>
28  * interface and adds a bytes message body. The receiver of the message
29  * supplies the interpretation of the bytes.
30  * </p>
31  *
32  * @author <a HREF="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
33  * @version Revision: 1.1 Date: 2002-12-08 22:46:11
34  */

35
36 public class StreamMessageImpl
37         extends JMSMessage
38         implements StreamMessage JavaDoc, Serializable JavaDoc {
39
40     public static final byte TYPE_NULL = 0;
41     public static final byte TYPE_BOOLEAN = 1;
42     public static final byte TYPE_BYTE = 2;
43     public static final byte TYPE_BYTE_ARRAY = 3;
44     public static final byte TYPE_SHORT = 4;
45     public static final byte TYPE_CHAR = 5;
46     public static final byte TYPE_INT = 6;
47     public static final byte TYPE_LONG = 7;
48     public static final byte TYPE_FLOAT = 8;
49     public static final byte TYPE_DOUBLE = 9;
50     public static final byte TYPE_STRING = 10;
51
52     private transient DataOutputStream JavaDoc dos = null;
53     private transient DataInputStream JavaDoc dis = null;
54     private transient ByteArrayOutputStream JavaDoc baos = null;
55     private transient ByteArrayInputStream JavaDoc bais = null;
56     private byte[] buffer = null;
57     private int readBytes = 0;
58     private int byteArrayLength = 0;
59
60     private static final String JavaDoc[] TYPE_NAMES =
61             {
62                 "null",
63                 "boolean",
64                 "byte",
65                 "byte[]",
66                 "short",
67                 "char",
68                 "int",
69                 "long",
70                 "float",
71                 "double",
72                 "String"};
73
74     /**
75      * Default constructor.
76      */

77     public StreamMessageImpl() {
78         super();
79         buffer = new byte[0];
80         baos = new ByteArrayOutputStream JavaDoc();
81         dos = new DataOutputStream JavaDoc(baos);
82     }
83
84     /**
85      * Reads a <code>boolean</code> from the stream message.
86      *
87      * @return the <code>boolean</code> value read
88      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
89      * internal error.
90      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
91      * @throws javax.jms.MessageFormatException
92      * if this type conversion is invalid.
93      * @throws javax.jms.MessageNotReadableException
94      * if the message is in write-only mode.
95      * @see javax.jms.StreamMessage#readBoolean()
96      */

97     public boolean readBoolean() throws JMSException JavaDoc {
98         prepareRead();
99         try {
100             return Conversions.getBoolean(readNext());
101         } catch (MessageFormatException JavaDoc e) {
102             try {
103                 dis.reset();
104             } catch (IOException JavaDoc ex) {
105                 throw new JMSException JavaDoc(ex.getMessage());
106             }
107             throw new MessageFormatException JavaDoc(e.getMessage());
108         }
109     }
110
111     /**
112      * Reads a <code>byte</code> value from the stream message.
113      *
114      * @return the next byte from the stream message as a 8-bit <code>byte</code>
115      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
116      * internal error.
117      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
118      * @throws javax.jms.MessageFormatException
119      * if this type conversion is invalid.
120      * @throws javax.jms.MessageNotReadableException
121      * if the message is in write-only mode.
122      * @see javax.jms.StreamMessage#readByte()
123      */

124     public byte readByte() throws JMSException JavaDoc {
125         prepareRead();
126         try {
127             return Conversions.getByte(readNext());
128         } catch (MessageFormatException JavaDoc e) {
129             try {
130                 dis.reset();
131             } catch (IOException JavaDoc ex) {
132                 throw new JMSException JavaDoc(ex.getMessage());
133             }
134             throw new MessageFormatException JavaDoc(e.getMessage());
135
136         } catch (NumberFormatException JavaDoc e) {
137             try {
138                 dis.reset();
139             } catch (IOException JavaDoc ex) {
140                 throw new JMSException JavaDoc(ex.getMessage());
141             }
142             throw new JMSException JavaDoc(e.getMessage());
143         }
144     }
145
146     /**
147      * Reads a 16-bit integer from the stream message.
148      *
149      * @return a 16-bit integer from the stream message
150      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
151      * internal error.
152      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
153      * @throws javax.jms.MessageFormatException
154      * if this type conversion is invalid.
155      * @throws javax.jms.MessageNotReadableException
156      * if the message is in write-only mode.
157      * @see javax.jms.StreamMessage#readShort()
158      */

159     public short readShort() throws JMSException JavaDoc {
160         prepareRead();
161         try {
162             return Conversions.getShort(readNext());
163         } catch (MessageFormatException JavaDoc e) {
164             try {
165                 dis.reset();
166             } catch (IOException JavaDoc ex) {
167                 throw new JMSException JavaDoc(ex.getMessage());
168             }
169             throw new MessageFormatException JavaDoc(e.getMessage());
170         } catch (NumberFormatException JavaDoc e) {
171             try {
172                 dis.reset();
173             } catch (IOException JavaDoc ex) {
174                 throw new JMSException JavaDoc(ex.getMessage());
175             }
176             throw new JMSException JavaDoc(e.getMessage());
177         }
178     }
179
180     /**
181      * Reads a Unicode character value from the stream message.
182      *
183      * @return a Unicode character from the stream message
184      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
185      * internal error.
186      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
187      * @throws javax.jms.MessageFormatException
188      * if this type conversion is invalid
189      * @throws javax.jms.MessageNotReadableException
190      * if the message is in write-only mode.
191      * @see javax.jms.StreamMessage#readChar()
192      */

193     public char readChar() throws JMSException JavaDoc {
194         prepareRead();
195         try {
196             return Conversions.getChar(readNext());
197         } catch (MessageFormatException JavaDoc e) {
198             try {
199                 dis.reset();
200             } catch (IOException JavaDoc ex) {
201                 throw new JMSException JavaDoc(ex.getMessage());
202             }
203             throw new MessageFormatException JavaDoc(e.getMessage());
204         } catch (NullPointerException JavaDoc e) {
205             try {
206                 dis.reset();
207             } catch (IOException JavaDoc ex) {
208                 throw new JMSException JavaDoc(ex.getMessage());
209             }
210             throw new JMSException JavaDoc(e.getMessage());
211         }
212     }
213
214     /**
215      * Reads a 32-bit integer from the stream message.
216      *
217      * @return a 32-bit integer value from the stream message, interpreted as
218      * an <code>int</code>
219      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
220      * internal error.
221      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
222      * @throws javax.jms.MessageFormatException
223      * if this type conversion is invalid.
224      * @throws javax.jms.MessageNotReadableException
225      * if the message is in write-only mode.
226      * @see javax.jms.StreamMessage#readInt()
227      */

228     public int readInt() throws JMSException JavaDoc {
229         prepareRead();
230         try {
231             return Conversions.getInt(readNext());
232         } catch (MessageFormatException JavaDoc e) {
233             try {
234                 dis.reset();
235             } catch (IOException JavaDoc ex) {
236                 throw new JMSException JavaDoc(ex.getMessage());
237             }
238             throw new MessageFormatException JavaDoc(e.getMessage());
239         } catch (NumberFormatException JavaDoc e) {
240             try {
241                 dis.reset();
242             } catch (IOException JavaDoc ex) {
243                 throw new JMSException JavaDoc(ex.getMessage());
244             }
245             throw new JMSException JavaDoc(e.getMessage());
246         }
247     }
248
249     /**
250      * Reads a 64-bit integer from the stream message.
251      *
252      * @return a 64-bit integer value from the stream message, interpreted as a
253      * <code>long</code>
254      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
255      * internal error.
256      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
257      * @throws javax.jms.MessageFormatException
258      * if this type conversion is invalid.
259      * @throws javax.jms.MessageNotReadableException
260      * if the message is in write-only mode.
261      * @see javax.jms.StreamMessage#readLong()
262      */

263     public long readLong() throws JMSException JavaDoc {
264         prepareRead();
265         try {
266             return Conversions.getLong(readNext());
267         } catch (MessageFormatException JavaDoc e) {
268             try {
269                 dis.reset();
270             } catch (IOException JavaDoc ex) {
271                 throw new JMSException JavaDoc(ex.getMessage());
272             }
273             throw new MessageFormatException JavaDoc(e.getMessage());
274         } catch (NumberFormatException JavaDoc e) {
275             try {
276                 dis.reset();
277             } catch (IOException JavaDoc ex) {
278                 throw new JMSException JavaDoc(ex.getMessage());
279             }
280             throw new JMSException JavaDoc(e.getMessage());
281         }
282     }
283
284     /**
285      * Reads a <code>float</code> from the stream message.
286      *
287      * @return a <code>float</code> value from the stream message
288      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
289      * internal error.
290      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
291      * @throws javax.jms.MessageFormatException
292      * if this type conversion is invalid.
293      * @throws javax.jms.MessageNotReadableException
294      * if the message is in write-only mode.
295      * @see javax.jms.StreamMessage#readFloat()
296      */

297     public float readFloat() throws JMSException JavaDoc {
298         prepareRead();
299         try {
300             return Conversions.getFloat(readNext());
301         } catch (MessageFormatException JavaDoc e) {
302             try {
303                 dis.reset();
304             } catch (IOException JavaDoc ex) {
305                 throw new JMSException JavaDoc(ex.getMessage());
306             }
307             throw new MessageFormatException JavaDoc(e.getMessage());
308         } catch (NullPointerException JavaDoc e) {
309             try {
310                 dis.reset();
311             } catch (IOException JavaDoc ex) {
312                 throw new JMSException JavaDoc(ex.getMessage());
313             }
314             throw new JMSException JavaDoc(e.getMessage());
315         } catch (NumberFormatException JavaDoc e) {
316             try {
317                 dis.reset();
318             } catch (IOException JavaDoc ex) {
319                 throw new JMSException JavaDoc(ex.getMessage());
320             }
321             throw new JMSException JavaDoc(e.getMessage());
322         }
323     }
324
325     /**
326      * Reads a <code>double</code> from the stream message.
327      *
328      * @return a <code>double</code> value from the stream message
329      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
330      * internal error.
331      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
332      * @throws javax.jms.MessageFormatException
333      * if this type conversion is invalid.
334      * @throws javax.jms.MessageNotReadableException
335      * if the message is in write-only mode.
336      * @see javax.jms.StreamMessage#readDouble()
337      */

338     public double readDouble() throws JMSException JavaDoc {
339         prepareRead();
340         try {
341             return Conversions.getDouble(readNext());
342         } catch (MessageFormatException JavaDoc e) {
343             try {
344                 dis.reset();
345             } catch (IOException JavaDoc ex) {
346                 throw new JMSException JavaDoc(ex.getMessage());
347             }
348             throw new MessageFormatException JavaDoc(e.getMessage());
349         } catch (NullPointerException JavaDoc e) {
350             try {
351                 dis.reset();
352             } catch (IOException JavaDoc ex) {
353                 throw new JMSException JavaDoc(ex.getMessage());
354             }
355             throw new JMSException JavaDoc(e.getMessage());
356         } catch (NumberFormatException JavaDoc e) {
357             try {
358                 dis.reset();
359             } catch (IOException JavaDoc ex) {
360                 throw new JMSException JavaDoc(ex.getMessage());
361             }
362             throw new JMSException JavaDoc(e.getMessage());
363         }
364     }
365
366     /**
367      * Reads a <CODE>String</CODE> from the stream message.
368      *
369      * @return a Unicode string from the stream message
370      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
371      * internal error.
372      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
373      * @throws javax.jms.MessageFormatException
374      * if this type conversion is invalid.
375      * @throws javax.jms.MessageNotReadableException
376      * if the message is in write-only mode.
377      * @see javax.jms.StreamMessage#readString()
378      */

379     public String JavaDoc readString() throws JMSException JavaDoc {
380         prepareRead();
381         try {
382             return Conversions.getString(readNext());
383         } catch (MessageFormatException JavaDoc e) {
384             try {
385                 dis.reset();
386             } catch (IOException JavaDoc ex) {
387                 throw new JMSException JavaDoc(ex.getMessage());
388             }
389             throw new MessageFormatException JavaDoc(e.getMessage());
390         }
391     }
392
393     /**
394      * Reads a byte array field from the stream message into the specified
395      * <CODE>byte[]</CODE> object (the read buffer).
396      * <p/>
397      * <P>
398      * To read the field value, <CODE>readBytes</CODE> should be successively
399      * called until it returns a value less than the length of the read buffer.
400      * The value of the bytes in the buffer following the last byte read is
401      * undefined.
402      * <p/>
403      * <P>
404      * If <CODE>readBytes</CODE> returns a value equal to the length of the
405      * buffer, a subsequent <CODE>readBytes</CODE> call must be made. If
406      * there are no more bytes to be read, this call returns -1.
407      * <p/>
408      * <P>
409      * If the byte array field value is null, <CODE>readBytes</CODE> returns
410      * -1.
411      * <p/>
412      * <P>
413      * If the byte array field value is empty, <CODE>readBytes</CODE> returns 0.
414      * <p/>
415      * <P>
416      * Once the first <CODE>readBytes</CODE> call on a <CODE>byte[]</CODE>
417      * field value has been made, the full value of the field must be read
418      * before it is valid to read the next field. An attempt to read the next
419      * field before that has been done will throw a <CODE>
420      * MessageFormatException</CODE>.
421      * <p/>
422      * <P>
423      * To read the byte field value into a new <CODE>byte[]</CODE> object,
424      * use the <CODE>readObject</CODE> method.
425      *
426      * @param value the buffer into which the data is read
427      * @return the total number of bytes read into the buffer, or -1 if there
428      * is no more data because the end of the byte field has been
429      * reached
430      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
431      * internal error.
432      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
433      * @throws javax.jms.MessageFormatException
434      * if this type conversion is invalid.
435      * @throws javax.jms.MessageNotReadableException
436      * if the message is in write-only mode.
437      * @see #readObject()
438      * @see javax.jms.StreamMessage#readBytes(byte[])
439      */

440     public int readBytes(byte[] value) throws JMSException JavaDoc {
441         if (isBodyModifiable()) {
442             throw new MessageNotReadableException JavaDoc("StreamMessage write_only");
443         }
444         getInputStream();
445         int read = 0; // the number of bytes read
446
if (readBytes == 0) {
447             try {
448                 dis.mark(buffer.length - dis.available());
449                 byte type = (byte) (dis.readByte() & 0x0F);
450                 if (type == TYPE_NULL) {
451                     return -1;
452                 } else if (type != TYPE_BYTE_ARRAY) {
453                     dis.reset();
454                     if (type < TYPE_NAMES.length) {
455                         throw new MessageFormatException JavaDoc("Expected type="
456                                 + TYPE_NAMES[TYPE_BYTE_ARRAY]
457                                 + ", but got type="
458                                 + TYPE_NAMES[type]);
459                     } else {
460                         throw new MessageFormatException JavaDoc("StreamMessage corrupted");
461                     }
462                 }
463             } catch (IOException JavaDoc e) {
464                 throw new JMSException JavaDoc(e.getMessage());
465             }
466             try {
467                 byteArrayLength = dis.readInt();
468             } catch (IOException JavaDoc e) {
469                 throw new JMSException JavaDoc(e.getMessage());
470             }
471         }
472         if (byteArrayLength == 0) {
473             if (readBytes != 0) { // not the first invocation
474
read = -1;
475             }
476             readBytes = 0; // finished reading the byte array
477
} else {
478             ++readBytes;
479             try {
480                 if (value.length <= byteArrayLength) {
481                     read = value.length;
482                     dis.readFully(value);
483                     byteArrayLength -= value.length;
484                 } else {
485                     read = byteArrayLength;
486                     dis.readFully(value, 0, byteArrayLength);
487                     byteArrayLength = 0;
488                 }
489             } catch (IOException JavaDoc e) {
490                 throw new JMSException JavaDoc(e.getMessage());
491             }
492         }
493         return read;
494     }
495
496     /**
497      * Reads an object from the stream message.
498      * <p/>
499      * <P>
500      * This method can be used to return, in objectified format, an object in
501      * the Java programming language ("Java object") that has been written to
502      * the stream with the equivalent <CODE>writeObject</CODE> method call,
503      * or its equivalent primitive <CODE>write <I>type</I></CODE> method.
504      * <p/>
505      * <P>
506      * Note that byte values are returned as <CODE>byte[]</CODE>, not <CODE>
507      * Byte[]</CODE>.
508      * <p/>
509      * <P>
510      * An attempt to call <CODE>readObject</CODE> to read a byte field value
511      * into a new <CODE>byte[]</CODE> object before the full value of the
512      * byte field has been read will throw a <CODE>MessageFormatException
513      * </CODE>.
514      *
515      * @return a Java object from the stream message, in objectified format
516      * (for example, if the object was written as an <CODE>int</CODE>,
517      * an <CODE>Integer</CODE> is returned)
518      * @throws javax.jms.JMSException if the JMS provider fails to read the message due to some
519      * internal error.
520      * @throws javax.jms.MessageEOFException if unexpected end of message stream has been reached.
521      * @throws javax.jms.MessageFormatException
522      * if this type conversion is invalid.
523      * @throws javax.jms.MessageNotReadableException
524      * if the message is in write-only mode.
525      * @see #readBytes(byte[] value)
526      * @see javax.jms.StreamMessage#readObject()
527      */

528     public Object JavaDoc readObject() throws JMSException JavaDoc {
529         prepareRead();
530         try {
531             return readNext();
532         } catch (MessageFormatException JavaDoc e) {
533             try {
534                 dis.reset();
535             } catch (IOException JavaDoc ex) {
536                 throw new JMSException JavaDoc(ex.getMessage());
537             }
538             throw new MessageFormatException JavaDoc(e.getMessage());
539         }
540     }
541
542     /**
543      * Writes a <code>boolean</code> to the stream message. The value <code>true</code>
544      * is written as the value <code>(byte)1</code>; the value <code>false</code>
545      * is written as the value <code>(byte)0</code>.
546      *
547      * @param value the <code>boolean</code> value to be written
548      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
549      * some internal error.
550      * @throws javax.jms.MessageNotWriteableException
551      * if the message is in read-only mode.
552      * @see javax.jms.StreamMessage#writeBoolean(boolean)
553      */

554     public void writeBoolean(boolean value) throws JMSException JavaDoc {
555         if (!isBodyModifiable()) {
556             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
557         }
558
559         try {
560             dos.writeByte((int) TYPE_BOOLEAN);
561             dos.writeBoolean(value);
562         } catch (IOException JavaDoc e) {
563             throw new JMSException JavaDoc(e.getMessage());
564         }
565     }
566
567     /**
568      * Writes a <code>byte</code> to the stream message.
569      *
570      * @param value the <code>byte</code> value to be written
571      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
572      * some internal error.
573      * @throws javax.jms.MessageNotWriteableException
574      * if the message is in read-only mode.
575      * @see javax.jms.StreamMessage#writeByte(byte)
576      */

577     public void writeByte(byte value) throws JMSException JavaDoc {
578         if (!isBodyModifiable()) {
579             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
580         }
581
582         try {
583             dos.writeByte((int) TYPE_BYTE);
584             dos.writeByte((int) value);
585         } catch (IOException JavaDoc e) {
586             throw new JMSException JavaDoc(e.getMessage());
587         }
588     }
589
590     /**
591      * Writes a <code>short</code> to the stream message.
592      *
593      * @param value the <code>short</code> value to be written
594      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
595      * some internal error.
596      * @throws javax.jms.MessageNotWriteableException
597      * if the message is in read-only mode.
598      * @see javax.jms.StreamMessage#writeShort(short)
599      */

600     public void writeShort(short value) throws JMSException JavaDoc {
601         if (!isBodyModifiable()) {
602             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
603         }
604
605         try {
606             dos.writeByte((int) TYPE_SHORT);
607             dos.writeShort((int) value);
608         } catch (IOException JavaDoc e) {
609             throw new JMSException JavaDoc(e.getMessage());
610         }
611     }
612
613     /**
614      * Writes a <code>char</code> to the stream message.
615      *
616      * @param value the <code>char</code> value to be written
617      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
618      * some internal error.
619      * @throws javax.jms.MessageNotWriteableException
620      * if the message is in read-only mode.
621      * @see javax.jms.StreamMessage#writeChar(char)
622      */

623     public void writeChar(char value) throws JMSException JavaDoc {
624         if (!isBodyModifiable()) {
625             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
626         }
627
628         try {
629             dos.writeByte((int) TYPE_CHAR);
630             dos.writeChar((int) value);
631         } catch (IOException JavaDoc e) {
632             throw new JMSException JavaDoc(e.getMessage());
633         }
634     }
635
636     /**
637      * Writes an <code>int</code> to the stream message.
638      *
639      * @param value the <code>int</code> value to be written
640      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
641      * some internal error.
642      * @throws javax.jms.MessageNotWriteableException
643      * if the message is in read-only mode.
644      * @see javax.jms.StreamMessage#writeInt(int)
645      */

646     public void writeInt(int value) throws JMSException JavaDoc {
647         if (!isBodyModifiable()) {
648             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
649         }
650
651         try {
652             dos.writeByte((int) TYPE_INT);
653             dos.writeInt(value);
654         } catch (IOException JavaDoc e) {
655             throw new JMSException JavaDoc(e.getMessage());
656         }
657     }
658
659     /**
660      * Writes a <code>long</code> to the stream message.
661      *
662      * @param value the <code>long</code> value to be written
663      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
664      * some internal error.
665      * @throws javax.jms.MessageNotWriteableException
666      * if the message is in read-only mode.
667      * @see javax.jms.StreamMessage#writeLong(long)
668      */

669     public void writeLong(long value) throws JMSException JavaDoc {
670         if (!isBodyModifiable()) {
671             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
672         }
673
674         try {
675             dos.writeByte((int) TYPE_LONG);
676             dos.writeLong(value);
677         } catch (IOException JavaDoc e) {
678             throw new JMSException JavaDoc(e.getMessage());
679         }
680     }
681
682     /**
683      * Writes a <code>float</code> to the stream message.
684      *
685      * @param value the <code>float</code> value to be written
686      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
687      * some internal error.
688      * @throws javax.jms.MessageNotWriteableException
689      * if the message is in read-only mode.
690      * @see javax.jms.StreamMessage#writeFloat(float)
691      */

692     public void writeFloat(float value) throws JMSException JavaDoc {
693         if (!isBodyModifiable()) {
694             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
695         }
696
697         try {
698             dos.writeByte((int) TYPE_FLOAT);
699             dos.writeFloat(value);
700         } catch (IOException JavaDoc e) {
701             throw new JMSException JavaDoc(e.getMessage());
702         }
703     }
704
705     /**
706      * Writes a <code>double</code> to the stream message.
707      *
708      * @param value the <code>double</code> value to be written
709      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
710      * some internal error.
711      * @throws javax.jms.MessageNotWriteableException
712      * if the message is in read-only mode.
713      * @see javax.jms.StreamMessage#writeDouble(double)
714      */

715     public void writeDouble(double value) throws JMSException JavaDoc {
716         if (!isBodyModifiable()) {
717             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
718         }
719
720         try {
721             dos.writeByte((int) TYPE_DOUBLE);
722             dos.writeDouble(value);
723         } catch (IOException JavaDoc e) {
724             throw new JMSException JavaDoc(e.getMessage());
725         }
726     }
727
728     /**
729      * Writes a <code>String</code> to the stream message.
730      *
731      * @param value the <code>String</code> value to be written
732      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
733      * some internal error.
734      * @throws javax.jms.MessageNotWriteableException
735      * if the message is in read-only mode.
736      * @see javax.jms.StreamMessage#writeString(String)
737      */

738     public void writeString(String JavaDoc value) throws JMSException JavaDoc {
739         if (!isBodyModifiable()) {
740             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
741         }
742
743         try {
744             if (value != null) {
745                 dos.writeByte((int) TYPE_STRING);
746                 dos.writeUTF(value);
747             } else {
748                 throw new JMSException JavaDoc("Value is null");
749             }
750         } catch (IOException JavaDoc e) {
751             throw new JMSException JavaDoc(e.getMessage());
752         }
753     }
754
755     /**
756      * Writes a byte array field to the stream message.
757      * <p/>
758      * <P>
759      * The byte array <code>value</code> is written to the message as a byte
760      * array field. Consecutively written byte array fields are treated as two
761      * distinct fields when the fields are read.
762      *
763      * @param value the byte array value to be written
764      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
765      * some internal error.
766      * @throws javax.jms.MessageNotWriteableException
767      * if the message is in read-only mode.
768      * @see javax.jms.StreamMessage#writeBytes(byte[])
769      */

770     public void writeBytes(byte[] value) throws JMSException JavaDoc {
771         writeBytes(value, 0, value.length);
772     }
773
774     /**
775      * Writes a portion of a byte array as a byte array field to the stream
776      * message.
777      * <p/>
778      * <P>
779      * The a portion of the byte array <code>value</code> is written to the
780      * message as a byte array field. Consecutively written byte array fields
781      * are treated as two distinct fields when the fields are read.
782      *
783      * @param value the byte array value to be written
784      * @param offset the initial offset within the byte array
785      * @param length the number of bytes to use
786      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
787      * some internal error.
788      * @throws javax.jms.MessageNotWriteableException
789      * if the message is in read-only mode.
790      * @see javax.jms.StreamMessage#writeBytes(byte[], int, int)
791      */

792     public void writeBytes(byte[] value, int offset, int length)
793             throws JMSException JavaDoc {
794         if (!isBodyModifiable()) {
795             throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
796         }
797
798         try {
799             if (value != null) {
800                 dos.writeByte((int) TYPE_BYTE_ARRAY);
801                 dos.writeInt(length);
802                 dos.write(value, offset, length);
803             } else {
804                 throw new JMSException JavaDoc("Value is null");
805             }
806         } catch (IOException JavaDoc e) {
807             throw new JMSException JavaDoc(e.getMessage());
808         }
809     }
810
811     /**
812      * Writes an object to the stream message.
813      * <p/>
814      * <P>
815      * This method works only for the objectified primitive object types (
816      * <code>Integer</code>,<code>Double</code>,<code>Long</code>
817      * &nbsp;...), <code>String</code> objects, and byte arrays.
818      *
819      * @param value the Java object to be written
820      * @throws javax.jms.JMSException if the JMS provider fails to write the message due to
821      * some internal error.
822      * @throws javax.jms.MessageFormatException
823      * if the object is invalid.
824      * @throws javax.jms.MessageNotWriteableException
825      * if the message is in read-only mode.
826      * @see javax.jms.StreamMessage#writeObject(Object)
827      */

828     public void writeObject(Object JavaDoc value) throws JMSException JavaDoc {
829         if (value == null) {
830             try {
831                 if (!isBodyModifiable()) {
832                     throw new MessageNotWriteableException JavaDoc("StreamMessage read_only");
833                 }
834                 getOutputStream();
835                 dos.writeByte(TYPE_NULL);
836             } catch (IOException JavaDoc e) {
837                 throw new JMSException JavaDoc(e.getMessage());
838             }
839         } else if (value instanceof Boolean JavaDoc) {
840             writeBoolean(((Boolean JavaDoc) value).booleanValue());
841         } else if (value instanceof Byte JavaDoc) {
842             writeByte(((Byte JavaDoc) value).byteValue());
843         } else if (value instanceof byte[]) {
844             writeBytes((byte[]) value);
845         } else if (value instanceof Short JavaDoc) {
846             writeShort(((Short JavaDoc) value).shortValue());
847         } else if (value instanceof Character JavaDoc) {
848             writeChar(((Character JavaDoc) value).charValue());
849         } else if (value instanceof Integer JavaDoc) {
850             writeInt(((Integer JavaDoc) value).intValue());
851         } else if (value instanceof Long JavaDoc) {
852             writeLong(((Long JavaDoc) value).longValue());
853         } else if (value instanceof Float JavaDoc) {
854             writeFloat(((Float JavaDoc) value).floatValue());
855         } else if (value instanceof Double JavaDoc) {
856             writeDouble(((Double JavaDoc) value).doubleValue());
857         } else if (value instanceof String JavaDoc) {
858             writeString((String JavaDoc) value);
859         } else {
860             throw new MessageFormatException JavaDoc("StreamMessag invalid_type");
861         }
862     }
863
864     /**
865      * @see javax.jms.StreamMessage#reset()
866      */

867     public void reset() throws JMSException JavaDoc {
868         try {
869             if (!isBodyModifiable()) {
870                 setBodyModifiable(true);
871                 if (dos != null) {
872                     dos.flush();
873                     buffer = baos.toByteArray();
874                     baos = null;
875                     dos.close();
876                     dos = null;
877                 }
878             } else {
879                 if (dis != null) {
880                     bais = null;
881                     dis.close();
882                     dis = null;
883                 }
884             }
885             readBytes = 0;
886         } catch (IOException JavaDoc e) {
887             if (e instanceof EOFException JavaDoc) {
888                 throw new MessageEOFException JavaDoc(e.getMessage());
889             } else {
890                 throw new JMSException JavaDoc(e.getMessage());
891             }
892         }
893     }
894
895     /**
896      * Sets the object reference in the message to null and sets the message to
897      * modifiable.
898      *
899      * @throws javax.jms.JMSException
900      */

901     public void clearBody() throws JMSException JavaDoc {
902         super.clearBody();
903         buffer = new byte[0];
904         baos = new ByteArrayOutputStream JavaDoc();
905         dos = new DataOutputStream JavaDoc(baos);
906         bais = null;
907         dis = null;
908     }
909
910     /**
911      * Read the next object from the stream and verify that it is one of the
912      * specified data types.
913      *
914      * @return The read object in its original type format
915      * @throws javax.jms.MessageEOFException if the end of the stream is reached.
916      * @throws javax.jms.MessageFormatException
917      * if the type conversion is invalid or the client is
918      * currently reading a bytes field (and hasn't completed).
919      * @throws javax.jms.MessageNotReadableException
920      * if the message is write-only.
921      * @throws javax.jms.JMSException if an IO error occurs with the underlying message.
922      * @throws NullPointerException if value is null.
923      */

924     private Object JavaDoc readNext() throws JMSException JavaDoc {
925         if (readBytes != 0) {
926             throw new MessageFormatException JavaDoc("Must finish reading byte array before reading next field");
927         }
928
929         byte type = 0;
930         try {
931             type = dis.readByte();
932         } catch (IOException JavaDoc e) {
933             throw new JMSException JavaDoc(e.getMessage());
934         }
935         if ((type & 0x0F) > TYPE_NAMES.length) {
936             throw new JMSException JavaDoc("StreamMessage corrupted");
937         }
938         Object JavaDoc result = null;
939
940         try {
941             switch (type & 0x0F) {
942                 case TYPE_BOOLEAN:
943                     boolean value = ((type & 0xF0) != 0) ? true : false;
944                     result = new Boolean JavaDoc(value);
945                     break;
946                 case TYPE_BYTE:
947                     result = new Byte JavaDoc(dis.readByte());
948                     break;
949                 case TYPE_BYTE_ARRAY:
950                     int length = dis.readInt();
951                     byte[] bytes = new byte[length];
952                     dis.readFully(bytes);
953                     result = bytes;
954                     break;
955                 case TYPE_SHORT:
956                     result = new Short JavaDoc(dis.readShort());
957                     break;
958                 case TYPE_CHAR:
959                     result = new Character JavaDoc(dis.readChar());
960                     break;
961                 case TYPE_INT:
962                     result = new Integer JavaDoc(dis.readInt());
963                     break;
964                 case TYPE_LONG:
965                     result = new Long JavaDoc(dis.readLong());
966                     break;
967                 case TYPE_FLOAT:
968                     result = new Float JavaDoc(dis.readFloat());
969                     break;
970                 case TYPE_DOUBLE:
971                     result = new Double JavaDoc(dis.readDouble());
972                     break;
973                 case TYPE_STRING:
974                     result = dis.readUTF();
975                     break;
976             }
977         } catch (IOException JavaDoc e) {
978             throw new JMSException JavaDoc(e.getMessage());
979         }
980         return result;
981     }
982
983     private void prepareRead() throws JMSException JavaDoc {
984         if (isBodyModifiable()) {
985             throw new MessageNotReadableException JavaDoc("StreamMessage write_only");
986         }
987         getInputStream();
988         try {
989             dis.mark(buffer.length - dis.available());
990         } catch (IOException JavaDoc e) {
991             throw new JMSException JavaDoc(e.getMessage());
992         }
993     }
994
995     /**
996      * Initialise the input stream if it hasn't been intialised
997      *
998      * @return the input stream
999      */

1000    private DataInputStream JavaDoc getInputStream() {
1001        if (dis == null) {
1002            bais = new ByteArrayInputStream JavaDoc(buffer);
1003            dis = new DataInputStream JavaDoc(bais);
1004        }
1005        return dis;
1006    }
1007
1008    /**
1009     * Initialise the output stream if it hasn't been intialised
1010     *
1011     * @return the output stream
1012     * @throws java.io.IOException if the output stream can't be created
1013     */

1014    private DataOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
1015        if (dos == null) {
1016            baos = new ByteArrayOutputStream JavaDoc();
1017            dos = new DataOutputStream JavaDoc(baos);
1018            dos.write(buffer);
1019        }
1020        return dos;
1021    }
1022
1023}
1024
Popular Tags