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