KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > logger > XServletOutputStreamFacade


1 /*
2  * ====================================================================
3  *
4  * Copyright 1999-2002 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20
21 package org.apache.slide.webdav.logger;
22
23 import java.io.IOException JavaDoc;
24
25 import javax.servlet.ServletOutputStream JavaDoc;
26
27 import org.apache.slide.common.Domain;
28
29 /**
30  *
31  */

32 public class XServletOutputStreamFacade extends ServletOutputStream JavaDoc {
33
34     
35     public static final String JavaDoc DEFAULT_CHAR_ENCODING = "8859_1";
36     public static final byte[] bCRLF = (System.getProperty("line.separator")).getBytes();
37
38     ServletOutputStream JavaDoc prStream;
39     //PrintStream prStream;
40
XByteBuffer bb;
41     
42     /** Encoding - first time print() is used.
43     IMPORTANT: print() is _bad_, if you want to write Strings and mix
44     bytes you'd better use a real writer ( it's faster ).
45     But _don't_ use the servlet writer - since you'll not be able to write
46     bytes.
47     */

48     String JavaDoc enc;
49     
50     protected XServletOutputStreamFacade( ServletOutputStream JavaDoc soStream, String JavaDoc enc) {
51         Domain.debug("Create XServletOutputStreamFacade");
52 // this.prStream = new PrintStream( soStream, true );
53
// if ( DEFAULT_CHAR_ENCODING.equals(enc) ) {
54
// this.enc = null;
55
// } else {
56
// this.enc = enc;
57
// }
58
this.prStream = soStream;
59         bb = new XByteBuffer();
60     }
61
62     // -------------------- Write methods --------------------
63

64     public void write(int i) throws IOException JavaDoc {
65         Domain.debug("XServletOutputStreamFacade:write(int)");
66         bb.write(i);
67         prStream.write(i);
68     }
69
70     public void write(byte[] b) throws IOException JavaDoc {
71         Domain.debug("XServletOutputStreamFacade:write(byte[])");
72         write(b,0,b.length);
73     }
74     
75     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
76         Domain.debug("XServletOutputStreamFacade:write(byte[] b, int off, int len)");
77         bb.write( b, off, len );
78         prStream.write(b, off, len);
79     }
80
81     // -------------------- Servlet Output Stream methods --------------------
82

83     /** Alternate implementation for print, using String.getBytes(enc).
84     It seems to be a bit faster for small strings, but it's 10..20% slower
85     for larger texts ( nor very large - 5..10k )
86
87     That seems to be mostly because of byte b[] - the writer has an
88     internal ( and fixed ) buffer.
89
90     Please use getWriter() if you want to send strings.
91     */

92     public void print(String JavaDoc s) throws IOException JavaDoc {
93         Domain.debug("XServletOutputStreamFacade:print(" + s + " )");
94         if (s==null) {
95             s="null";
96         }
97         byte b[]=null;
98         if( enc==null) {
99             b=s.getBytes();
100         } else {
101             try {
102                 b=s.getBytes( enc );
103             } catch (java.io.UnsupportedEncodingException JavaDoc ex) {
104                 b=s.getBytes();
105             enc=null;
106             }
107         }
108         write( b );
109         prStream.print( s );
110     }
111     
112     public void println() throws IOException JavaDoc {
113         Domain.debug("XServletOutputStreamFacade:println()");
114         write(bCRLF);
115         prStream.println();
116     }
117
118     public void println(String JavaDoc s) throws IOException JavaDoc {
119         Domain.debug("XServletOutputStreamFacade:println(" + s + " )");
120         if (s==null) {
121             s="null";
122         }
123         byte b[]=null;
124         if( enc==null) {
125             b=s.getBytes();
126         } else {
127             try {
128                 b=s.getBytes( enc );
129             } catch (java.io.UnsupportedEncodingException JavaDoc ex) {
130                 b=s.getBytes();
131             enc=null;
132             }
133         }
134         write(b);
135         write(bCRLF);
136         prStream.println(s);
137     }
138
139     /** Will send the buffer to the client.
140      */

141     public void flush() throws IOException JavaDoc {
142         Domain.debug("XServletOutputStreamFacade:flush()");
143         prStream.flush();
144     }
145
146     public void close() throws IOException JavaDoc {
147         Domain.debug("XServletOutputStreamFacade:close()");
148         prStream.close();
149     }
150
151     /**
152      * Returns the content of the byte buffer as string.
153      *
154      * @return content of the byte buffer as string.
155      */

156     public String JavaDoc getOutputBuffer() throws IOException JavaDoc {
157         return bb.getBufferContent();
158     }
159
160     
161 }
162
163
Popular Tags