01: /*
02: * @(#)LogReader.java 1.2 04/12/06
03: *
04: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution of
07: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.tools;
10:
11: import java.io.FileWriter;
12: import java.io.FilterReader;
13: import java.io.IOException;
14: import java.io.Reader;
15:
16: class LogReader extends FilterReader {
17: FileWriter writer;
18:
19: LogReader(Reader in, String path) throws IOException {
20: super (in);
21: this .writer = new FileWriter(path);
22:
23: java.lang.Runtime.getRuntime().addShutdownHook(new Thread() {
24: public void run() {
25: try {
26: writer.flush();
27: writer.close();
28: } catch (IOException e) {
29: }
30: }
31: });
32: }
33:
34: public int read() throws IOException {
35: int c = super .read();
36: writer.write(c);
37: writer.flush();
38: return c;
39: }
40:
41: public int read(char cbuf[], int off, int len) throws IOException {
42: int n = super .read(cbuf, off, len);
43: if (n > 0) {
44: writer.write(cbuf, off, n);
45: writer.flush();
46: }
47: return n;
48: }
49:
50: public void close() throws IOException {
51: super.close();
52: writer.close();
53: }
54: }
|