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.DataTarget;
25
26 /**
27 * <p>
28 * A {@link DataTarget} implementation based on a stream.
29 * </p>
30 *
31 * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
32 * @version <b>1.0</b>, 31.12.2015 by Gerrit Hohl
33 */
34 public class DataStreamTarget implements DataTarget {
35
36
37 /** The stream. */
38 private OutputStream out;
39 /**
40 * <p>
41 * The public stream which is returned by the {@link #getOutputStream()}
42 * method.
43 * </p>
44 */
45 private UncloseableOutputStream publicOut;
46 /** The name of the source. */
47 private String name;
48 /** The flag if the {@link #close()} method will close the stream. */
49 private boolean closeable;
50
51
52 /**
53 * <p>
54 * Creates a target.
55 * </p>
56 *
57 * @param out
58 * The stream.
59 * @param name
60 * The name of the source.
61 * @param closeable
62 * The flag if the {@link #close()} method will close the stream.
63 * @throws IllegalArgumentException
64 * If any of the parameters are <code>null</code>.
65 */
66 public DataStreamTarget(OutputStream out, String name, boolean closeable) {
67 super();
68
69 if (out == null)
70 throw new IllegalArgumentException("Argument out is null.");
71 if (name == null)
72 throw new IllegalArgumentException("Argument name is null.");
73
74 this.out = out;
75 this.publicOut = null;
76 this.name = name;
77 this.closeable = closeable;
78
79 this.createPublicOutputStream();
80 }
81
82
83 @Override
84 public String getName() {
85 return this.name;
86 }
87
88
89 /**
90 * <p>
91 * Creates the {@link OutputStream} which is returned by the
92 * {@link #getOutputStream()} method.
93 * </p>
94 */
95 private void createPublicOutputStream() {
96 this.publicOut = new UncloseableOutputStream(this.out, new DelegateCloseHandler(this));
97 }
98
99
100 @Override
101 public OutputStream getOutputStream() throws IOException {
102 return this.publicOut;
103 }
104
105
106 @Override
107 public void close() throws IOException {
108 try {
109 if ((this.out != null) && this.closeable) {
110 this.out.close();
111 }
112 } finally {
113 this.publicOut = null;
114 this.out = null;
115 }
116 }
117
118
119 }