src/org/objectweb/cjdbc/common/util/LoggingOutputStream.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.common.util; 00026 00027 import java.io.IOException; 00028 import java.io.OutputStream; 00029 00030 import org.apache.log4j.Category; 00031 import org.apache.log4j.Priority; 00032 00045 public class LoggingOutputStream extends OutputStream 00046 { 00047 protected static final String LINE_SEPERATOR = System 00048 .getProperty("line.separator"); 00052 protected boolean hasBeenClosed = false; 00056 protected byte[] buf; 00062 protected int count; 00066 private int bufLength; 00070 public static final int DEFAULT_BUFFER_LENGTH = 2048; 00074 protected Category category; 00078 protected Priority priority; 00079 00080 private LoggingOutputStream() 00081 { 00082 // illegal 00083 } 00084 00092 public LoggingOutputStream(Category cat, Priority priority) 00093 throws IllegalArgumentException 00094 { 00095 if (cat == null) 00096 { 00097 throw new IllegalArgumentException("cat == null"); 00098 } 00099 if (priority == null) 00100 { 00101 throw new IllegalArgumentException("priority == null"); 00102 } 00103 this.priority = priority; 00104 category = cat; 00105 bufLength = DEFAULT_BUFFER_LENGTH; 00106 buf = new byte[DEFAULT_BUFFER_LENGTH]; 00107 count = 0; 00108 } 00109 00116 public void close() 00117 { 00118 flush(); 00119 hasBeenClosed = true; 00120 } 00121 00132 public void write(final int b) throws IOException 00133 { 00134 if (hasBeenClosed) 00135 { 00136 throw new IOException("The stream has been closed."); 00137 } 00138 // don't log nulls 00139 if (b == 0) 00140 { 00141 return; 00142 } 00143 // would this be writing past the buffer? 00144 if (count == bufLength) 00145 { 00146 // grow the buffer 00147 final int newBufLength = bufLength + DEFAULT_BUFFER_LENGTH; 00148 final byte[] newBuf = new byte[newBufLength]; 00149 System.arraycopy(buf, 0, newBuf, 0, bufLength); 00150 buf = newBuf; 00151 bufLength = newBufLength; 00152 } 00153 buf[count] = (byte) b; 00154 count++; 00155 } 00156 00164 public void flush() 00165 { 00166 if (count == 0) 00167 { 00168 return; 00169 } 00170 // don't print out blank lines; flushing from PrintStream puts out these 00171 if (count == LINE_SEPERATOR.length()) 00172 { 00173 if (((char) buf[0]) == LINE_SEPERATOR.charAt(0) && ((count == 1) || // <- 00174 // Unix 00175 // & 00176 // Mac, 00177 // -> 00178 // Windows 00179 ((count == 2) && ((char) buf[1]) == LINE_SEPERATOR.charAt(1)))) 00180 { 00181 reset(); 00182 return; 00183 } 00184 } 00185 final byte[] theBytes = new byte[count]; 00186 System.arraycopy(buf, 0, theBytes, 0, count); 00187 00188 // ADDED: We don't want blank lines at all 00189 String bytes = new String(theBytes).trim(); 00190 int line = -1; 00191 while((line = bytes.indexOf(LINE_SEPERATOR))!=-1) 00192 { 00193 bytes = bytes.substring(0, line) + bytes.substring(line+LINE_SEPERATOR.length()); 00194 } 00195 // END ADDED 00196 00197 category.log(priority, bytes); 00198 reset(); 00199 } 00200 00201 private void reset() 00202 { 00203 // not resetting the buffer -- assuming that if it grew that it 00204 // will likely grow similarly again 00205 count = 0; 00206 } 00207 }

CJDBCversion1.0.4に対してTue Oct 12 15:15:58 2004に生成されました。 doxygen 1.3.8