1 /*
2 * dpkg - Debian Package library and the Debian Package Maven plugin
3 * (c) Copyright 2015 Gerrit Hohl
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 package net.sourceforge.javadpkg.io.impl;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23
24 import net.sourceforge.javadpkg.io.CloseHandler;
25
26
27 /**
28 * <p>
29 * An {@link OutputStream} which can't be closed.
30 * </p>
31 * <p>
32 * Calls of the {@link #close()} method don't have any effect on the underlying
33 * stream.
34 * </p>
35 *
36 * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
37 * @version <b>1.0</b>, 31.12.2015 by Gerrit Hohl
38 */
39 public class UncloseableOutputStream extends OutputStream {
40
41
42 /** The underlying stream. */
43 private OutputStream out;
44 /** The close handler (optional). */
45 private CloseHandler closeHandler;
46
47
48 /**
49 * <p>
50 * Creates a stream.
51 * </p>
52 *
53 * @param out
54 * The underlying stream.
55 * @throws IllegalArgumentException
56 * If the underlying stream is <code>null</code>.
57 */
58 public UncloseableOutputStream(OutputStream out) {
59 super();
60
61 if (out == null)
62 throw new IllegalArgumentException("Argument out is null.");
63
64 this.out = out;
65 this.closeHandler = null;
66 }
67
68
69 /**
70 * <p>
71 * Creates a stream.
72 * </p>
73 *
74 * @param out
75 * The underlying stream.
76 * @param closeHandler
77 * The close handler.
78 * @throws IllegalArgumentException
79 * If any of the parameters are <code>null</code>.
80 */
81 public UncloseableOutputStream(OutputStream out, CloseHandler closeHandler) {
82 super();
83
84 if (out == null)
85 throw new IllegalArgumentException("Argument out is null.");
86 if (closeHandler == null)
87 throw new IllegalArgumentException("Argument closeHandler is null.");
88
89 this.out = out;
90 this.closeHandler = closeHandler;
91 }
92
93
94 @Override
95 public void write(int b) throws IOException {
96 this.out.write(b);
97 }
98
99
100 @Override
101 public void write(byte[] b) throws IOException {
102 this.out.write(b);
103 }
104
105
106 @Override
107 public void write(byte[] b, int off, int len) throws IOException {
108 this.out.write(b, off, len);
109 }
110
111
112 @Override
113 public void flush() throws IOException {
114 this.out.flush();
115 }
116
117
118 @Override
119 public void close() throws IOException {
120 this.out.flush();
121 if (this.closeHandler != null)
122 this.closeHandler.closed();
123 }
124
125
126 }