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.InputStream;
23
24 import net.sourceforge.javadpkg.io.DataSource;
25
26 /**
27 * <p>
28 * A {@link DataSource} 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 DataStreamSource implements DataSource {
35
36
37 /** The stream. */
38 private InputStream in;
39 /**
40 * <p>
41 * The public stream which is returned by the {@link #getInputStream()}
42 * method.
43 * </p>
44 */
45 private UncloseableInputStream publicIn;
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 source.
55 * </p>
56 *
57 * @param in
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 DataStreamSource(InputStream in, String name, boolean closeable) {
67 super();
68
69 if (in == null)
70 throw new IllegalArgumentException("Argument in is null.");
71 if (name == null)
72 throw new IllegalArgumentException("Argument name is null.");
73
74 this.in = in;
75 this.name = name;
76 this.closeable = closeable;
77
78 this.createPublicInputStream();
79 }
80
81
82 @Override
83 public String getName() {
84 return this.name;
85 }
86
87
88 @Override
89 public long getLength() {
90 return -1;
91 }
92
93
94 @Override
95 public boolean isResettable() {
96 return false;
97 }
98
99
100 @Override
101 public void reset() throws IOException {
102 throw new IOException("Source |" + this.name + "| doesn't support a reset.");
103 }
104
105
106 /**
107 * <p>
108 * Creates the {@link InputStream} which is returned by the
109 * {@link #getInputStream()} method.
110 * </p>
111 */
112 private void createPublicInputStream() {
113 this.publicIn = new UncloseableInputStream(this.in, new DelegateCloseHandler(this));
114 }
115
116
117 @Override
118 public InputStream getInputStream() throws IOException {
119 return this.publicIn;
120 }
121
122
123 @Override
124 public void close() throws IOException {
125 try {
126 if ((this.in != null) && this.closeable) {
127 this.in.close();
128 }
129 } finally {
130 this.publicIn = null;
131 this.in = null;
132 }
133 }
134
135
136 }