View Javadoc
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.watchdog; 10 11 import java.io.IOException; 12 import java.io.OutputStream; 13 14 /*** 15 * This will reset the Watchdog each time a certain amount of data has 16 * been transferred. This allows us to keep the timeout settings low, while 17 * not timing out during large data transfers. 18 */ 19 public class BytesWrittenResetOutputStream extends OutputStream { 20 21 /*** 22 * The output stream wrapped by this method 23 */ 24 OutputStream out = null; 25 26 /*** 27 * The Watchdog to be reset every lengthReset bytes 28 */ 29 private Watchdog watchdog; 30 31 /*** 32 * The number of bytes that need to be written before the counter is reset. 33 */ 34 int lengthReset = 0; 35 36 /*** 37 * The number of bytes written since the counter was last reset 38 */ 39 int writtenCounter = 0; 40 41 public BytesWrittenResetOutputStream(OutputStream out, 42 Watchdog watchdog, 43 int lengthReset) { 44 this.out = out; 45 this.watchdog = watchdog; 46 this.lengthReset = lengthReset; 47 48 writtenCounter = 0; 49 } 50 51 /*** 52 * Write an array of bytes to the stream 53 * 54 * @param b the array of bytes to write to the stream 55 * @param off the index in the array where we start writing 56 * @param len the number of bytes of the array to write 57 * 58 * @throws IOException if an exception is encountered when writing 59 */ 60 public void write(byte[] b, int off, int len) throws IOException { 61 out.write(b, off, len); 62 writtenCounter += len; 63 64 if (writtenCounter > lengthReset) { 65 writtenCounter = 0; 66 watchdog.reset(); 67 } 68 } 69 70 /*** 71 * Write a byte to the stream 72 * 73 * @param b the byte to write to the stream 74 * 75 * @throws IOException if an exception is encountered when writing 76 */ 77 public void write(int b) throws IOException { 78 out.write(b); 79 writtenCounter++; 80 81 if (writtenCounter > lengthReset) { 82 writtenCounter = 0; 83 watchdog.reset(); 84 } 85 } 86 87 /*** 88 * Flush the stream 89 * 90 * @throws IOException if an exception is encountered when flushing 91 */ 92 public void flush() throws IOException { 93 out.flush(); 94 } 95 96 /*** 97 * Close the stream 98 * 99 * @throws IOException if an exception is encountered when closing 100 */ 101 public void close() throws IOException { 102 out.close(); 103 } 104 }

This page was automatically generated by Maven