KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: WriterOutputStream.java,v 1.3 2004/05/09 20:33:05 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.Writer JavaDoc;
21
22
23 /* ------------------------------------------------------------ */
24 /** Wrap a Writer as an OutputStream.
25  * When all you have is a Writer and only an OutputStream will do.
26  * Try not to use this as it indicates that your design is a dogs
27  * breakfast (JSP made me write it).
28  * @version $Id: WriterOutputStream.java,v 1.3 2004/05/09 20:33:05 gregwilkins Exp $
29  * @author Greg Wilkins (gregw)
30  */

31 public class WriterOutputStream extends OutputStream JavaDoc
32 {
33     protected Writer JavaDoc _writer;
34     protected String JavaDoc _encoding;
35     private byte[] _buf=new byte[1];
36     
37     /* ------------------------------------------------------------ */
38     public WriterOutputStream(Writer JavaDoc writer, String JavaDoc encoding)
39     {
40         _writer=writer;
41         _encoding=encoding;
42     }
43     
44     /* ------------------------------------------------------------ */
45     public WriterOutputStream(Writer JavaDoc writer)
46     {
47         _writer=writer;
48     }
49
50     /* ------------------------------------------------------------ */
51     public void close()
52         throws IOException JavaDoc
53     {
54         _writer.close();
55         _writer=null;
56         _encoding=null;
57     }
58     
59     /* ------------------------------------------------------------ */
60     public void flush()
61         throws IOException JavaDoc
62     {
63         _writer.flush();
64     }
65     
66     /* ------------------------------------------------------------ */
67     public void write(byte[] b)
68         throws IOException JavaDoc
69     {
70         if (_encoding==null)
71             _writer.write(new String JavaDoc(b));
72         else
73             _writer.write(new String JavaDoc(b,_encoding));
74     }
75     
76     /* ------------------------------------------------------------ */
77     public void write(byte[] b, int off, int len)
78         throws IOException JavaDoc
79     {
80         if (_encoding==null)
81             _writer.write(new String JavaDoc(b,off,len));
82         else
83             _writer.write(new String JavaDoc(b,off,len,_encoding));
84     }
85     
86     /* ------------------------------------------------------------ */
87     public synchronized void write(int b)
88         throws IOException JavaDoc
89     {
90         _buf[0]=(byte)b;
91         write(_buf);
92     }
93 }
94
95
Popular Tags