KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > naming > LogOutputStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.naming;
25
26 import java.io.*;
27 import java.util.logging.*;
28
29
30 /**
31  * Capture output lines and send them to the system error log.
32  */

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     /**
42      * Log to the specified facility at the default FINE level.
43      */

44     public LogOutputStream(String JavaDoc facility) {
45     this(facility, Level.FINE);
46     }
47
48     /**
49      * Log to the specified facility at the specified level.
50      */

51     public LogOutputStream(String JavaDoc 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     /**
108      * Log the specified message.
109      * Can be overridden by subclass to do different logging.
110      */

111     protected void log(String JavaDoc msg) {
112     logger.log(level, msg);
113     }
114
115     /**
116      * Convert the buffer to a string and log it.
117      */

118     private void logBuf() {
119     String JavaDoc msg = new String JavaDoc(buf, 0, pos);
120     pos = 0;
121     log(msg);
122     }
123
124     /**
125      * Ensure that the buffer can hold at least len bytes
126      * beyond the current position.
127      */

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