KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > watchdog > BytesWrittenResetOutputStream


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.watchdog;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * This will reset the Watchdog each time a certain amount of data has
26  * been transferred. This allows us to keep the timeout settings low, while
27  * not timing out during large data transfers.
28  */

29 public class BytesWrittenResetOutputStream extends OutputStream JavaDoc {
30
31     /**
32      * The output stream wrapped by this method
33      */

34     OutputStream JavaDoc out = null;
35
36     /**
37      * The Watchdog to be reset every lengthReset bytes
38      */

39     private Watchdog watchdog;
40
41     /**
42      * The number of bytes that need to be written before the counter is reset.
43      */

44     int lengthReset = 0;
45
46     /**
47      * The number of bytes written since the counter was last reset
48      */

49     int writtenCounter = 0;
50
51     public BytesWrittenResetOutputStream(OutputStream JavaDoc out,
52                                          Watchdog watchdog,
53                                          int lengthReset) {
54         this.out = out;
55         this.watchdog = watchdog;
56         this.lengthReset = lengthReset;
57
58         writtenCounter = 0;
59     }
60
61     /**
62      * Write an array of bytes to the stream
63      *
64      * @param b the array of bytes to write to the stream
65      * @param off the index in the array where we start writing
66      * @param len the number of bytes of the array to write
67      *
68      * @throws IOException if an exception is encountered when writing
69      */

70     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
71         out.write(b, off, len);
72         writtenCounter += len;
73
74         if (writtenCounter > lengthReset) {
75             writtenCounter = 0;
76             watchdog.reset();
77         }
78     }
79
80     /**
81      * Write a byte to the stream
82      *
83      * @param b the byte to write to the stream
84      *
85      * @throws IOException if an exception is encountered when writing
86      */

87     public void write(int b) throws IOException JavaDoc {
88         out.write(b);
89         writtenCounter++;
90
91         if (writtenCounter > lengthReset) {
92             writtenCounter = 0;
93             watchdog.reset();
94         }
95     }
96
97     /**
98      * Flush the stream
99      *
100      * @throws IOException if an exception is encountered when flushing
101      */

102     public void flush() throws IOException JavaDoc {
103         out.flush();
104     }
105
106     /**
107      * Close the stream
108      *
109      * @throws IOException if an exception is encountered when closing
110      */

111     public void close() throws IOException JavaDoc {
112         out.close();
113     }
114 }
115
Popular Tags