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