View Javadoc

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.OutputStream;
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 BytesWrittenResetOutputStream extends OutputStream {
32  
33      /***
34       * The output stream wrapped by this method
35       */
36      OutputStream out = 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 written before the counter is reset.
45       */
46      int lengthReset = 0;
47  
48      /***
49       * The number of bytes written since the counter was last reset
50       */
51      int writtenCounter = 0;
52  
53      public BytesWrittenResetOutputStream(OutputStream out,
54                                           Watchdog watchdog,
55                                           int lengthReset) {
56          this.out = out;
57          this.watchdog = watchdog;
58          this.lengthReset = lengthReset;
59  
60          writtenCounter = 0;
61      }
62  
63      /***
64       * Write an array of bytes to the stream
65       *
66       * @param b the array of bytes to write to the stream
67       * @param off the index in the array where we start writing
68       * @param len the number of bytes of the array to write
69       *
70       * @throws IOException if an exception is encountered when writing
71       */
72      public void write(byte[] b, int off, int len) throws IOException {
73          out.write(b, off, len);
74          writtenCounter += len;
75  
76          if (writtenCounter > lengthReset) {
77              writtenCounter = 0;
78              watchdog.reset();
79          }
80      }
81  
82      /***
83       * Write a byte to the stream
84       *
85       * @param b the byte to write to the stream
86       *
87       * @throws IOException if an exception is encountered when writing
88       */
89      public void write(int b) throws IOException {
90          out.write(b);
91          writtenCounter++;
92  
93          if (writtenCounter > lengthReset) {
94              writtenCounter = 0;
95              watchdog.reset();
96          }
97      }
98  
99      /***
100      * Flush the stream
101      *
102      * @throws IOException if an exception is encountered when flushing
103      */
104     public void flush() throws IOException {
105         out.flush();
106     }
107 
108     /***
109      * Close the stream
110      *
111      * @throws IOException if an exception is encountered when closing
112      */
113     public void close() throws IOException {
114         out.close();
115     }
116 }