1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8
9 package org.apache.james.util;
10
11 import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15
16 /***
17 * This will reset the scheduler each time a certain amount of data has
18 * been transfered. This allows us to keep the timeout settings low, while
19 * not timing out during large data transfers.
20 */
21 public class SchedulerNotifyInputStream extends InputStream {
22
23 /***
24 * The wrapped InputStream
25 */
26 InputStream in = null;
27
28 /***
29 * The scheduler managing the trigger to be reset by this stream
30 */
31 TimeScheduler scheduler = null;
32
33 /***
34 * The name of the trigger
35 */
36 String triggerName = null;
37
38 /***
39 * The number of bytes that need to be read before the counter is reset.
40 */
41 int lengthReset = 0;
42
43 /***
44 * The number of bytes read since the counter was last reset
45 */
46 int readCounter = 0;
47
48 /***
49 * @param in the InputStream to be wrapped by this stream
50 * @param scheduler the TimeScheduler managing the trigger to be reset by this stream
51 * @param triggerName the name of the particular trigger to be reset by this stream
52 * @param lengthReset the number of bytes to be read in between trigger resets
53 */
54 public SchedulerNotifyInputStream(InputStream in,
55 TimeScheduler scheduler, String triggerName, int lengthReset) {
56 this.in = in;
57 this.scheduler = scheduler;
58 this.triggerName = triggerName;
59 this.lengthReset = lengthReset;
60
61 readCounter = 0;
62 }
63
64 /***
65 * Read an array of bytes from the stream
66 *
67 * @param b the array of bytes to read from 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 read
70 *
71 * @return the number of bytes read
72 *
73 * @throws IOException if an exception is encountered when reading
74 */
75 public int read(byte[] b, int off, int len) throws IOException {
76 int l = in.read(b, off, len);
77 readCounter += l;
78
79 if (readCounter > lengthReset) {
80 readCounter -= lengthReset;
81 scheduler.resetTrigger(triggerName);
82 }
83
84 return l;
85 }
86
87 /***
88 * Read a byte from the stream
89 *
90 * @return the byte read from the stream
91 * @throws IOException if an exception is encountered when reading
92 */
93 public int read() throws IOException {
94 int b = in.read();
95 readCounter++;
96
97 if (readCounter > lengthReset) {
98 readCounter -= lengthReset;
99 scheduler.resetTrigger(triggerName);
100 }
101
102 return b;
103 }
104
105 /***
106 * Close the stream
107 *
108 * @throws IOException if an exception is encountered when closing
109 */
110 public void close() throws IOException {
111 in.close();
112 }
113 }
This page was automatically generated by Maven