KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > WriterOutputStream


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

16 package org.directwebremoting.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Writer JavaDoc;
20
21 import javax.servlet.ServletOutputStream JavaDoc;
22
23 /**
24  * This is not the evil hack you are looking for.
25  * @author Joe Walker [joe at getahead dot ltd dot uk]
26  */

27 public final class WriterOutputStream extends ServletOutputStream JavaDoc
28 {
29     /**
30      * ctor using platform default encoding
31      * @param writer
32      */

33     public WriterOutputStream(Writer JavaDoc writer)
34     {
35         this.writer = writer;
36     }
37
38     /**
39      * ctor that allows us to specify how strings are created
40      * @param writer
41      * @param encoding
42      */

43     public WriterOutputStream(Writer JavaDoc writer, String JavaDoc encoding)
44     {
45         this.writer = writer;
46         this.encoding = encoding;
47     }
48
49     /* (non-Javadoc)
50      * @see javax.servlet.ServletOutputStream#print(java.lang.String)
51      */

52     public void print(String JavaDoc s) throws IOException JavaDoc
53     {
54         writer.write(s);
55     }
56
57     /* (non-Javadoc)
58      * @see java.io.OutputStream#write(byte[])
59      */

60     public void write(byte[] ba) throws IOException JavaDoc
61     {
62         if (encoding == null)
63         {
64             writer.write(new String JavaDoc(ba));
65         }
66         else
67         {
68             writer.write(new String JavaDoc(ba, encoding));
69         }
70     }
71
72     /* (non-Javadoc)
73      * @see java.io.OutputStream#write(byte[], int, int)
74      */

75     public void write(byte[] ba, int off, int len) throws IOException JavaDoc
76     {
77         if (encoding == null)
78         {
79             writer.write(new String JavaDoc(ba, off, len));
80         }
81         else
82         {
83             writer.write(new String JavaDoc(ba, off, len, encoding));
84         }
85     }
86
87     /* (non-Javadoc)
88      * @see java.io.OutputStream#write(int)
89      */

90     public synchronized void write(int bite) throws IOException JavaDoc
91     {
92         buffer[0] = (byte) bite;
93         write(buffer);
94     }
95
96     /* (non-Javadoc)
97      * @see java.io.OutputStream#close()
98      */

99     public void close() throws IOException JavaDoc
100     {
101         if (writer != null)
102         {
103             writer.close();
104             writer = null;
105             encoding = null;
106         }
107     }
108
109     /* (non-Javadoc)
110      * @see java.io.OutputStream#flush()
111      */

112     public void flush() throws IOException JavaDoc
113     {
114         writer.flush();
115     }
116
117     /**
118      * The destination of all our printing
119      */

120     private Writer JavaDoc writer;
121
122     /**
123      * What string encoding should we use
124      */

125     private String JavaDoc encoding = null;
126
127     /**
128      * Buffer for write(int)
129      */

130     private byte[] buffer = new byte[1];
131 }
132
Popular Tags