View Javadoc
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.CloseHandler;
25  
26  
27  /**
28   * <p>
29   * An {@link InputStream} 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 UncloseableInputStream extends InputStream {
40  
41  
42  	/** The underlying stream. */
43  	private InputStream		in;
44  	/** The close handler (optional). */
45  	private CloseHandler	closeHandler;
46  
47  
48  	/**
49  	 * <p>
50  	 * Creates a stream.
51  	 * </p>
52  	 *
53  	 * @param in
54  	 *            The underlying stream.
55  	 * @throws IllegalArgumentException
56  	 *             If the underlying stream is <code>null</code>.
57  	 */
58  	public UncloseableInputStream(InputStream in) {
59  		super();
60  		
61  		if (in == null)
62  			throw new IllegalArgumentException("Argument in is null.");
63  
64  		this.in = in;
65  		this.closeHandler = null;
66  	}
67  
68  
69  	/**
70  	 * <p>
71  	 * Creates a stream.
72  	 * </p>
73  	 *
74  	 * @param in
75  	 *            The underlying stream.
76  	 * @param closeHandler
77  	 *            The handler which is called when the {@link #close()} method
78  	 *            is called.
79  	 * @throws IllegalArgumentException
80  	 *             If any of the parameters are <code>null</code>.
81  	 */
82  	public UncloseableInputStream(InputStream in, CloseHandler closeHandler) {
83  		super();
84  		
85  		if (in == null)
86  			throw new IllegalArgumentException("Argument in is null.");
87  		if (closeHandler == null)
88  			throw new IllegalArgumentException("Argument closeHandler is null.");
89  
90  		this.in = in;
91  		this.closeHandler = closeHandler;
92  	}
93  
94  
95  	@Override
96  	public int read() throws IOException {
97  		return this.in.read();
98  	}
99  
100 
101 	@Override
102 	public int read(byte[] b) throws IOException {
103 		return this.in.read(b);
104 	}
105 
106 
107 	@Override
108 	public int read(byte[] b, int off, int len) throws IOException {
109 		return this.in.read(b, off, len);
110 	}
111 
112 
113 	@Override
114 	public long skip(long n) throws IOException {
115 		return this.in.skip(n);
116 	}
117 
118 
119 	@Override
120 	public int available() throws IOException {
121 		return this.in.available();
122 	}
123 
124 
125 	@Override
126 	public void close() throws IOException {
127 		if (this.closeHandler != null)
128 			this.closeHandler.closed();
129 	}
130 
131 
132 	@Override
133 	public synchronized void mark(int readlimit) {
134 		this.in.mark(readlimit);
135 	}
136 
137 
138 	@Override
139 	public synchronized void reset() throws IOException {
140 		this.in.reset();
141 	}
142 
143 
144 	@Override
145 	public boolean markSupported() {
146 		return this.in.markSupported();
147 	}
148 
149 
150 }