1 23 24 package com.sun.enterprise.naming; 25 26 import java.io.*; 27 import java.util.logging.*; 28 29 30 33 public class LogOutputStream extends OutputStream { 34 protected Logger logger; 35 protected Level level; 36 37 private int lastb = -1; 38 private byte[] buf = new byte[80]; 39 private int pos = 0; 40 41 44 public LogOutputStream(String facility) { 45 this(facility, Level.FINE); 46 } 47 48 51 public LogOutputStream(String facility, Level level) { 52 logger = Logger.getLogger(facility); 53 this.level = level; 54 } 55 56 public void write(int b) throws IOException { 57 if (!logger.isLoggable(level)) 58 return; 59 60 if (b == '\r') { 61 logBuf(); 62 } else if (b == '\n') { 63 if (lastb != '\r') 64 logBuf(); 65 } else { 66 expandCapacity(1); 67 buf[pos++] = (byte)b; 68 } 69 lastb = b; 70 } 71 72 public void write(byte b[]) throws IOException { 73 write(b, 0, b.length); 74 } 75 76 public void write(byte b[], int off, int len) throws IOException { 77 int start = off; 78 79 if (!logger.isLoggable(level)) 80 return; 81 len += off; 82 for (int i = start; i < len ; i++) { 83 if (b[i] == '\r') { 84 expandCapacity(i - start); 85 System.arraycopy(b, start, buf, pos, i - start); 86 pos += i - start; 87 logBuf(); 88 start = i + 1; 89 } else if (b[i] == '\n') { 90 if (lastb != '\r') { 91 expandCapacity(i - start); 92 System.arraycopy(b, start, buf, pos, i - start); 93 pos += i - start; 94 logBuf(); 95 } 96 start = i + 1; 97 } 98 lastb = b[i]; 99 } 100 if ((len - start) > 0) { 101 expandCapacity(len - start); 102 System.arraycopy(b, start, buf, pos, len - start); 103 pos += len - start; 104 } 105 } 106 107 111 protected void log(String msg) { 112 logger.log(level, msg); 113 } 114 115 118 private void logBuf() { 119 String msg = new String (buf, 0, pos); 120 pos = 0; 121 log(msg); 122 } 123 124 128 private void expandCapacity(int len) { 129 while (pos + len > buf.length) { 130 byte[] nb = new byte[buf.length * 2]; 131 System.arraycopy(buf, 0, nb, 0, pos); 132 buf = nb; 133 } 134 } 135 } 136 | Popular Tags |