2 // ========================================================================
3 // Copyright (c) 1995-2016 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.io;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.Writer;
24 import java.nio.charset.Charset;
27 /* ------------------------------------------------------------ */
28 /** Wrap a Writer as an OutputStream.
29 * When all you have is a Writer and only an OutputStream will do.
30 * Try not to use this as it indicates that your design is a dogs
31 * breakfast (JSP made me write it).
34 public class WriterOutputStream extends OutputStream
36 protected final Writer _writer;
37 protected final Charset _encoding;
38 private final byte[] _buf=new byte[1];
40 /* ------------------------------------------------------------ */
41 public WriterOutputStream(Writer writer, String encoding)
44 _encoding=encoding==null?null:Charset.forName(encoding);
47 /* ------------------------------------------------------------ */
48 public WriterOutputStream(Writer writer)
54 /* ------------------------------------------------------------ */
62 /* ------------------------------------------------------------ */
70 /* ------------------------------------------------------------ */
72 public void write(byte[] b)
76 _writer.write(new String(b));
78 _writer.write(new String(b,_encoding));
81 /* ------------------------------------------------------------ */
83 public void write(byte[] b, int off, int len)
87 _writer.write(new String(b,off,len));
89 _writer.write(new String(b,off,len,_encoding));
92 /* ------------------------------------------------------------ */
94 public synchronized void write(int b)