1 package com.quadcap.io; 2 3 40 41 import java.io.IOException ; 42 import java.io.Reader ; 43 import java.io.Writer ; 44 45 import com.quadcap.util.Debug; 46 import com.quadcap.util.Util; 47 48 54 55 public class LogReader extends Reader { 56 Reader in; 57 Writer log; 58 String prefix; 59 boolean bol = true; 60 61 69 public LogReader(Reader in, Writer log, String prefix) { 70 this.in = in; 71 this.log = log; 72 this.prefix = prefix; 73 } 74 75 80 synchronized public int read() throws IOException { 81 int c = in.read(); 82 if (c > 0) { 83 if (bol) log.write(prefix); 84 log.write((char)c); 85 } 86 bol = (c == '\n'); 87 return c; 88 } 89 90 93 public int read(char[] cbuf, int off, int len) throws IOException { 94 for (int i = 0; i < len; i++) { 95 int c = read(); 96 if (c < 0) return i > 0 ? i : -1; 97 cbuf[off + i] = (char)c; 98 } 99 return len > 0 ? len : -1; 100 } 101 102 107 public void close() throws IOException { 108 in.close(); 109 } 110 } 111 | Popular Tags |