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.InputStream;
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 SchedulerNotifyInputStream extends InputStream {
35
36 /**
37 * The wrapped InputStream
38 */
39 InputStream in = null;
40
41 /**
42 * The scheduler managing the trigger to be reset by this stream
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 read before the counter is reset.
53 */
54 int lengthReset = 0;
55
56 /**
57 * The number of bytes read since the counter was last reset
58 */
59 int readCounter = 0;
60
61 /**
62 * @param in the InputStream to be wrapped by this stream
63 * @param scheduler the TimeScheduler managing the trigger to be reset by this stream
64 * @param triggerName the name of the particular trigger to be reset by this stream
65 * @param lengthReset the number of bytes to be read in between trigger resets
66 */
67 public SchedulerNotifyInputStream(InputStream in,
68 TimeScheduler scheduler, String triggerName, int lengthReset) {
69 this.in = in;
70 this.scheduler = scheduler;
71 this.triggerName = triggerName;
72 this.lengthReset = lengthReset;
73
74 readCounter = 0;
75 }
76
77 /**
78 * Read an array of bytes from the stream
79 *
80 * @param b the array of bytes to read from the stream
81 * @param off the index in the array where we start writing
82 * @param len the number of bytes of the array to read
83 *
84 * @return the number of bytes read
85 *
86 * @throws IOException if an exception is encountered when reading
87 */
88 public int read(byte[] b, int off, int len) throws IOException {
89 int l = in.read(b, off, len);
90 readCounter += l;
91
92 if (readCounter > lengthReset) {
93 readCounter -= lengthReset;
94 scheduler.resetTrigger(triggerName);
95 }
96
97 return l;
98 }
99
100 /**
101 * Read a byte from the stream
102 *
103 * @return the byte read from the stream
104 * @throws IOException if an exception is encountered when reading
105 */
106 public int read() throws IOException {
107 int b = in.read();
108 readCounter++;
109
110 if (readCounter > lengthReset) {
111 readCounter -= lengthReset;
112 scheduler.resetTrigger(triggerName);
113 }
114
115 return b;
116 }
117
118 /**
119 * Close the stream
120 *
121 * @throws IOException if an exception is encountered when closing
122 */
123 public void close() throws IOException {
124 in.close();
125 }
126 }