1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: XStreamOutput.java,v 1.10 2005/09/10 11:26:47 mat007 Exp $
29 */
30
31 package jtge.engine.io;
32
33 import java.io.ObjectOutputStream;
34 import java.io.OutputStream;
35 import java.io.OutputStreamWriter;
36 import jtge.engine.command.ICommand;
37 import com.thoughtworks.xstream.XStream;
38 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
39
40 /***
41 * Implements an output implementation based on the XStream library.
42 *
43 * @see <a href="http://xstream.codehaus.org">XStream</a>
44 * @author Mathieu Champlon
45 * @version $Revision: 1.10 $ $Date: 2005/09/10 11:26:47 $
46 */
47 public class XStreamOutput implements IOutputImp
48 {
49 private final XStream xstream;
50 private final OutputStreamWriter writer;
51 private ObjectOutputStream output;
52 private boolean isClosed;
53
54 /***
55 * Create an output.
56 *
57 * @param stream the stream to read data from
58 */
59 public XStreamOutput( final OutputStream stream )
60 {
61 xstream = new XStream();
62 writer = new OutputStreamWriter( stream );
63 isClosed = false;
64 }
65
66 /***
67 * {@inheritDoc}
68 */
69 public final void write( final ICommand command )
70 {
71 try
72 {
73 if( output == null )
74 {
75
76
77 output = xstream.createObjectOutputStream( new PrettyPrintWriter( writer, " " ), "object-stream" );
78 }
79 if( command != null )
80 {
81 output.writeObject( command );
82 output.flush();
83 }
84 }
85 catch( Exception e )
86 {
87 throw new InputOutputException( e );
88 }
89 }
90
91 /***
92 * {@inheritDoc}
93 */
94 public final void close()
95 {
96 isClosed = true;
97 try
98 {
99 if( output != null )
100 output.close();
101 writer.close();
102 }
103 catch( Exception e )
104 {
105 throw new InputOutputException( e );
106 }
107 }
108
109 /***
110 * {@inheritDoc}
111 */
112 public final boolean isClosed()
113 {
114 return isClosed;
115 }
116 }