1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 21 22 package org.apache.james.util.watchdog; 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 27 /** 28 * This will reset the Watchdog each time a certain amount of data has 29 * been transferred. This allows us to keep the timeout settings low, while 30 * not timing out during large data transfers. 31 */ 32 public class BytesReadResetInputStream extends InputStream { 33 34 /** 35 * The wrapped InputStream 36 */ 37 private InputStream in = null; 38 39 /** 40 * The Watchdog to be reset every lengthReset bytes 41 */ 42 private Watchdog watchdog; 43 44 /** 45 * The number of bytes that need to be read before the counter is reset. 46 */ 47 private int lengthReset = 0; 48 49 /** 50 * The number of bytes read since the counter was last reset 51 */ 52 int readCounter = 0; 53 54 /** 55 * @param in the InputStream to be wrapped by this stream 56 * @param watchdog the watchdog to be reset 57 * @param lengthReset the number of bytes to be read in between trigger resets 58 */ 59 public BytesReadResetInputStream(InputStream in, 60 Watchdog watchdog, 61 int lengthReset) { 62 this.in = in; 63 this.watchdog = watchdog; 64 this.lengthReset = lengthReset; 65 66 readCounter = 0; 67 } 68 69 /** 70 * Read an array of bytes from the stream 71 * 72 * @param b the array of bytes to read from the stream 73 * @param off the index in the array where we start writing 74 * @param len the number of bytes of the array to read 75 * 76 * @return the number of bytes read 77 * 78 * @throws IOException if an exception is encountered when reading 79 */ 80 public int read(byte[] b, int off, int len) throws IOException { 81 int l = in.read(b, off, len); 82 readCounter += l; 83 84 if (readCounter > lengthReset) { 85 readCounter = 0; 86 watchdog.reset(); 87 } 88 89 return l; 90 } 91 92 /** 93 * Read a byte from the stream 94 * 95 * @return the byte read from the stream 96 * @throws IOException if an exception is encountered when reading 97 */ 98 public int read() throws IOException { 99 int b = in.read(); 100 readCounter++; 101 102 if (readCounter > lengthReset) { 103 readCounter = 0; 104 watchdog.reset(); 105 } 106 107 return b; 108 } 109 110 /** 111 * Close the stream 112 * 113 * @throws IOException if an exception is encountered when closing 114 */ 115 public void close() throws IOException { 116 in.close(); 117 } 118 }