1 36 package org.columba.ristretto.log; 37 38 import java.io.FilterOutputStream ; 39 import java.io.IOException ; 40 import java.io.OutputStream ; 41 42 49 public class LogOutputStream extends FilterOutputStream { 50 private static final byte[] PREFIX_STRING = { 'C', ':', ' ' }; 51 52 private static final int LINEEND = 0; 53 private static final int IN_LINE = 1; 54 55 private static final int MAX_LENGTH = 100 - PREFIX_STRING.length; 56 57 private int state; 58 private int line_length; 59 60 private OutputStream logOutputStream; 61 62 68 public LogOutputStream(OutputStream arg0, OutputStream logStream) { 69 super(arg0); 70 this.logOutputStream =logStream; 71 } 72 73 78 public LogOutputStream(OutputStream arg0) { 79 this( arg0, System.out ); 80 } 81 82 83 86 public void write(byte[] arg0, int arg1, int arg2) throws IOException { 87 for( int i=arg1; i<arg2+arg1; i++) { 88 write((int)(0x0ff & arg0[i])); 89 } 90 } 91 92 95 public void write(int write) throws IOException { 96 97 switch( state ) { 98 case( LINEEND ) : { 99 line_length = 0; 100 state = IN_LINE; 101 102 if( write != -1 ) { 104 logOutputStream.write(PREFIX_STRING); 105 } 106 107 break; 108 } 109 110 case( IN_LINE ) : { 111 line_length++; 112 113 if( write == '\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 if( write != -1 ) { 129 logOutputStream.write(write); 130 } 131 132 out.write(write); 133 } 134 135 } 136 | Popular Tags |