1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.util.watchdog;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 /***
27 * This will reset the Watchdog each time a certain amount of data has
28 * been transferred. This allows us to keep the timeout settings low, while
29 * not timing out during large data transfers.
30 */
31 public class BytesReadResetInputStream extends InputStream {
32
33 /***
34 * The wrapped InputStream
35 */
36 private InputStream in = null;
37
38 /***
39 * The Watchdog to be reset every lengthReset bytes
40 */
41 private Watchdog watchdog;
42
43 /***
44 * The number of bytes that need to be read before the counter is reset.
45 */
46 private int lengthReset = 0;
47
48 /***
49 * The number of bytes read since the counter was last reset
50 */
51 int readCounter = 0;
52
53 /***
54 * @param in the InputStream to be wrapped by this stream
55 * @param watchdog the watchdog to be reset
56 * @param lengthReset the number of bytes to be read in between trigger resets
57 */
58 public BytesReadResetInputStream(InputStream in,
59 Watchdog watchdog,
60 int lengthReset) {
61 this.in = in;
62 this.watchdog = watchdog;
63 this.lengthReset = lengthReset;
64
65 readCounter = 0;
66 }
67
68 /***
69 * Read an array of bytes from the stream
70 *
71 * @param b the array of bytes to read from the stream
72 * @param off the index in the array where we start writing
73 * @param len the number of bytes of the array to read
74 *
75 * @return the number of bytes read
76 *
77 * @throws IOException if an exception is encountered when reading
78 */
79 public int read(byte[] b, int off, int len) throws IOException {
80 int l = in.read(b, off, len);
81 readCounter += l;
82
83 if (readCounter > lengthReset) {
84 readCounter = 0;
85 watchdog.reset();
86 }
87
88 return l;
89 }
90
91 /***
92 * Read a byte from the stream
93 *
94 * @return the byte read from the stream
95 * @throws IOException if an exception is encountered when reading
96 */
97 public int read() throws IOException {
98 int b = in.read();
99 readCounter++;
100
101 if (readCounter > lengthReset) {
102 readCounter = 0;
103 watchdog.reset();
104 }
105
106 return b;
107 }
108
109 /***
110 * Close the stream
111 *
112 * @throws IOException if an exception is encountered when closing
113 */
114 public void close() throws IOException {
115 in.close();
116 }
117 }