KickJava   Java API By Example, From Geeks To Geeks.

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


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.OutputStream 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 SchedulerNotifyOutputStream extends OutputStream JavaDoc {
32
33     /**
34      * The output stream wrapped by this method
35      */

36     OutputStream JavaDoc out = null;
37
38     /**
39      * The scheduler used by this class to timeout
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 written before the counter is reset.
50      */

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

56     int writtenCounter = 0;
57
58     public SchedulerNotifyOutputStream(OutputStream JavaDoc out,
59             TimeScheduler scheduler, String JavaDoc triggerName, int lengthReset) {
60         this.out = out;
61         this.scheduler = scheduler;
62         this.triggerName = triggerName;
63         this.lengthReset = lengthReset;
64
65         writtenCounter = 0;
66     }
67
68     /**
69      * Write an array of bytes to the stream
70      *
71      * @param b the array of bytes to write to the stream
72      * @param off the index in the array where we start writing
73      * @param len the number of bytes of the array to write
74      *
75      * @throws IOException if an exception is encountered when writing
76      */

77     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
78         out.write(b, off, len);
79         writtenCounter += len;
80
81         if (writtenCounter > lengthReset) {
82             writtenCounter -= lengthReset;
83             scheduler.resetTrigger(triggerName);
84         }
85     }
86
87     /**
88      * Write a byte to the stream
89      *
90      * @param b the byte to write to the stream
91      *
92      * @throws IOException if an exception is encountered when writing
93      */

94     public void write(int b) throws IOException JavaDoc {
95         out.write(b);
96         writtenCounter++;
97
98         if (writtenCounter > lengthReset) {
99             writtenCounter -= lengthReset;
100             scheduler.resetTrigger(triggerName);
101         }
102     }
103
104     /**
105      * Flush the stream
106      *
107      * @throws IOException if an exception is encountered when flushing
108      */

109     public void flush() throws IOException JavaDoc {
110         out.flush();
111     }
112
113     /**
114      * Close the stream
115      *
116      * @throws IOException if an exception is encountered when closing
117      */

118     public void close() throws IOException JavaDoc {
119         out.close();
120     }
121 }
122
Popular Tags