KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > stream > AppendingObjectOutputStream


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.util.stream;
11
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.OutputStream JavaDoc;
15
16 /**
17  * An <tt>ObjectOutputStream</tt> that can conditionally be put into
18  * <i>appending</i> mode.
19  *
20  * <dl>
21  * <dt><b>Concurrency: </b></dt>
22  * <dd>This class is <b>not</b> synchronized.</dd>
23  * </dl>
24  *
25  * @version <tt>$Revision: 1.1 $</tt>
26  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
27  */

28 public class AppendingObjectOutputStream
29    extends ObjectOutputStreamAdapter
30 {
31    /**
32     * Construct an <tt>AppendingObjectOutputStream</tt>.
33     *
34     * @param out An <tt>OutputStream</tt> stream.
35     * @param append <tt>True</tt> to append written objects; <tt>false</tt>
36     * to use default action (writes stream header).
37     *
38     * @throws IOException Any exception thrown by
39     * the underlying <tt>OutputStream</tt>.
40     */

41    public AppendingObjectOutputStream(OutputStream JavaDoc out, boolean append)
42       throws IOException JavaDoc
43    {
44       super(createStream(out, append));
45    }
46
47    /**
48     * Helper to return a <tt>ObjectOutputStream</tt>.
49     */

50    private static ObjectOutputStream JavaDoc createStream(OutputStream JavaDoc out,
51                                                   boolean append)
52       throws IOException JavaDoc
53    {
54       ObjectOutputStream JavaDoc stream;
55
56       // if we are appending then return an append only stream
57
if (append) {
58          stream = new AppendObjectOutputStream(out);
59       }
60       // else if it already an oos then return it
61
else if (out instanceof ObjectOutputStream JavaDoc) {
62          stream = (ObjectOutputStream JavaDoc)out;
63       }
64       // else wrap the stream in an oos
65
else {
66          stream = new ObjectOutputStream JavaDoc(out);
67       }
68
69       return stream;
70    }
71 }
72
Popular Tags