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.OutputStream;
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 SchedulerNotifyOutputStream extends OutputStream {
22
23 /***
24 * The output stream wrapped by this method
25 */
26 OutputStream out = null;
27
28 /***
29 * The scheduler used by this class to timeout
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 written before the counter is reset.
40 */
41 int lengthReset = 0;
42
43 /***
44 * The number of bytes written since the counter was last reset
45 */
46 int writtenCounter = 0;
47
48 public SchedulerNotifyOutputStream(OutputStream out,
49 TimeScheduler scheduler, String triggerName, int lengthReset) {
50 this.out = out;
51 this.scheduler = scheduler;
52 this.triggerName = triggerName;
53 this.lengthReset = lengthReset;
54
55 writtenCounter = 0;
56 }
57
58 /***
59 * Write an array of bytes to the stream
60 *
61 * @param b the array of bytes to write to the stream
62 * @param off the index in the array where we start writing
63 * @param len the number of bytes of the array to write
64 *
65 * @throws IOException if an exception is encountered when writing
66 */
67 public void write(byte[] b, int off, int len) throws IOException {
68 out.write(b, off, len);
69 writtenCounter += len;
70
71 if (writtenCounter > lengthReset) {
72 writtenCounter -= lengthReset;
73 scheduler.resetTrigger(triggerName);
74 }
75 }
76
77 /***
78 * Write a byte to the stream
79 *
80 * @param b the byte to write to the stream
81 *
82 * @throws IOException if an exception is encountered when writing
83 */
84 public void write(int b) throws IOException {
85 out.write(b);
86 writtenCounter++;
87
88 if (writtenCounter > lengthReset) {
89 writtenCounter -= lengthReset;
90 scheduler.resetTrigger(triggerName);
91 }
92 }
93
94 /***
95 * Flush the stream
96 *
97 * @throws IOException if an exception is encountered when flushing
98 */
99 public void flush() throws IOException {
100 out.flush();
101 }
102
103 /***
104 * Close the stream
105 *
106 * @throws IOException if an exception is encountered when closing
107 */
108 public void close() throws IOException {
109 out.close();
110 }
111 }
This page was automatically generated by Maven