View Javadoc

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