KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > FilterOutputStream


1 /*
2  * @(#)FilterOutputStream.java 1.31 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * This class is the superclass of all classes that filter output
12  * streams. These streams sit on top of an already existing output
13  * stream (the <i>underlying</i> output stream) which it uses as its
14  * basic sink of data, but possibly transforming the data along the
15  * way or providing additional functionality.
16  * <p>
17  * The class <code>FilterOutputStream</code> itself simply overrides
18  * all methods of <code>OutputStream</code> with versions that pass
19  * all requests to the underlying output stream. Subclasses of
20  * <code>FilterOutputStream</code> may further override some of these
21  * methods as well as provide additional methods and fields.
22  *
23  * @author Jonathan Payne
24  * @version 1.31, 12/19/03
25  * @since JDK1.0
26  */

27 public
28 class FilterOutputStream extends OutputStream JavaDoc {
29     /**
30      * The underlying output stream to be filtered.
31      */

32     protected OutputStream JavaDoc out;
33
34     /**
35      * Creates an output stream filter built on top of the specified
36      * underlying output stream.
37      *
38      * @param out the underlying output stream to be assigned to
39      * the field <tt>this.out</tt> for later use, or
40      * <code>null</code> if this instance is to be
41      * created without an underlying stream.
42      */

43     public FilterOutputStream(OutputStream JavaDoc out) {
44     this.out = out;
45     }
46
47     /**
48      * Writes the specified <code>byte</code> to this output stream.
49      * <p>
50      * The <code>write</code> method of <code>FilterOutputStream</code>
51      * calls the <code>write</code> method of its underlying output stream,
52      * that is, it performs <tt>out.write(b)</tt>.
53      * <p>
54      * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
55      *
56      * @param b the <code>byte</code>.
57      * @exception IOException if an I/O error occurs.
58      */

59     public void write(int b) throws IOException JavaDoc {
60     out.write(b);
61     }
62
63     /**
64      * Writes <code>b.length</code> bytes to this output stream.
65      * <p>
66      * The <code>write</code> method of <code>FilterOutputStream</code>
67      * calls its <code>write</code> method of three arguments with the
68      * arguments <code>b</code>, <code>0</code>, and
69      * <code>b.length</code>.
70      * <p>
71      * Note that this method does not call the one-argument
72      * <code>write</code> method of its underlying stream with the single
73      * argument <code>b</code>.
74      *
75      * @param b the data to be written.
76      * @exception IOException if an I/O error occurs.
77      * @see java.io.FilterOutputStream#write(byte[], int, int)
78      */

79     public void write(byte b[]) throws IOException JavaDoc {
80     write(b, 0, b.length);
81     }
82
83     /**
84      * Writes <code>len</code> bytes from the specified
85      * <code>byte</code> array starting at offset <code>off</code> to
86      * this output stream.
87      * <p>
88      * The <code>write</code> method of <code>FilterOutputStream</code>
89      * calls the <code>write</code> method of one argument on each
90      * <code>byte</code> to output.
91      * <p>
92      * Note that this method does not call the <code>write</code> method
93      * of its underlying input stream with the same arguments. Subclasses
94      * of <code>FilterOutputStream</code> should provide a more efficient
95      * implementation of this method.
96      *
97      * @param b the data.
98      * @param off the start offset in the data.
99      * @param len the number of bytes to write.
100      * @exception IOException if an I/O error occurs.
101      * @see java.io.FilterOutputStream#write(int)
102      */

103     public void write(byte b[], int off, int len) throws IOException JavaDoc {
104     if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
105         throw new IndexOutOfBoundsException JavaDoc();
106
107     for (int i = 0 ; i < len ; i++) {
108         write(b[off + i]);
109     }
110     }
111
112     /**
113      * Flushes this output stream and forces any buffered output bytes
114      * to be written out to the stream.
115      * <p>
116      * The <code>flush</code> method of <code>FilterOutputStream</code>
117      * calls the <code>flush</code> method of its underlying output stream.
118      *
119      * @exception IOException if an I/O error occurs.
120      * @see java.io.FilterOutputStream#out
121      */

122     public void flush() throws IOException JavaDoc {
123     out.flush();
124     }
125
126     /**
127      * Closes this output stream and releases any system resources
128      * associated with the stream.
129      * <p>
130      * The <code>close</code> method of <code>FilterOutputStream</code>
131      * calls its <code>flush</code> method, and then calls the
132      * <code>close</code> method of its underlying output stream.
133      *
134      * @exception IOException if an I/O error occurs.
135      * @see java.io.FilterOutputStream#flush()
136      * @see java.io.FilterOutputStream#out
137      */

138     public void close() throws IOException JavaDoc {
139     try {
140       flush();
141     } catch (IOException JavaDoc ignored) {
142     }
143     out.close();
144     }
145 }
146
Popular Tags