1//
2//  ========================================================================
3//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4//  ------------------------------------------------------------------------
5//  All rights reserved. This program and the accompanying materials
6//  are made available under the terms of the Eclipse Public License v1.0
7//  and Apache License v2.0 which accompanies this distribution.
8//
9//      The Eclipse Public License is available at
10//      http://www.eclipse.org/legal/epl-v10.html
11//
12//      The Apache License v2.0 is available at
13//      http://www.opensource.org/licenses/apache2.0.php
14//
15//  You may elect to redistribute this code under either of these licenses.
16//  ========================================================================
17//
18
19package org.eclipse.jetty.io;
20
21import java.io.IOException;
22import java.io.OutputStream;
23import java.io.Writer;
24
25
26/* ------------------------------------------------------------ */
27/** Wrap a Writer as an OutputStream.
28 * When all you have is a Writer and only an OutputStream will do.
29 * Try not to use this as it indicates that your design is a dogs
30 * breakfast (JSP made me write it).
31 *
32 */
33public class WriterOutputStream extends OutputStream
34{
35    protected final Writer _writer;
36    protected final String _encoding;
37    private final byte[] _buf=new byte[1];
38
39    /* ------------------------------------------------------------ */
40    public WriterOutputStream(Writer writer, String encoding)
41    {
42        _writer=writer;
43        _encoding=encoding;
44    }
45
46    /* ------------------------------------------------------------ */
47    public WriterOutputStream(Writer writer)
48    {
49        _writer=writer;
50        _encoding=null;
51    }
52
53    /* ------------------------------------------------------------ */
54    @Override
55    public void close()
56        throws IOException
57    {
58        _writer.close();
59    }
60
61    /* ------------------------------------------------------------ */
62    @Override
63    public void flush()
64        throws IOException
65    {
66        _writer.flush();
67    }
68
69    /* ------------------------------------------------------------ */
70    @Override
71    public void write(byte[] b)
72        throws IOException
73    {
74        if (_encoding==null)
75            _writer.write(new String(b));
76        else
77            _writer.write(new String(b,_encoding));
78    }
79
80    /* ------------------------------------------------------------ */
81    @Override
82    public void write(byte[] b, int off, int len)
83        throws IOException
84    {
85        if (_encoding==null)
86            _writer.write(new String(b,off,len));
87        else
88            _writer.write(new String(b,off,len,_encoding));
89    }
90
91    /* ------------------------------------------------------------ */
92    @Override
93    public synchronized void write(int b)
94        throws IOException
95    {
96        _buf[0]=(byte)b;
97        write(_buf);
98    }
99}
100
101