KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 /**
23  * An InputStream that provides buffering and end-of-stream monitoring.
24  *
25  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
26  */

27 public class MonitorInputStream
28     extends BufferedInputStream JavaDoc
29 {
30     private boolean finished;
31     private long count;
32
33     public MonitorInputStream(final InputStream JavaDoc in)
34     {
35         super(in);
36         count = 0;
37     }
38
39     /**
40      * Reads a character.
41      */

42     public int read() throws IOException JavaDoc
43     {
44         if (finished)
45         {
46             return -1;
47         }
48
49         final int ch = super.read();
50         if (ch != -1)
51         {
52             count++;
53             return ch;
54         }
55
56         // End-of-stream
57
close();
58         return -1;
59     }
60
61     /**
62      * Reads bytes from this input stream.error occurs.
63      */

64     public int read(final byte[] buffer, final int offset, final int length)
65         throws IOException JavaDoc
66     {
67         if (finished)
68         {
69             return -1;
70         }
71
72         final int nread = super.read(buffer, offset, length);
73         if (nread != -1)
74         {
75             count += nread;
76             return nread;
77         }
78
79         // End-of-stream
80
close();
81         return -1;
82     }
83
84     /**
85      * Closes this input stream and releases any system resources
86      * associated with the stream.
87      */

88     public void close() throws IOException JavaDoc
89     {
90         if (finished)
91         {
92             return;
93         }
94
95         // Close the stream
96
IOException JavaDoc exc = null;
97         try
98         {
99             super.close();
100         }
101         catch (final IOException JavaDoc ioe)
102         {
103             exc = ioe;
104         }
105
106         // Notify that the stream has been closed
107
try
108         {
109             onClose();
110         }
111         catch (final IOException JavaDoc ioe)
112         {
113             exc = ioe;
114         }
115
116         finished = true;
117         if (exc != null)
118         {
119             throw exc;
120         }
121     }
122
123     /**
124      * Called after the stream has been closed. This implementation does
125      * nothing.
126      */

127     protected void onClose() throws IOException JavaDoc
128     {
129     }
130
131     /**
132      * Get the nuber of bytes read by this input stream
133      */

134     public long getCount()
135     {
136         return count;
137     }
138 }
139
Popular Tags