KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import java.io.DataInput JavaDoc;
25 import java.io.DataOutput JavaDoc;
26
27 /**
28  * Interface to the rest of the JMS service for encoding and decoding
29  * messages to and from a byte stream.
30  *
31  * @author Dan Greff
32  */

33 public final class MessageEncoder
34 {
35   /*
36    * Used to identify different encoded message types.
37    */

38   static final byte MESSAGE = 0;
39   static final byte TEXT_MESSAGE = 1;
40   static final byte BYTE_MESSAGE = 2;
41   static final byte MAP_MESSAGE = 3;
42   static final byte STREAM_MESSAGE = 4;
43   static final byte OBJECT_MESSAGE = 5;
44   
45     /////////////////////////////////////////////////////////////////////////
46
// Public Methods //
47
/////////////////////////////////////////////////////////////////////////
48

49   /**
50    * Encodes the given message to bytes on the DataOutput stream.
51    *
52    * @param JmsMessage Message to be encoded.
53    * @param DataOutput DataOutput implementation to have the encoding sent to
54    *
55    * @exception IOException If there is any error during the encoding.
56    */

57   public static void encode(JmsMessage msg, DataOutput JavaDoc out) throws IOException JavaDoc
58   {
59     if (msg == null)
60       return;
61       
62     out.write(msg.getMarshalingID());
63     msg.marshal(out);
64   }
65   
66   /**
67    * Encodes the given messages to bytes on the DataOutput stream. This
68    * will result in the same byte output as multiple calls to <code>
69    * encode(JmsMessage msg, DataOutput out)</code>
70    *
71    * @param JmsMessage[] Messages to be encoded.
72    * @param DataOutput Dataoutput implementation to have the encoding sent to.
73    *
74    * @exception If there is any error during the encoding.
75    */

76   public static void encode(JmsMessage [] msgs, DataOutput JavaDoc out) throws IOException JavaDoc
77   {
78     if (msgs == null)
79       return;
80       
81     for (int i=0; i < msgs.length; ++i) {
82       out.write(msgs[i].getMarshalingID());
83       msgs[i].marshal(out);
84     }
85   }
86   
87   /**
88    * Decodes the given DataInput into a JmsMessage. Will only decode one
89    * message off of the input stream. If the stream does not contain
90    * a message, a IOException will be thrown.
91    *
92    * @param DataInput DataInput stream holding the encoded message
93    *
94    * @return JmsMessage or one of its subclasses.
95    *
96    * @exception If there is any error during the decoding or if the
97    * given stream does not contain a message.
98    */

99   public static JmsMessage decode(DataInput JavaDoc in) throws IOException JavaDoc
100   {
101     byte messageID = in.readByte();
102     JmsMessage msg = null;
103     switch (messageID)
104     {
105       case (MESSAGE):
106         msg = new JmsMessage();
107         msg.unmarshal(in);
108         
109         break;
110       case (TEXT_MESSAGE):
111         msg = new JmsTextMessage();
112         msg.unmarshal(in);
113       
114         break;
115       case (BYTE_MESSAGE):
116         msg = new JmsBytesMessage();
117         msg.unmarshal(in);
118        
119         break;
120       case (MAP_MESSAGE):
121         msg = new JmsMapMessage();
122         msg.unmarshal(in);
123         
124         break;
125       case (STREAM_MESSAGE):
126         msg = new JmsStreamMessage();
127         msg.unmarshal(in);
128         
129         break;
130       case (OBJECT_MESSAGE):
131         msg = new JmsObjectMessage();
132         msg.unmarshal(in);
133         
134         break;
135       default:
136         throw new IOException JavaDoc("The input stream is corrupted.");
137     }
138     return msg;
139   }
140
141 }
142
Popular Tags