KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32
33 import org.objectweb.dream.message.Message;
34
35 /**
36  * Abstract codec implementation for codec using {@link ObjectOutputStream }and
37  * {@link ObjectInputStream}. This codec implementation requires
38  * {@link InputStream }as codec input and {@link OutputStream }as codec
39  * output.
40  */

41 public abstract class AbstractMessageCodecObjectStream
42     extends
43       AbstractMessageCodecDataStream
44     implements
45       MessageCodecObjectStreamAttributeController
46 {
47
48   // ---------------------------------------------------------------------------
49
// Attribute field
50
// ---------------------------------------------------------------------------
51
boolean reuseObjectStream;
52
53   // ---------------------------------------------------------------------------
54
// Implementation of the MessageCodec interface
55
// ---------------------------------------------------------------------------
56

57   /**
58    * @see MessageCodec#encode(CodecInputOutput, Message)
59    */

60   public void encode(CodecInputOutput cio, Message message) throws IOException JavaDoc
61   {
62     Object JavaDoc output = cio.getOutput();
63     ObjectOutputStream JavaDoc oos;
64     if (output instanceof ObjectOutputStream JavaDoc)
65     {
66       oos = (ObjectOutputStream JavaDoc) output;
67     }
68     else
69     {
70       if (!(output instanceof OutputStream JavaDoc))
71       {
72         throw new IOException JavaDoc("Unknown output : " + output);
73       }
74       oos = new ObjectOutputStream JavaDoc((OutputStream JavaDoc) output);
75       if (reuseObjectStream)
76       {
77         cio.setOutput(oos);
78       }
79     }
80     doEncode(message, oos);
81     oos.flush();
82   }
83
84   /**
85    * @see MessageCodec#decode(CodecInputOutput)
86    */

87   public Message decode(CodecInputOutput cio) throws IOException JavaDoc
88   {
89     Object JavaDoc input = cio.getInput();
90     ObjectInputStream JavaDoc ois;
91     if (input instanceof ObjectInputStream JavaDoc)
92     {
93       ois = (ObjectInputStream JavaDoc) input;
94     }
95     else
96     {
97       if (!(input instanceof InputStream JavaDoc))
98       {
99         throw new IOException JavaDoc("Unknown input : " + input);
100       }
101       ois = new ObjectInputStream JavaDoc((InputStream JavaDoc) input);
102       if (reuseObjectStream)
103       {
104         cio.setInput(ois);
105       }
106     }
107     return doDecode(ois);
108   }
109
110   // ---------------------------------------------------------------------------
111
// Implementation of the AttributeController interface
112
// ---------------------------------------------------------------------------
113

114   /**
115    * @see MessageCodecObjectStreamAttributeController#getReuseObjectStream()
116    */

117   public boolean getReuseObjectStream()
118   {
119     return reuseObjectStream;
120   }
121
122   /**
123    * @see MessageCodecObjectStreamAttributeController#setReuseObjectStream(boolean)
124    */

125   public void setReuseObjectStream(boolean reuseObjectStream)
126   {
127     this.reuseObjectStream = reuseObjectStream;
128   }
129 }
Popular Tags