KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > SchedulerNotifyInputStream


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18
19 package org.apache.james.util;
20
21 import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
22
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 /**
27  * This will reset the scheduler each time a certain amount of data has
28  * been transfered. This allows us to keep the timeout settings low, while
29  * not timing out during large data transfers.
30  */

31 public class SchedulerNotifyInputStream extends InputStream JavaDoc {
32
33     /**
34      * The wrapped InputStream
35      */

36     InputStream JavaDoc in = null;
37
38     /**
39      * The scheduler managing the trigger to be reset by this stream
40      */

41     TimeScheduler scheduler = null;
42
43     /**
44      * The name of the trigger
45      */

46     String JavaDoc triggerName = null;
47
48     /**
49      * The number of bytes that need to be read before the counter is reset.
50      */

51     int lengthReset = 0;
52
53     /**
54      * The number of bytes read since the counter was last reset
55      */

56     int readCounter = 0;
57
58     /**
59      * @param in the InputStream to be wrapped by this stream
60      * @param scheduler the TimeScheduler managing the trigger to be reset by this stream
61      * @param triggerName the name of the particular trigger to be reset by this stream
62      * @param lengthReset the number of bytes to be read in between trigger resets
63      */

64     public SchedulerNotifyInputStream(InputStream JavaDoc in,
65             TimeScheduler scheduler, String JavaDoc triggerName, int lengthReset) {
66         this.in = in;
67         this.scheduler = scheduler;
68         this.triggerName = triggerName;
69         this.lengthReset = lengthReset;
70
71         readCounter = 0;
72     }
73
74     /**
75      * Read an array of bytes from the stream
76      *
77      * @param b the array of bytes to read from the stream
78      * @param off the index in the array where we start writing
79      * @param len the number of bytes of the array to read
80      *
81      * @return the number of bytes read
82      *
83      * @throws IOException if an exception is encountered when reading
84      */

85     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
86         int l = in.read(b, off, len);
87         readCounter += l;
88
89         if (readCounter > lengthReset) {
90             readCounter -= lengthReset;
91             scheduler.resetTrigger(triggerName);
92         }
93
94         return l;
95     }
96
97     /**
98      * Read a byte from the stream
99      *
100      * @return the byte read from the stream
101      * @throws IOException if an exception is encountered when reading
102      */

103     public int read() throws IOException JavaDoc {
104         int b = in.read();
105         readCounter++;
106
107         if (readCounter > lengthReset) {
108             readCounter -= lengthReset;
109             scheduler.resetTrigger(triggerName);
110         }
111
112         return b;
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 JavaDoc {
121         in.close();
122     }
123 }
124
Popular Tags