KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > XmlOutputStream


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.xml;
10
11 import j2me.lang.IllegalStateException;
12 import java.io.IOException;
13 import java.io.OutputStream;
14
15 import javolution.lang.Reusable;
16
17 /**
18  * <p> This class represents an object output stream using {@link ObjectWriter}
19  * for object serialization.</p>
20  *
21  * <p> Instances of this class embed their own data buffer, wrapping using a
22  * <code>java.io.BufferedOutputStream</code> is therefore unnescessary.</p>
23  *
24  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
25  * @version 2.2, January 8, 2005
26  * @see XmlInputStream
27  */

28 public class XmlOutputStream/*<T>*/ extends OutputStream implements Reusable {
29
30     /**
31      * Holds the byte indicating the end of a XML document transmission,
32      * acceptable values are <code>0xFE</code> and <code>0xFF</code>
33      * as they do not occur in a UTF-8 encoded text.
34      */

35     static final byte END_XML = (byte) 0xFE;
36
37     /**
38      * Holds the output stream.
39      */

40     private OutputStream _outputStream;
41
42     /**
43      * Holds the object writer.
44      */

45     private final ObjectWriter/*<T>*/ _objectWriter = new ObjectWriter/*<T>*/();
46
47     /**
48      * Holds the object writer.
49      */

50     private final OutputStreamProxy _outputStreamProxy = new OutputStreamProxy();
51
52     /**
53      * Default constructor.
54      */

55     public XmlOutputStream() {
56     }
57     
58     /**
59      * Sets the underlying output destination for this stream.
60      *
61      * @param out the output destination.
62      * @return <code>this</code>
63      * @throws IllegalStateException if this stream is being reused and
64      * it has not been {@link #close closed} or {@link #reset reset}.
65      */

66     public XmlOutputStream setOutputStream(OutputStream out) {
67         if (_outputStream != null)
68             throw new IllegalStateException("Stream not closed or reset");
69         _outputStream = out;
70         return this;
71     }
72
73     /**
74      * Writes an object to the underlying stream using an {@link ObjectWriter}.
75      *
76      * @param obj the object writen using its xml representation.
77      * @throws IOException if an I/O error occurs.
78      */

79     public void writeObject(Object/*T*/ obj) throws IOException {
80         if (_outputStream == null) throw new IOException("Stream closed");
81         _objectWriter.write(obj, _outputStreamProxy);
82         _outputStream.write(END_XML);
83         _outputStream.flush();
84     }
85
86     // Implements abstract method.
87

88     /**
89      * Writes the specified byte to this output stream
90      *
91      * @param b the byte.
92      * @throws IOException if an I/O error occurs.
93      */

94     public void write(int b) throws IOException {
95         if (_outputStream == null) throw new IOException("Stream closed");
96         _outputStream.write(b);
97     }
98     
99     /**
100      * Flushes this output stream and forces any buffered output bytes
101      * to be written out.
102      *
103      * @throws IOException if an I/O error occurs.
104      */

105     public void flush() throws IOException {
106         if (_outputStream == null) throw new IOException("Stream closed");
107         _outputStream.flush();
108     }
109     
110     /**
111      * Writes the specified number of bytes from the specified byte array
112      * starting at the specified offset to this output stream.
113      *
114      * @param b the data.
115      * @param off the start offset in the data.
116      * @param len the number of bytes to write.
117      * @throws IOException if an I/O error occurs.
118      */

119     public void write(byte b[], int off, int len) throws IOException {
120         if (_outputStream == null) throw new IOException("Stream closed");
121         _outputStream.write(b, off, len);
122     }
123     
124     /**
125      * Closes and {@link #reset resets} this stream for reuse.
126      *
127      * @throws IOException if an I/O error occurs.
128      */

129     public void close() throws IOException {
130         if (_outputStream != null) {
131             _outputStream.close();
132             reset();
133         }
134     }
135
136     // Implements Reusable interface.
137
public void reset() {
138         _objectWriter.reset();
139         _outputStream = null;
140     }
141
142     /**
143      * This inner class represents an output stream proxy for which the
144      * {@link #close()} command has no effect.
145      */

146     private final class OutputStreamProxy extends OutputStream {
147         public void flush() throws IOException {
148             _outputStream.flush();
149         }
150         public void write(byte b[], int off, int len) throws IOException {
151             _outputStream.write(b, off, len);
152         }
153         public void write(int b) throws IOException {
154             _outputStream.write(b);
155         }
156         public void close() throws IOException {
157             // Do nothing.
158
}
159     }
160 }
Popular Tags