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
22 package org.apache.james.util.watchdog;
23
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 /**
28 * This will reset the Watchdog each time a certain amount of data has
29 * been transferred. This allows us to keep the timeout settings low, while
30 * not timing out during large data transfers.
31 */
32 public class BytesWrittenResetOutputStream extends OutputStream {
33
34 /**
35 * The output stream wrapped by this method
36 */
37 OutputStream out = null;
38
39 /**
40 * The Watchdog to be reset every lengthReset bytes
41 */
42 private Watchdog watchdog;
43
44 /**
45 * The number of bytes that need to be written before the counter is reset.
46 */
47 int lengthReset = 0;
48
49 /**
50 * The number of bytes written since the counter was last reset
51 */
52 int writtenCounter = 0;
53
54 public BytesWrittenResetOutputStream(OutputStream out,
55 Watchdog watchdog,
56 int lengthReset) {
57 this.out = out;
58 this.watchdog = watchdog;
59 this.lengthReset = lengthReset;
60
61 writtenCounter = 0;
62 }
63
64 /**
65 * Write an array of bytes to the stream
66 *
67 * @param b the array of bytes to write to the stream
68 * @param off the index in the array where we start writing
69 * @param len the number of bytes of the array to write
70 *
71 * @throws IOException if an exception is encountered when writing
72 */
73 public void write(byte[] b, int off, int len) throws IOException {
74 out.write(b, off, len);
75 writtenCounter += len;
76
77 if (writtenCounter > lengthReset) {
78 writtenCounter = 0;
79 watchdog.reset();
80 }
81 }
82
83 /**
84 * Write a byte to the stream
85 *
86 * @param b the byte to write to the stream
87 *
88 * @throws IOException if an exception is encountered when writing
89 */
90 public void write(int b) throws IOException {
91 out.write(b);
92 writtenCounter++;
93
94 if (writtenCounter > lengthReset) {
95 writtenCounter = 0;
96 watchdog.reset();
97 }
98 }
99
100 /**
101 * Flush the stream
102 *
103 * @throws IOException if an exception is encountered when flushing
104 */
105 public void flush() throws IOException {
106 out.flush();
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 out.close();
116 }
117 }