KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > message > codec > MessageCodecStreamImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.message.codec;
26
27 import java.io.DataInput JavaDoc;
28 import java.io.DataOutput JavaDoc;
29 import java.io.Externalizable JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.ObjectOutput JavaDoc;
33
34 import org.objectweb.dream.message.Message;
35 import org.objectweb.dream.message.MessageNC;
36 import org.objectweb.dream.message.MessageTypeImpl;
37 import org.objectweb.dream.message.MessageTypeNCImpl;
38
39 /**
40  * Basic implementation of a message codec.
41  */

42 public class MessageCodecStreamImpl extends AbstractMessageCodecObjectStream
43 {
44
45   // ---------------------------------------------------------------------------
46
// Implementation of the AbstractMessageCodecDataStream abstract methods
47
// ---------------------------------------------------------------------------
48

49   /**
50    * @see AbstractMessageCodecDataStream#doDecodeSingleMessage(DataInput)
51    */

52   protected Message doDecodeSingleMessage(DataInput JavaDoc input) throws IOException JavaDoc
53   {
54     Message m;
55     boolean messageNC = input.readBoolean();
56     if (messageNC)
57     {
58       m = messageManagerItf
59           .createMessage(MessageTypeNCImpl.EMPTY_MESSAGE_TYPE_NC);
60     }
61     else
62     {
63       m = messageManagerItf.createMessage(MessageTypeImpl.EMPTY_MESSAGE_TYPE);
64     }
65     try
66     {
67       ((Externalizable JavaDoc) m).readExternal((ObjectInput JavaDoc) input);
68     }
69     catch (ClassNotFoundException JavaDoc e)
70     {
71       throw new IOException JavaDoc("Unable to decode message : " + e.getMessage());
72     }
73     m.setMessageManagerId(messageManagerItf.getMessageManagerId());
74     return m;
75   }
76
77   /**
78    * @see AbstractMessageCodecDataStream#doEncodeSingleMessage(Message,
79    * DataOutput)
80    */

81   protected void doEncodeSingleMessage(Message message, DataOutput JavaDoc output)
82       throws IOException JavaDoc
83   {
84     if (message instanceof MessageNC)
85     {
86       output.writeBoolean(true);
87     }
88     else
89     {
90       output.writeBoolean(false);
91     }
92     ((Externalizable JavaDoc) message).writeExternal((ObjectOutput JavaDoc) output);
93   }
94 }
Popular Tags