001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042: import java.io.Reader;
043: import java.io.Writer;
044:
045: import com.quadcap.util.Debug;
046: import com.quadcap.util.Util;
047:
048: /**
049: * A filter input stream that tees off input into a log file, with a prefix
050: * identifying the stream.
051: *
052: * @author Stan Bailes
053: */
054:
055: public class LogReader extends Reader {
056: Reader in;
057: Writer log;
058: String prefix;
059: boolean bol = true;
060:
061: /**
062: * Construct a new log reader
063: *
064: * @param is the underlying reader
065: * @param log the log stream
066: * @param prefix a string identifying this log stream to be prefixed
067: * to every line output by this filter.
068: */
069: public LogReader(Reader in, Writer log, String prefix) {
070: this .in = in;
071: this .log = log;
072: this .prefix = prefix;
073: }
074:
075: /**
076: * Read (and log) a byte
077: *
078: * @exception IOException may be thrown
079: */
080: synchronized public int read() throws IOException {
081: int c = in.read();
082: if (c > 0) {
083: if (bol)
084: log.write(prefix);
085: log.write((char) c);
086: }
087: bol = (c == '\n');
088: return c;
089: }
090:
091: /**
092: * Read (and log) an array of bytes
093: */
094: public int read(char[] cbuf, int off, int len) throws IOException {
095: for (int i = 0; i < len; i++) {
096: int c = read();
097: if (c < 0)
098: return i > 0 ? i : -1;
099: cbuf[off + i] = (char) c;
100: }
101: return len > 0 ? len : -1;
102: }
103:
104: /**
105: * Close the underlying stream (but not the log stream!)
106: *
107: * @exception IOException may be thrown
108: */
109: public void close() throws IOException {
110: in.close();
111: }
112: }
|