KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Streams that are 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  * A writer 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 writer
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 NoCloseWriter extends Writer implements NoCloseStream {
37
38     /**
39      * The writer 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 Writer out;
48
49     /**
50      * Protect a new writer.
51      *
52      * @param out The writer that is being protected.
53      */

54     public NoCloseWriter(Writer out){
55         this.out = out;
56     }
57
58     /**
59      * {@inheritDoc}
60      */

61     public void write(int c) throws IOException {
62         out.write(c);
63     }
64
65     /**
66      * {@inheritDoc}
67      */

68     public void write(char[] cbuf) throws IOException {
69         out.write(cbuf);
70     }
71
72     /**
73      * {@inheritDoc}
74      */

75     public void write(char[] cbuf, int off, int len) throws IOException {
76         out.write(cbuf, off, len);
77     }
78
79     /**
80      * {@inheritDoc}
81      */

82     public void write(String JavaDoc str) throws IOException {
83         out.write(str);
84     }
85
86     /**
87      * {@inheritDoc}
88      */

89     public void write(String JavaDoc str, int off, int len) throws IOException {
90         out.write(str, off, len);
91     }
92
93     /**
94      * {@inheritDoc}
95      */

96     public void flush() throws IOException {
97         out.flush();
98     }
99
100     /**
101      * Has no effect.
102      *
103      * @see #reallyClose()
104      *
105      * @since ostermillerutils 1.01.00
106      */

107     public void close() throws IOException {
108     }
109
110     /**
111      * {@inheritDoc}
112      */

113     public void reallyClose() throws IOException {
114         out.close();
115     }
116 }
117
Popular Tags