1 36 package org.columba.ristretto.log; 37 38 import java.io.FilterInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.io.OutputStream ; 42 43 49 public class LogInputStream extends FilterInputStream { 50 51 private static final byte[] PREFIX_STRING = { 'S', ':', ' ' }; 52 53 private static final int LINEEND = 0; 54 private static final int IN_LINE = 1; 55 56 private static final int MAX_LENGTH = 100 - PREFIX_STRING.length; 57 58 private int state; 59 private int line_length; 60 61 private OutputStream logOutputStream; 62 63 70 public LogInputStream(InputStream arg0, OutputStream log) { 71 super(arg0); 72 this.logOutputStream = log; 73 74 state = LINEEND; 75 76 line_length = 0; 77 } 78 79 85 public LogInputStream(InputStream arg0) { 86 this(arg0, System.out); 87 } 88 89 92 public int read() throws IOException { 93 int read = in.read(); 94 95 if( read == -1 ) return -1; 96 97 switch (state) { 98 case (LINEEND) : 99 { 100 line_length = 0; 101 state = IN_LINE; 102 103 logOutputStream.write(PREFIX_STRING); 105 106 break; 107 } 108 109 case (IN_LINE) : 110 { 111 line_length++; 112 113 if (read == '\n') { 115 state = LINEEND; 116 } else if (line_length == MAX_LENGTH) { 117 line_length = 0; 119 logOutputStream.write('\\'); 120 logOutputStream.write('\n'); 121 logOutputStream.write(PREFIX_STRING); 122 } 123 124 break; 125 } 126 } 127 128 logOutputStream.write(read); 129 130 return read; 131 } 132 133 136 public int read(byte[] arg0, int arg1, int arg2) throws IOException { 137 int next; 138 int i = 0; 139 140 for (; i < arg2; i++) { 141 next = read(); 142 if (next == -1) { 143 break; 144 } 145 arg0[arg1 + i] = (byte) next; 146 } 147 148 if( i == 0) return -1; 149 else return i; 150 } 151 152 155 public OutputStream getLogOutputStream() { 156 return logOutputStream; 157 } 158 159 163 public void setLogOutputStream(OutputStream logOutputStream) { 164 this.logOutputStream = logOutputStream; 165 } 166 167 } 168 | Popular Tags |