Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractRemoteResource |
|
| 0.0;0 | ||||
AbstractRemoteResource$ProgressInputStream |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 | * | |
5 | * This code is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 only, as | |
7 | * published by the Free Software Foundation. Sun designates this | |
8 | * particular file as subject to the "Classpath" exception as provided | |
9 | * by Sun in the LICENSE file that accompanied this code. | |
10 | * | |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * version 2 for more details (a copy is included in the LICENSE file that | |
15 | * accompanied this code). | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License version | |
18 | * 2 along with this work; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | * | |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
23 | * have any questions. | |
24 | */ | |
25 | ||
26 | package com.sun.javafx.runtime.async; | |
27 | ||
28 | import java.io.IOException; | |
29 | import java.io.InputStream; | |
30 | import java.io.BufferedInputStream; | |
31 | import java.io.InterruptedIOException; | |
32 | import java.net.HttpURLConnection; | |
33 | import java.net.URL; | |
34 | ||
35 | /** | |
36 | * Abstract base class for representing remote resources identified by a URL. Subclasses may plug in arbitrary | |
37 | * post-processing on the stream to turn it into the desired result. Manages progress indication if the remote resource | |
38 | * provides a content-length header. | |
39 | * | |
40 | * @author Brian Goetz | |
41 | */ | |
42 | public abstract class AbstractRemoteResource<T> extends AbstractAsyncOperation<T> { | |
43 | ||
44 | protected final String url; | |
45 | protected int fileSize; | |
46 | ||
47 | protected AbstractRemoteResource(String url, AsyncOperationListener<T> listener) { | |
48 | 0 | super(listener); |
49 | 0 | this.url = url; |
50 | 0 | } |
51 | ||
52 | protected abstract T processStream(InputStream stream) throws IOException; | |
53 | ||
54 | public T call() throws IOException { | |
55 | 0 | URL u = new URL(url); |
56 | 0 | HttpURLConnection conn = (HttpURLConnection) u.openConnection(); |
57 | 0 | fileSize = conn.getContentLength(); |
58 | 0 | setProgressMax(fileSize); |
59 | ||
60 | 0 | InputStream stream = new ProgressInputStream(conn.getInputStream()); |
61 | try { | |
62 | 0 | return processStream(stream); |
63 | } | |
64 | finally { | |
65 | 0 | stream.close(); |
66 | } | |
67 | } | |
68 | ||
69 | protected class ProgressInputStream extends BufferedInputStream { | |
70 | 0 | public ProgressInputStream(InputStream in) { |
71 | 0 | super(in); |
72 | 0 | } |
73 | ||
74 | @Override | |
75 | public synchronized int read() throws IOException { | |
76 | 0 | if (Thread.currentThread().isInterrupted()) |
77 | 0 | throw new InterruptedIOException(); |
78 | 0 | int ch = super.read(); |
79 | 0 | addProgress(1); |
80 | 0 | return ch; |
81 | } | |
82 | ||
83 | @Override | |
84 | public synchronized int read(byte b[], int off, int len) throws IOException { | |
85 | 0 | if (Thread.currentThread().isInterrupted()) |
86 | 0 | throw new InterruptedIOException(); |
87 | 0 | int bytes = super.read(b, off, len); |
88 | 0 | addProgress(bytes); |
89 | 0 | return bytes; |
90 | } | |
91 | ||
92 | @Override | |
93 | public int read(byte b[]) throws IOException { | |
94 | 0 | if (Thread.currentThread().isInterrupted()) |
95 | 0 | throw new InterruptedIOException(); |
96 | 0 | int bytes = super.read(b); |
97 | 0 | addProgress(bytes); |
98 | 0 | return bytes; |
99 | } | |
100 | } | |
101 | } |