KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > NoCloseOutputStream


1 /*
2  * Streams that have a different close mechanism.
3  * Copyright (C) 2003 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.io.*;
21
22 /**
23  * An output stream with a close method with no effect.
24  * More information about this class is available from <a target="_top" HREF=
25  * "http://ostermiller.org/utils/NoCloseStream.html">ostermiller.org</a>.
26  * <p>
27  * This class is designed to wrap a normal output stream
28  * so that it can be passed to methods that write to it
29  * and may erroneously close it. This class is a workaround
30  * when the method cannot be modified because it is in a
31  * library.
32  *
33  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
34  * @since ostermillerutils 1.01.00
35  */

36 public class NoCloseOutputStream extends OutputStream implements NoCloseStream {
37
38     /**
39      * The output stream that is being protected.
40      * All methods should be forwarded to it,
41      * except for the close method, which should
42      * do nothing. The reallyClose method should
43      * actually close this stream.
44      *
45      * @since ostermillerutils 1.01.00
46      */

47     protected OutputStream out;
48
49     /**
50      * Protect a new output stream.
51      *
52      * @param out The output stream that is being protected.
53      *
54      * @since ostermillerutils 1.01.00
55      */

56     public NoCloseOutputStream(OutputStream out){
57         this.out = out;
58     }
59
60     /**
61      * {@inheritDoc}
62      */

63     public void write(int b) throws IOException {
64         out.write(b);
65     }
66
67     /**
68      * {@inheritDoc}
69      */

70     public void write(byte[] b) throws IOException {
71         out.write(b);
72     }
73
74     /**
75      * {@inheritDoc}
76      */

77     public void write(byte[] b, int off, int len) throws IOException {
78         out.write(b, off, len);
79     }
80
81     /**
82      * {@inheritDoc}
83      */

84     public void flush() throws IOException {
85         out.flush();
86     }
87
88     /**
89      * Has no effect.
90      *
91      * @see #reallyClose()
92      *
93      * @since ostermillerutils 1.01.00
94      */

95     public void close() throws IOException {
96     }
97
98     /**
99      * {@inheritDoc}
100      */

101     public void reallyClose() throws IOException {
102         out.close();
103     }
104 }
105
Popular Tags