KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > watchdog > BytesReadResetInputStream


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

17
18
19 package org.apache.james.util.watchdog;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 /**
25  * This will reset the Watchdog each time a certain amount of data has
26  * been transferred. This allows us to keep the timeout settings low, while
27  * not timing out during large data transfers.
28  */

29 public class BytesReadResetInputStream extends InputStream JavaDoc {
30
31     /**
32      * The wrapped InputStream
33      */

34     private InputStream JavaDoc in = null;
35
36     /**
37      * The Watchdog to be reset every lengthReset bytes
38      */

39     private Watchdog watchdog;
40
41     /**
42      * The number of bytes that need to be read before the counter is reset.
43      */

44     private int lengthReset = 0;
45
46     /**
47      * The number of bytes read since the counter was last reset
48      */

49     int readCounter = 0;
50
51     /**
52      * @param in the InputStream to be wrapped by this stream
53      * @param watchdog the watchdog to be reset
54      * @param lengthReset the number of bytes to be read in between trigger resets
55      */

56     public BytesReadResetInputStream(InputStream JavaDoc in,
57                                      Watchdog watchdog,
58                                      int lengthReset) {
59         this.in = in;
60         this.watchdog = watchdog;
61         this.lengthReset = lengthReset;
62
63         readCounter = 0;
64     }
65
66     /**
67      * Read an array of bytes from the stream
68      *
69      * @param b the array of bytes to read from the stream
70      * @param off the index in the array where we start writing
71      * @param len the number of bytes of the array to read
72      *
73      * @return the number of bytes read
74      *
75      * @throws IOException if an exception is encountered when reading
76      */

77     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
78         int l = in.read(b, off, len);
79         readCounter += l;
80
81         if (readCounter > lengthReset) {
82             readCounter = 0;
83             watchdog.reset();
84         }
85
86         return l;
87     }
88
89     /**
90      * Read a byte from the stream
91      *
92      * @return the byte read from the stream
93      * @throws IOException if an exception is encountered when reading
94      */

95     public int read() throws IOException JavaDoc {
96         int b = in.read();
97         readCounter++;
98
99         if (readCounter > lengthReset) {
100             readCounter = 0;
101             watchdog.reset();
102         }
103
104         return b;
105     }
106
107     /**
108      * Close the stream
109      *
110      * @throws IOException if an exception is encountered when closing
111      */

112     public void close() throws IOException JavaDoc {
113         in.close();
114     }
115 }
116
Popular Tags