1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.astrogrid.filestore.common ;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.FileInputStream;
27
28 import java.net.URL ;
29 import java.net.URLConnection ;
30 import java.net.HttpURLConnection ;
31 import java.net.MalformedURLException ;
32
33 import org.apache.commons.logging.Log ;
34 import org.apache.commons.logging.LogFactory ;
35
36 import org.astrogrid.filestore.common.transfer.mock.Handler ;
37
38 /***
39 * A wrapper for GET transfer streams.
40 *
41 */
42 public class FileStoreInputStream
43 extends InputStream
44 {
45
46 /***
47 * Our debug logger.
48 *
49 */
50 private static Log log = LogFactory.getLog(FileStoreInputStream.class);
51
52 /***
53 * Create a FileStoreOutputStream from a string URL.
54 * @param string A string representation of the target URL.
55 * @throws MalformedURLException
56 *
57 */
58 public FileStoreInputStream(String string)
59 throws MalformedURLException
60 {
61 this(
62 new URL(
63 string
64 )
65 );
66 }
67
68 /***
69 * Create a FileStoreInputStream from a URL.
70 * @param url The target URL to connect to.
71 *
72 */
73 public FileStoreInputStream(URL url)
74 {
75 if (null == url)
76 {
77 throw new IllegalArgumentException(
78 "Null url"
79 );
80 }
81 this.url = url ;
82 }
83
84 /***
85 * Our url.
86 *
87 */
88 private URL url ;
89
90 /***
91 * Our URL connection.
92 *
93 */
94 private URLConnection conn ;
95
96 /***
97 * Our HTTP connection.
98 *
99 */
100 private HttpURLConnection http ;
101
102 /***
103 * Our underlying stream.
104 *
105 */
106 private InputStream stream ;
107
108 /***
109 * Open our connection.
110 * @todo Better exception handling ....
111 * @todo Add support for other protocols, ftp etc ...
112 *
113 */
114 public void open()
115 throws IOException
116 {
117 log.debug("");
118 log.debug("FileStoreInputStream.open()");
119 log.debug(" URL : " + url);
120
121
122 if ("http".equals(url.getProtocol()))
123 {
124 log.debug(" Handling http URL");
125 this.http = (HttpURLConnection) url.openConnection() ;
126 this.http.setAllowUserInteraction(false);
127 this.http.setDoInput(true);
128
129 this.http.setUseCaches(false);
130 this.http.setRequestMethod("GET");
131 this.http.setRequestProperty("User-Agent", this.getClass().getName());
132
133
134 this.http.connect();
135
136
137 this.stream = http.getInputStream() ;
138 }
139
140
141 else if ("file".equals(url.getProtocol()))
142 {
143 log.debug(" Handling file URL");
144 log.debug(" Path : " + url.getPath());
145
146
147 this.stream = new FileInputStream(
148 url.getPath()
149 );
150 }
151
152
153 else {
154 log.debug(" Handling generic URL");
155
156
157 this.conn = url.openConnection() ;
158 this.stream = this.conn.getInputStream() ;
159 }
160 log.debug(" PASS : Stream open");
161 }
162
163 /***
164 * Close our connection.
165 * @todo Better exception handling ....
166 *
167 */
168 public void close()
169 throws IOException
170 {
171 log.debug("");
172 log.debug("FileStoreInputStream.close()");
173 log.debug(" URL : " + url);
174 if (null != this.stream)
175 {
176
177
178 this.stream.close();
179 log.debug(" PASS : Stream closed");
180 }
181 else {
182 throw new IOException(
183 "Stream not open"
184 );
185 }
186 }
187
188 /***
189 * InputStream method ...
190 *
191 */
192 public int available()
193 throws IOException
194 {
195 if (null != this.stream)
196 {
197 return this.stream.available();
198 }
199 else {
200 throw new IOException(
201 "Stream not open"
202 );
203 }
204 }
205
206 /***
207 * InputStream method ...
208 *
209 */
210 public void mark(int limit)
211 {
212 if (null != this.stream)
213 {
214 this.stream.mark(limit);
215 }
216 }
217
218 /***
219 * InputStream method ...
220 *
221 */
222 public boolean markSupported()
223 {
224 if (null != this.stream)
225 {
226 return this.stream.markSupported();
227 }
228 else {
229 return false ;
230 }
231 }
232
233 /***
234 * InputStream method ...
235 *
236 */
237 public int read()
238 throws IOException
239 {
240 if (null != this.stream)
241 {
242 return this.stream.read();
243 }
244 else {
245 throw new IOException(
246 "Stream not open"
247 );
248 }
249 }
250
251 /***
252 * InputStream method ...
253 *
254 */
255 public int read(byte[] b)
256 throws IOException
257 {
258 if (null != this.stream)
259 {
260 return this.stream.read(b);
261 }
262 else {
263 throw new IOException(
264 "Stream not open"
265 );
266 }
267 }
268
269 /***
270 * InputStream method ...
271 *
272 */
273 public int read(byte[] b, int off, int len)
274 throws IOException
275 {
276 if (null != this.stream)
277 {
278 return this.stream.read(b, off, len);
279 }
280 else {
281 throw new IOException(
282 "Stream not open"
283 );
284 }
285 }
286
287 /***
288 * InputStream method ...
289 *
290 */
291 public void reset()
292 throws IOException
293 {
294 if (null != this.stream)
295 {
296 this.stream.reset();
297 }
298 else {
299 throw new IOException(
300 "Stream not open"
301 );
302 }
303 }
304
305 /***
306 * InputStream method ...
307 *
308 */
309 public long skip(long n)
310 throws IOException
311 {
312 if (null != this.stream)
313 {
314 return this.stream.skip(n);
315 }
316 else {
317 throw new IOException(
318 "Stream not open"
319 );
320 }
321 }
322
323 }