KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * An <code>ObjectOutputStream</code> that will auto reset after <i>n</i>
17  * objects have been written to the underlying stream.
18  *
19  * <h3>Concurrency</h3>
20  * This class is <b>not</b> synchronized.
21  *
22  * @version <tt>$Revision: 1.1 $</tt>
23  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
24  */

25 public class AutoResetObjectOutputStream
26    extends ObjectOutputStreamAdapter
27 {
28    /** Number of objects to write before resetting. */
29    protected int after; // = 0
30

31    /** Number of objects written so far. */
32    protected int count; // = 0
33

34    /**
35     * Construct a new AutoResetObjectOutputStream.
36     *
37     * @param out An ObjectOutputStream stream.
38     * @param after Number of objects to write before resetting.
39     *
40     * @throws IllegalArgumentException After <= 0
41     * @throws IOException Any exception thrown by
42     * the underlying OutputStream.
43     */

44    public AutoResetObjectOutputStream(ObjectOutputStream JavaDoc out, int after)
45       throws IOException JavaDoc
46    {
47       super(out);
48
49       setResetAfter(after);
50    }
51
52    /**
53     * Set the number of objects that must be written before resetting
54     * the stream.
55     *
56     * @param after Number of objects to write before resetting.
57     *
58     * @throws IllegalArgumentException After <= 0
59     */

60    public void setResetAfter(int after) {
61       if (after <= 0)
62          throw new IllegalArgumentException JavaDoc("after <= 0");
63
64       this.after = after;
65    }
66
67    /**
68     * Get the number of objects that must be written before resetting
69     * the stream.
70     *
71     * @return Number of objects to write before resetting.
72     */

73    public final int getResetAfter() {
74       return after;
75    }
76
77    /**
78     * Get the number of objects written to the stream so far.
79     *
80     * @return The number of objects written to the stream so far.
81     */

82    public final int getCount() {
83       return count;
84    }
85
86    /**
87     * Write the given object and reset if the number of objects written
88     * (including this one) exceeds the after count.
89     *
90     * @param obj Object to write.
91     *
92     * @throws IOException Any exception thrown by the underlying stream.
93     */

94    protected void writeObjectOverride(Object JavaDoc obj) throws IOException JavaDoc {
95       super.writeObjectOverride(obj);
96       count++;
97
98       if (count >= after) {
99          reset();
100       }
101    }
102
103    /**
104     * Resets the object counter as well as the nested stream.
105     *
106     * @throws IOException
107     */

108    public void reset() throws IOException JavaDoc {
109       out.reset();
110       count = 0;
111    }
112 }
113
Popular Tags