KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > util > MonitorOutputStream


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.util;
17
18 import java.io.BufferedOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 /**
23  * An OutputStream that provides buffering and end-of-stream monitoring.
24  *
25  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
26  */

27 public class MonitorOutputStream
28     extends BufferedOutputStream JavaDoc
29 {
30     private boolean finished;
31
32     public MonitorOutputStream(final OutputStream JavaDoc out)
33     {
34         super(out);
35     }
36
37     /**
38      * Closes this output stream.
39      */

40     public void close() throws IOException JavaDoc
41     {
42         if (finished)
43         {
44             return;
45         }
46
47         // Close the output stream
48
IOException JavaDoc exc = null;
49         try
50         {
51             super.close();
52         }
53         catch (final IOException JavaDoc ioe)
54         {
55             exc = ioe;
56         }
57
58         // Notify of end of output
59
try
60         {
61             onClose();
62         }
63         catch (final IOException JavaDoc ioe)
64         {
65             exc = ioe;
66         }
67
68         finished = true;
69
70         if (exc != null)
71         {
72             throw exc;
73         }
74     }
75
76     /**
77      * Called after this stream is closed. This implementation does nothing.
78      */

79     protected void onClose() throws IOException JavaDoc
80     {
81     }
82 }
83
Popular Tags