KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > message > JmsStreamMessage


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.message;
22
23 import java.io.Serializable JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.EOFException JavaDoc;
26
27 import java.io.DataInput JavaDoc;
28 import java.io.DataOutput JavaDoc;
29
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.ByteArrayInputStream JavaDoc;
32 import java.io.DataOutputStream JavaDoc;
33 import java.io.DataInputStream JavaDoc;
34
35 import javax.jms.StreamMessage JavaDoc;
36 import javax.jms.JMSException JavaDoc;
37 import javax.jms.MessageFormatException JavaDoc;
38 import javax.jms.MessageNotReadableException JavaDoc;
39 import javax.jms.MessageNotWriteableException JavaDoc;
40 import javax.jms.MessageEOFException JavaDoc;
41
42 /**
43  * Implementation of the JMS interface StreamMessage.
44  *
45  * @see javax.jms.StreamMessage
46  * @author Dan Greff
47  */

48 public final class JmsStreamMessage extends JmsMessage
49   implements StreamMessage JavaDoc, java.io.Externalizable JavaDoc
50 {
51
52   private byte [] streamMsgArray;
53   private ByteArrayOutputStream JavaDoc byteOutStream;
54   private DataOutputStream JavaDoc dataOutStream;
55   private ByteArrayInputStream JavaDoc byteInStream;
56   private DataInputStream JavaDoc dataInStream;
57
58
59
60   protected static final byte BOOL_TYPE = 0;
61   protected static final byte BYTE_TYPE = 1;
62   protected static final byte SHORT_TYPE = 2;
63   protected static final byte INT_TYPE = 3;
64   protected static final byte LONG_TYPE = 4;
65   protected static final byte FLOAT_TYPE = 5;
66   protected static final byte DOUBLE_TYPE = 6;
67   protected static final byte CHAR_TYPE = 7;
68   protected static final byte BYTES_TYPE = 8;
69   protected static final byte UTF_TYPE = 9;
70   protected static final byte UNBOUND_TYPE = 20;
71
72   protected byte dataType = UNBOUND_TYPE;
73
74
75     /////////////////////////////////////////////////////////////////////////
76
// Constructors //
77
/////////////////////////////////////////////////////////////////////////
78

79   public JmsStreamMessage()
80   {
81     super();
82   }
83
84   public JmsStreamMessage(String JavaDoc name)
85   {
86     super(name);
87   }
88
89   public JmsStreamMessage(byte [] msg, int offset, int length)
90     throws IOException JavaDoc
91   {
92     super(msg, offset, length);
93   }
94     ///////////////////////////////////////////////////////////////////////////
95
// Public Methods //
96
///////////////////////////////////////////////////////////////////////////
97

98   /**
99    *
100    *
101    */

102   public boolean readBoolean() throws JMSException JavaDoc
103   {
104    
105     boolean retval;
106     if (!this.readOnly){
107       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
108       throw jmse;
109     }
110
111     try {
112       switch(this.nextType())
113       {
114       case(BOOL_TYPE):
115         retval = dataInStream.readBoolean();
116         this.popType();
117         return retval;
118       case(UTF_TYPE):
119         String JavaDoc s = dataInStream.readUTF();
120         retval = Boolean.valueOf(s).booleanValue();
121         this.popType();
122         return retval;
123       default:
124         throw new JMSException JavaDoc("Incorrect Type Called");
125       }
126       
127
128
129     } catch (EOFException JavaDoc e) {
130       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
131       jmse.setLinkedException(e);
132       throw jmse;
133     } catch (IOException JavaDoc e) {
134       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
135       jmse.setLinkedException(e);
136       throw jmse;
137     }
138     
139             
140   }
141
142   public byte readByte() throws JMSException JavaDoc
143   {
144     
145     byte retval;
146     if (!this.readOnly){
147       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
148       throw jmse;
149     }
150     
151     try {
152         
153         switch(this.nextType())
154         {
155         case(BYTE_TYPE):
156             retval = dataInStream.readByte();
157             this.popType();
158             return retval;
159         case(UTF_TYPE):
160             String JavaDoc s = dataInStream.readUTF();
161             retval = Byte.valueOf(s).byteValue();
162             this.popType();
163             return retval;
164         default:
165             throw new JMSException JavaDoc("Incorrect Type Called");
166         }
167          
168
169     } catch (EOFException JavaDoc e) {
170       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
171       jmse.setLinkedException(e);
172       throw jmse;
173     } catch (IOException JavaDoc e) {
174       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
175       jmse.setLinkedException(e);
176       throw jmse;
177     }
178           
179   }
180
181   public int readBytes(byte [] value) throws JMSException JavaDoc
182   {
183     int retval;
184      if (!this.readOnly){
185       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
186       throw jmse;
187     }
188
189     try {
190       if(this.nextType() != BYTES_TYPE)
191         throw new JMSException JavaDoc("Incorrect Type Called");
192       else {
193         retval = dataInStream.read(value);
194         this.popType();
195       }
196       return retval;
197     } catch (IOException JavaDoc e) {
198       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
199       jmse.setLinkedException(e);
200       throw jmse;
201     }
202        
203   }
204
205   public short readShort() throws JMSException JavaDoc
206   {
207     short retval;
208     
209      if (!this.readOnly){
210       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
211       throw jmse;
212     }
213
214     try {
215         switch(this.nextType())
216         {
217         case(BYTE_TYPE):
218             retval = dataInStream.readByte();
219             this.popType();
220             return retval;
221         case(SHORT_TYPE):
222             retval = dataInStream.readShort();
223             this.popType();
224             return retval;
225         case(UTF_TYPE):
226             String JavaDoc s = dataInStream.readUTF();
227             retval = Short.valueOf(s).shortValue();
228             this.popType();
229             return retval;
230         default:
231             throw new JMSException JavaDoc("Incorrect Type Called");
232         }
233         
234
235     } catch (EOFException JavaDoc e) {
236       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
237       jmse.setLinkedException(e);
238       throw jmse;
239     } catch (IOException JavaDoc e) {
240       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
241       jmse.setLinkedException(e);
242       throw jmse;
243     }
244                   
245     
246   }
247
248   public char readChar() throws JMSException JavaDoc
249   {
250     char retval;
251     
252      if (!this.readOnly){
253       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
254       throw jmse;
255     }
256
257     try {
258       if(this.nextType() != CHAR_TYPE)
259         throw new JMSException JavaDoc("Incorrect Type Called");
260       else {
261         retval = dataInStream.readChar();
262         this.popType();
263       }
264       return retval;
265
266     } catch (EOFException JavaDoc e) {
267       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
268       jmse.setLinkedException(e);
269       throw jmse;
270     } catch (IOException JavaDoc e) {
271       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
272       jmse.setLinkedException(e);
273       throw jmse;
274     }
275            
276   }
277
278   public int readInt() throws JMSException JavaDoc
279   {
280     int retval;
281     
282      if (!this.readOnly){
283       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
284       throw jmse;
285     }
286
287     try {
288         switch(this.nextType())
289         {
290         case(BYTE_TYPE):
291             retval = dataInStream.readByte();
292             this.popType();
293             return retval;
294         case(SHORT_TYPE):
295             retval = dataInStream.readShort();
296             this.popType();
297             return retval;
298         case(INT_TYPE):
299             retval = dataInStream.readInt();
300             this.popType();
301             return retval;
302         case(UTF_TYPE):
303             String JavaDoc s = dataInStream.readUTF();
304             retval = Integer.valueOf(s).intValue();
305             this.popType();
306             return retval;
307         default:
308             throw new JMSException JavaDoc("Incorrect Type Called");
309         }
310         
311
312     } catch (EOFException JavaDoc e) {
313       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
314       jmse.setLinkedException(e);
315       throw jmse;
316     } catch (IOException JavaDoc e) {
317       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
318       jmse.setLinkedException(e);
319       throw jmse;
320     }
321            
322     
323   }
324
325   public long readLong() throws JMSException JavaDoc
326   {
327     long retval;
328      if (!this.readOnly){
329       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
330       throw jmse;
331     }
332
333     try {
334         switch(this.nextType())
335         {
336         case(BYTE_TYPE):
337             retval = dataInStream.readByte();
338             this.popType();
339             return retval;
340         case(SHORT_TYPE):
341             retval = dataInStream.readShort();
342             this.popType();
343             return retval;
344         case(INT_TYPE):
345             retval = dataInStream.readInt();
346             this.popType();
347             return retval;
348         case(LONG_TYPE):
349             retval = dataInStream.readLong();
350             this.popType();
351             return retval;
352         case(UTF_TYPE):
353             String JavaDoc s = dataInStream.readUTF();
354             retval = Long.valueOf(s).longValue();
355             this.popType();
356             return retval;
357         default:
358             throw new JMSException JavaDoc("Incorrect Type Called");
359         }
360         
361
362     } catch (EOFException JavaDoc e) {
363       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
364       jmse.setLinkedException(e);
365       throw jmse;
366     } catch (IOException JavaDoc e) {
367       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
368       jmse.setLinkedException(e);
369       throw jmse;
370     }
371           
372         
373   }
374
375   public float readFloat() throws JMSException JavaDoc
376   {
377     float retval;
378      if (!this.readOnly){
379       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
380       throw jmse;
381     }
382
383     try {
384         switch(this.nextType())
385         {
386         case(FLOAT_TYPE):
387             retval = dataInStream.readFloat();
388             this.popType();
389             return retval;
390         case(UTF_TYPE):
391             String JavaDoc s = dataInStream.readUTF();
392             retval = Float.valueOf(s).floatValue();
393             this.popType();
394             return retval;
395         default:
396             throw new JMSException JavaDoc("Incorrect Type Called");
397         }
398         
399
400     } catch (EOFException JavaDoc e) {
401       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
402       jmse.setLinkedException(e);
403       throw jmse;
404     } catch (IOException JavaDoc e) {
405       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
406       jmse.setLinkedException(e);
407       throw jmse;
408     }
409                    
410     
411   }
412
413   public double readDouble() throws JMSException JavaDoc
414   {
415     double retval;
416      if (!this.readOnly){
417       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
418       throw jmse;
419     }
420
421     try {
422         switch(this.nextType())
423         {
424         case(FLOAT_TYPE):
425             retval = dataInStream.readFloat();
426             this.popType();
427             return retval;
428         case(DOUBLE_TYPE):
429             retval = dataInStream.readDouble();
430             this.popType();
431             return retval;
432         case(UTF_TYPE):
433             String JavaDoc s = dataInStream.readUTF();
434             retval = Double.valueOf(s).doubleValue();
435             this.popType();
436             return retval;
437         default:
438             throw new JMSException JavaDoc("Incorrect Type Called");
439         }
440         
441
442     } catch (EOFException JavaDoc e) {
443       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
444       jmse.setLinkedException(e);
445       throw jmse;
446     } catch (IOException JavaDoc e) {
447       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
448       jmse.setLinkedException(e);
449       throw jmse;
450     }
451           
452    
453   }
454
455   public String JavaDoc readString() throws JMSException JavaDoc
456   {
457     String JavaDoc retval;
458      if (!this.readOnly){
459       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
460       throw jmse;
461     }
462
463     try {
464         switch(this.nextType())
465         {
466         case(BYTE_TYPE):
467             retval = new Byte JavaDoc(dataInStream.readByte()).toString();
468             this.popType();
469             return retval;
470         case(SHORT_TYPE):
471             retval = new Short JavaDoc(dataInStream.readShort()).toString();
472             this.popType();
473             return retval;
474         case(INT_TYPE):
475             retval = new Integer JavaDoc(dataInStream.readInt()).toString();
476             this.popType();
477             return retval;
478         case(LONG_TYPE):
479             retval = new Long JavaDoc(dataInStream.readLong()).toString();
480             this.popType();
481             return retval;
482         case(BOOL_TYPE):
483             retval = new Boolean JavaDoc(dataInStream.readBoolean()).toString();
484             this.popType();
485             return retval;
486         case(FLOAT_TYPE):
487             retval = new Float JavaDoc(dataInStream.readFloat()).toString();
488             this.popType();
489             return retval;
490         case(DOUBLE_TYPE):
491             retval = new Double JavaDoc(dataInStream.readDouble()).toString();
492             this.popType();
493             return retval;
494         case(UTF_TYPE):
495             retval = dataInStream.readUTF();
496             this.popType();
497             return retval;
498         default:
499             throw new JMSException JavaDoc("Incorrect Type Called");
500         }
501         
502
503     } catch (EOFException JavaDoc e) {
504       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
505       jmse.setLinkedException(e);
506       throw jmse;
507     } catch (IOException JavaDoc e) {
508       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
509       jmse.setLinkedException(e);
510       throw jmse;
511     }
512             
513        
514   }
515
516   public Object JavaDoc readObject() throws JMSException JavaDoc
517   {
518     Object JavaDoc retval;
519      if (!this.readOnly){
520       JMSException JavaDoc jmse = new MessageNotReadableException JavaDoc("This Message is open for writing only");
521       throw jmse;
522     }
523
524     try {
525         switch(this.nextType())
526         {
527         case(BYTE_TYPE):
528             retval = new Byte JavaDoc(dataInStream.readByte());
529             this.popType();
530             return retval;
531         case(SHORT_TYPE):
532             retval = new Short JavaDoc(dataInStream.readShort());
533             this.popType();
534             return retval;
535         case(INT_TYPE):
536             retval = new Integer JavaDoc(dataInStream.readInt());
537             this.popType();
538             return retval;
539         case(LONG_TYPE):
540             retval = new Long JavaDoc(dataInStream.readLong());
541             this.popType();
542             return retval;
543         case(BOOL_TYPE):
544             retval = new Boolean JavaDoc(dataInStream.readBoolean());
545             this.popType();
546             return retval;
547         case(FLOAT_TYPE):
548             retval = new Float JavaDoc(dataInStream.readFloat());
549             this.popType();
550             return retval;
551         case(DOUBLE_TYPE):
552             retval = new Double JavaDoc(dataInStream.readDouble());
553             this.popType();
554             return retval;
555         case(UTF_TYPE):
556             retval = dataInStream.readUTF();
557             this.popType();
558             return retval;
559         default:
560             throw new JMSException JavaDoc("Incorrect Type Called");
561         }
562         
563
564     } catch (EOFException JavaDoc e) {
565       JMSException JavaDoc jmse = new MessageEOFException JavaDoc("End of Message Reached");
566       jmse.setLinkedException(e);
567       throw jmse;
568     } catch (IOException JavaDoc e) {
569       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to read from Stream");
570       jmse.setLinkedException(e);
571       throw jmse;
572     }
573        
574   }
575
576   public void writeBoolean(boolean value) throws JMSException JavaDoc
577   {
578     checkWrite();
579       
580     try {
581       dataOutStream.writeByte(BOOL_TYPE);
582       dataOutStream.writeBoolean(value);
583     } catch (IOException JavaDoc e) {
584       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
585       jmse.setLinkedException(e);
586       throw jmse;
587     }
588   }
589
590   public void writeByte(byte value) throws JMSException JavaDoc
591   {
592     checkWrite();
593
594     try {
595       dataOutStream.writeByte(BYTE_TYPE);
596       dataOutStream.writeByte(value);
597     } catch (IOException JavaDoc e) {
598       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
599       jmse.setLinkedException(e);
600       throw jmse;
601     }
602   }
603
604   public void writeBytes(byte[] value) throws JMSException JavaDoc
605   {
606     checkWrite();
607     
608     try {
609       dataOutStream.writeByte(BYTES_TYPE);
610       dataOutStream.write(value, 0, value.length);
611     } catch (IOException JavaDoc e) {
612       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
613       jmse.setLinkedException(e);
614       throw jmse;
615     }
616   }
617
618   public void writeBytes(byte[] value, int offset, int length)
619     throws JMSException JavaDoc
620   {
621     checkWrite();
622     
623     if (this.readOnly){
624       JMSException JavaDoc jmse = new MessageNotWriteableException JavaDoc("This Message is readOnly");
625       throw jmse;
626     }
627
628     try {
629       dataOutStream.writeByte(BYTES_TYPE);
630       dataOutStream.write(value, offset, length);
631     } catch (IOException JavaDoc e) {
632       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
633       jmse.setLinkedException(e);
634       throw jmse;
635     }
636   }
637
638   public void writeShort(short value) throws JMSException JavaDoc
639   {
640     checkWrite();
641     
642     try {
643       dataOutStream.writeByte(SHORT_TYPE);
644       dataOutStream.writeShort(value);
645     } catch (IOException JavaDoc e) {
646       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
647       jmse.setLinkedException(e);
648       throw jmse;
649     }
650   }
651
652   public void writeChar(char value) throws JMSException JavaDoc
653   {
654     checkWrite();
655     
656     try {
657       dataOutStream.writeByte(CHAR_TYPE);
658       dataOutStream.writeChar(value);
659     } catch (IOException JavaDoc e) {
660       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
661       jmse.setLinkedException(e);
662       throw jmse;
663     }
664   }
665
666   public void writeInt(int value) throws JMSException JavaDoc
667   {
668     checkWrite();
669     
670     try {
671       dataOutStream.writeByte(INT_TYPE);
672       dataOutStream.writeInt(value);
673     } catch (IOException JavaDoc e) {
674       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
675       jmse.setLinkedException(e);
676       throw jmse;
677     }
678   }
679
680   public void writeLong(long value) throws JMSException JavaDoc
681   {
682     checkWrite();
683
684     try {
685       dataOutStream.writeByte(LONG_TYPE);
686       dataOutStream.writeLong(value);
687     } catch (IOException JavaDoc e) {
688       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
689       jmse.setLinkedException(e);
690       throw jmse;
691     }
692   }
693
694   public void writeFloat(float value) throws JMSException JavaDoc
695   {
696     checkWrite();
697     
698     try {
699       dataOutStream.writeByte(FLOAT_TYPE);
700       dataOutStream.writeFloat(value);
701     } catch (IOException JavaDoc e) {
702       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
703       jmse.setLinkedException(e);
704       throw jmse;
705     }
706   }
707
708   public void writeDouble(double value) throws JMSException JavaDoc
709   {
710     checkWrite();
711
712     try {
713       dataOutStream.writeByte(DOUBLE_TYPE);
714       dataOutStream.writeDouble(value);
715     } catch (IOException JavaDoc e) {
716       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
717       jmse.setLinkedException(e);
718       throw jmse;
719     }
720   }
721
722   public void writeString(String JavaDoc value) throws JMSException JavaDoc
723   {
724     checkWrite();
725     
726     try {
727       dataOutStream.writeByte(UTF_TYPE);
728       dataOutStream.writeUTF(value);
729     } catch (IOException JavaDoc e) {
730       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
731       jmse.setLinkedException(e);
732       throw jmse;
733     }
734   }
735
736   public void writeObject(Object JavaDoc value) throws JMSException JavaDoc
737   {
738     checkWrite();
739     
740     try {
741         if (value instanceof String JavaDoc) {
742             writeString((String JavaDoc)value);
743         } else if (value instanceof Boolean JavaDoc) {
744             writeBoolean(( (Boolean JavaDoc) value).booleanValue());
745         } else if (value instanceof Byte JavaDoc) {
746             writeByte(( (Byte JavaDoc) value).byteValue());
747         } else if (value instanceof Short JavaDoc) {
748             writeShort(( (Short JavaDoc) value).shortValue());
749         } else if (value instanceof Integer JavaDoc) {
750             writeInt(( (Integer JavaDoc) value).intValue());
751         } else if (value instanceof Long JavaDoc) {
752             writeLong(( (Long JavaDoc) value).longValue());
753         } else if (value instanceof Float JavaDoc) {
754             writeFloat(( (Float JavaDoc) value).floatValue());
755         } else if (value instanceof Double JavaDoc) {
756             writeDouble(( (Double JavaDoc) value).doubleValue());
757         }
758         else {
759             throw new MessageFormatException JavaDoc("Object must be a primitive type");
760         }
761     } catch (MessageFormatException JavaDoc e) {
762       JMSException JavaDoc jmse = new MessageFormatException JavaDoc("Unable to write to Stream");
763       jmse.setLinkedException(e);
764       throw jmse;
765     }
766   }
767
768  
769   public void reset() throws JMSException JavaDoc
770   {
771     this.setReadOnly();
772   }
773
774   
775   /**
776    *
777    */

778   public void unmarshal(DataInput JavaDoc in) throws IOException JavaDoc
779   {
780     //
781
// Read in the message header and properties
782
//
783
super.unmarshal(in);
784     
785     int arraySize = in.readInt();
786     if (arraySize > 0) {
787       this.streamMsgArray = new byte[arraySize];
788       in.readFully(streamMsgArray, 0, arraySize);
789       convertByteArrayToData(streamMsgArray);
790     }
791
792      this.setReadOnly();
793   }
794   
795   /**
796    *
797    */

798   public void marshal(DataOutput JavaDoc out) throws IOException JavaDoc
799   {
800     //
801
// Write out the message header and properties
802
//
803
super.marshal(out);
804
805     this.streamMsgArray = this.convertDataToByteArray();
806     
807     if (streamMsgArray == null || streamMsgArray.length == 0)
808       out.writeInt(0);
809     else {
810       out.writeInt(streamMsgArray.length);
811       out.write(streamMsgArray);
812     }
813   }
814
815   public void clearBody() throws JMSException JavaDoc
816   {
817     checkWrite();
818     super.clearBody();
819     this.streamMsgArray = null;
820   }
821
822
823     /////////////////////////////////////////////////////////////////////////
824
// Package Methods //
825
/////////////////////////////////////////////////////////////////////////
826

827
828   /*
829    * @return byte identifier for this message type. Specifically
830    * MessageConverter#OBJECT_MESSAGE
831    */

832   byte getMarshalingID()
833   {
834     return MessageEncoder.STREAM_MESSAGE;
835   }
836   
837   
838     ///////////////////////////////////////////////////////////////////////////
839
// Protected Methods //
840
///////////////////////////////////////////////////////////////////////////
841

842   protected byte nextType() throws JMSException JavaDoc, IOException JavaDoc
843   {
844     byte retval;
845     
846     if(dataType == UNBOUND_TYPE) {
847       if (dataInStream == null)
848         throw new MessageEOFException JavaDoc("End of Message Reached");
849       retval = dataInStream.readByte();
850     }
851     else
852       retval = dataType;
853     
854     return retval;
855     
856   }
857
858   protected void popType() throws IOException JavaDoc
859   {
860     dataType = dataInStream.readByte();
861   }
862
863   protected void finalize() throws Throwable JavaDoc
864   {
865     try{
866         dataInStream.close();
867         byteInStream.close();
868         byteOutStream.close();
869         dataOutStream.close();
870     }catch(IOException JavaDoc e){
871     //Eat it
872
}
873   }
874     /////////////////////////////////////////////////////////////////////////
875
// Private Methods //
876
/////////////////////////////////////////////////////////////////////////
877

878   protected void checkWrite() throws JMSException JavaDoc
879   {
880     super.checkWrite();
881     
882     if (dataOutStream == null) {
883       byteOutStream = new ByteArrayOutputStream JavaDoc();
884       dataOutStream = new DataOutputStream JavaDoc(byteOutStream);
885     }
886   }
887   
888   
889   private byte[] convertDataToByteArray()
890     throws IOException JavaDoc
891   {
892     if (dataOutStream == null)
893       return null;
894       
895     dataOutStream.close();
896     return byteOutStream.toByteArray();
897   }
898
899   
900   /*
901    * Converts a byte array representing an object back to its
902    * object form.
903    */

904   private void convertByteArrayToData(byte[] byteArray)
905     throws IOException JavaDoc
906   {
907     if (byteArray.length != 0){
908         byteInStream = new ByteArrayInputStream JavaDoc(byteArray);
909         dataInStream = new DataInputStream JavaDoc(byteInStream);
910     }
911   }
912
913 }
914
915
Popular Tags