Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb1e82e700348ec0fcaedb34316f043dd9f15fb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.transforms;

import java.io.IOException;
import java.io.InputStream;

/**
 * An input stream that is based off of another stream.  
 * This other stream is provided as needed by an {@link InputStreamProvider} so that the underlying stream is not eagerly loaded.
 */
public class LazyInputStream extends InputStream {

	private InputStreamProvider provider;

	private InputStream original = null;

	/**
	 * Construct a new lazy stream based off the given provider.
	 * @param provider the input stream provider.  Must not be <code>null</code>.
	 */
	public LazyInputStream(InputStreamProvider provider) {
		if (provider == null)
			throw new IllegalArgumentException();
		this.provider = provider;
	}

	private void initOriginal() throws IOException {
		if (original == null)
			original = provider.getInputStream();
	}

	public int available() throws IOException {
		initOriginal();
		return original.available();
	}

	public void close() throws IOException {
		initOriginal();
		original.close();
	}

	public boolean equals(Object obj) {
		try {
			initOriginal();
			return original.equals(obj);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public int hashCode() {
		try {
			initOriginal();
			return original.hashCode();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public void mark(int readlimit) {
		try {
			initOriginal();
			original.mark(readlimit);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public boolean markSupported() {
		try {
			initOriginal();
			return original.markSupported();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public int read() throws IOException {
		initOriginal();
		return original.read();
	}

	public int read(byte[] b, int off, int len) throws IOException {
		initOriginal();
		return original.read(b, off, len);
	}

	public int read(byte[] b) throws IOException {
		initOriginal();
		return original.read(b);
	}

	public void reset() throws IOException {
		initOriginal();
		original.reset();
	}

	public long skip(long n) throws IOException {
		initOriginal();
		return original.skip(n);
	}

	public String toString() {
		try {
			initOriginal();
			return original.toString();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * An interface to be implemented by clients that wish to utilize {@link LazyInputStream}s.  
	 * The implementation of this interface should defer obtaining the desired input stream until absolutely necessary.
	 */
	public static interface InputStreamProvider {
		/**
		 * Return the input stream.
		 * @return the input stream
		 * @throws IOException thrown if there is an issue obtaining the stream
		 */
		InputStream getInputStream() throws IOException;
	}
}

Back to the top