View Javadoc

1   //
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.
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  
19  package org.eclipse.jetty.io;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.Writer;
24  import java.nio.charset.Charset;
25  
26  
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).
32   * 
33   */
34  public class WriterOutputStream extends OutputStream
35  {
36      protected final Writer _writer;
37      protected final Charset _encoding;
38      private final byte[] _buf=new byte[1];
39      
40      /* ------------------------------------------------------------ */
41      public WriterOutputStream(Writer writer, String encoding)
42      {
43          _writer=writer;
44          _encoding=encoding==null?null:Charset.forName(encoding);
45      }
46      
47      /* ------------------------------------------------------------ */
48      public WriterOutputStream(Writer writer)
49      {
50          _writer=writer;
51          _encoding=null;
52      }
53  
54      /* ------------------------------------------------------------ */
55      @Override
56      public void close()
57          throws IOException
58      {
59          _writer.close();
60      }
61      
62      /* ------------------------------------------------------------ */
63      @Override
64      public void flush()
65          throws IOException
66      {
67          _writer.flush();
68      }
69      
70      /* ------------------------------------------------------------ */
71      @Override
72      public void write(byte[] b) 
73          throws IOException
74      {
75          if (_encoding==null)
76              _writer.write(new String(b));
77          else
78              _writer.write(new String(b,_encoding));
79      }
80      
81      /* ------------------------------------------------------------ */
82      @Override
83      public void write(byte[] b, int off, int len)
84          throws IOException
85      {
86          if (_encoding==null)
87              _writer.write(new String(b,off,len));
88          else
89              _writer.write(new String(b,off,len,_encoding));
90      }
91      
92      /* ------------------------------------------------------------ */
93      @Override
94      public synchronized void write(int b)
95          throws IOException
96      {
97          _buf[0]=(byte)b;
98          write(_buf);
99      }
100 }
101