View Javadoc

1   /********************************************************************************
2    * $Id: WriterOutputStream.java 395 2007-09-09 17:40:33Z pgmjsd $
3    * $Author: pgmjsd $
4    * $Date: 2007-09-09 13:40:33 -0400 (Sun, 09 Sep 2007) $
5    *
6    * Copyright 2002 - YAJUL Developers, Joshua Davis, Kent Vogel.
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy
9    * of this software and associated documentation files (the "Software"), to deal
10   * in the Software without restriction, including without limitation the rights
11   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12   * copies of the Software, and to permit persons to whom the Software is
13   * furnished to do so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in
16   * all copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24   * THE SOFTWARE.
25   *
26   ******************************************************************************/
27  
28  /*
29  * Created by IntelliJ IDEA.
30  * User: josh
31  * Date: Sep 15, 2002
32  * Time: 1:46:23 PM
33  */
34  package org.yajul.io;
35  
36  import java.io.IOException;
37  import java.io.OutputStream;
38  import java.io.Writer;
39  
40  /***
41   * Adapter that converts a Writer into an OutputStream.  This is often usefull
42   * when there is a library that needs an output stream, but the caller has
43   * a Writer.
44   *
45   * @author Joshua Davis
46   */
47  public class WriterOutputStream extends OutputStream {
48      private Writer writer;
49  
50      /***
51       * Creates a new WriterOutputStream based on the given Writer.
52       *
53       * @param writer The writer that this OutputStream will use to write things to.
54       */
55      public WriterOutputStream(Writer writer) {
56          this.writer = writer;
57      }
58  
59      /***
60       * Writes the specified byte to this output stream. The general
61       * contract for <code>write</code> is that one byte is written
62       * to the output stream. The byte to be written is the eight
63       * low-order bits of the argument <code>b</code>. The 24
64       * high-order bits of <code>b</code> are ignored.
65       * <p/>
66       * Subclasses of <code>OutputStream</code> must provide an
67       * implementation for this method.
68       *
69       * @param b the <code>byte</code>.
70       * @throws IOException if an I/O error occurs. In particular,
71       *                     an <code>IOException</code> may be thrown if the
72       *                     output stream has been closed.
73       */
74      public void write(int b) throws IOException {
75          writer.write(b);
76      }
77  
78      /***
79       * Writes <code>b.length</code> bytes from the specified byte array
80       * to this output stream. The general contract for <code>write(b)</code>
81       * is that it should have exactly the same effect as the call
82       * <code>write(b, 0, b.length)</code>.
83       *
84       * @param b the data.
85       * @throws IOException if an I/O error occurs.
86       * @see java.io.OutputStream#write(byte[],int,int)
87       */
88      public void write(byte b[]) throws IOException {
89          super.write(b);
90      }
91  
92      /***
93       * Writes <code>len</code> bytes from the specified byte array
94       * starting at offset <code>off</code> to this output stream.
95       * The general contract for <code>write(b, off, len)</code> is that
96       * some of the bytes in the array <code>b</code> are written to the
97       * output stream in order; element <code>b[off]</code> is the first
98       * byte written and <code>b[off+len-1]</code> is the last byte written
99       * by this operation.
100      * <p/>
101      * The <code>write</code> method of <code>OutputStream</code> calls
102      * the write method of one argument on each of the bytes to be
103      * written out. Subclasses are encouraged to override this method and
104      * provide a more efficient implementation.
105      * <p/>
106      * If <code>b</code> is <code>null</code>, a
107      * <code>NullPointerException</code> is thrown.
108      * <p/>
109      * If <code>off</code> is negative, or <code>len</code> is negative, or
110      * <code>off+len</code> is greater than the length of the array
111      * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
112      *
113      * @param b   the data.
114      * @param off the start offset in the data.
115      * @param len the number of bytes to write.
116      * @throws IOException if an I/O error occurs. In particular,
117      *                     an <code>IOException</code> is thrown if the output
118      *                     stream is closed.
119      */
120     public void write(byte b[], int off, int len) throws IOException {
121         super.write(b, off, len);
122     }
123 
124     /***
125      * Closes this output stream and releases any system resources
126      * associated with this stream. The general contract of <code>close</code>
127      * is that it closes the output stream. A closed stream cannot perform
128      * output operations and cannot be reopened.
129      * <p/>
130      * The <code>close</code> method of <code>OutputStream</code> does nothing.
131      *
132      * @throws IOException if an I/O error occurs.
133      */
134     public void close() throws IOException {
135         super.close();
136         writer.close();
137     }
138 
139     /***
140      * Flushes this output stream and forces any buffered output bytes
141      * to be written out. The general contract of <code>flush</code> is
142      * that calling it is an indication that, if any bytes previously
143      * written have been buffered by the implementation of the output
144      * stream, such bytes should immediately be written to their
145      * intended destination.
146      * <p/>
147      * The <code>flush</code> method of <code>OutputStream</code> does nothing.
148      *
149      * @throws IOException if an I/O error occurs.
150      */
151     public void flush() throws IOException {
152         super.flush();
153         writer.flush();
154     }
155 }