1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8
9 package org.apache.james.util.watchdog;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13
14 /***
15 * This will reset the Watchdog each time a certain amount of data has
16 * been transferred. This allows us to keep the timeout settings low, while
17 * not timing out during large data transfers.
18 */
19 public class BytesReadResetInputStream extends InputStream {
20
21 /***
22 * The wrapped InputStream
23 */
24 private InputStream in = null;
25
26 /***
27 * The Watchdog to be reset every lengthReset bytes
28 */
29 private Watchdog watchdog;
30
31 /***
32 * The number of bytes that need to be read before the counter is reset.
33 */
34 private int lengthReset = 0;
35
36 /***
37 * The number of bytes read since the counter was last reset
38 */
39 int readCounter = 0;
40
41 /***
42 * @param in the InputStream to be wrapped by this stream
43 * @param watchdog the watchdog to be reset
44 * @param lengthReset the number of bytes to be read in between trigger resets
45 */
46 public BytesReadResetInputStream(InputStream in,
47 Watchdog watchdog,
48 int lengthReset) {
49 this.in = in;
50 this.watchdog = watchdog;
51 this.lengthReset = lengthReset;
52
53 readCounter = 0;
54 }
55
56 /***
57 * Read an array of bytes from the stream
58 *
59 * @param b the array of bytes to read from the stream
60 * @param off the index in the array where we start writing
61 * @param len the number of bytes of the array to read
62 *
63 * @return the number of bytes read
64 *
65 * @throws IOException if an exception is encountered when reading
66 */
67 public int read(byte[] b, int off, int len) throws IOException {
68 int l = in.read(b, off, len);
69 readCounter += l;
70
71 if (readCounter > lengthReset) {
72 readCounter = 0;
73 watchdog.reset();
74 }
75
76 return l;
77 }
78
79 /***
80 * Read a byte from the stream
81 *
82 * @return the byte read from the stream
83 * @throws IOException if an exception is encountered when reading
84 */
85 public int read() throws IOException {
86 int b = in.read();
87 readCounter++;
88
89 if (readCounter > lengthReset) {
90 readCounter = 0;
91 watchdog.reset();
92 }
93
94 return b;
95 }
96
97 /***
98 * Close the stream
99 *
100 * @throws IOException if an exception is encountered when closing
101 */
102 public void close() throws IOException {
103 in.close();
104 }
105 }
This page was automatically generated by Maven