001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.util.watchdog;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: /**
024: * This will reset the Watchdog each time a certain amount of data has
025: * been transferred. This allows us to keep the timeout settings low, while
026: * not timing out during large data transfers.
027: */
028: public class BytesReadResetInputStream extends InputStream {
029:
030: /**
031: * The wrapped InputStream
032: */
033: private InputStream in = null;
034:
035: /**
036: * The Watchdog to be reset every lengthReset bytes
037: */
038: private Watchdog watchdog;
039:
040: /**
041: * The number of bytes that need to be read before the counter is reset.
042: */
043: private int lengthReset = 0;
044:
045: /**
046: * The number of bytes read since the counter was last reset
047: */
048: int readCounter = 0;
049:
050: /**
051: * @param in the InputStream to be wrapped by this stream
052: * @param watchdog the watchdog to be reset
053: * @param lengthReset the number of bytes to be read in between trigger resets
054: */
055: public BytesReadResetInputStream(InputStream in, Watchdog watchdog,
056: int lengthReset) {
057: this .in = in;
058: this .watchdog = watchdog;
059: this .lengthReset = lengthReset;
060:
061: readCounter = 0;
062: }
063:
064: /**
065: * Read an array of bytes from the stream
066: *
067: * @param b the array of bytes to read from the stream
068: * @param off the index in the array where we start writing
069: * @param len the number of bytes of the array to read
070: *
071: * @return the number of bytes read
072: *
073: * @throws IOException if an exception is encountered when reading
074: */
075: public int read(byte[] b, int off, int len) throws IOException {
076: int l = in.read(b, off, len);
077: readCounter += l;
078:
079: if (readCounter > lengthReset) {
080: readCounter = 0;
081: watchdog.reset();
082: }
083:
084: return l;
085: }
086:
087: /**
088: * Read a byte from the stream
089: *
090: * @return the byte read from the stream
091: * @throws IOException if an exception is encountered when reading
092: */
093: public int read() throws IOException {
094: int b = in.read();
095: readCounter++;
096:
097: if (readCounter > lengthReset) {
098: readCounter = 0;
099: watchdog.reset();
100: }
101:
102: return b;
103: }
104:
105: /**
106: * Close the stream
107: *
108: * @throws IOException if an exception is encountered when closing
109: */
110: public void close() throws IOException {
111: in.close();
112: }
113: }
|