Revision 38431
branches/v2_0_0_prep/extensions/org.gvsig.installer/org.gvsig.installer.lib/org.gvsig.installer.lib.impl/src/main/java/org/gvsig/installer/lib/impl/utils/Download.java | ||
---|---|---|
34 | 34 |
import org.slf4j.Logger; |
35 | 35 |
import org.slf4j.LoggerFactory; |
36 | 36 |
|
37 |
import org.gvsig.tools.ToolsLocator; |
|
37 | 38 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
38 | 39 |
import org.gvsig.tools.task.SimpleTaskStatus; |
40 |
import org.gvsig.tools.task.TaskStatusManager; |
|
39 | 41 |
|
40 | 42 |
/** |
41 | 43 |
* @author gvSIG Team |
... | ... | |
45 | 47 |
public class Download extends AbstractMonitorableTask { |
46 | 48 |
|
47 | 49 |
private static final Logger LOG = LoggerFactory.getLogger(Download.class); |
48 |
private boolean isMyTaskStatus; |
|
50 |
|
|
51 |
/** |
|
52 |
* If file size provided by URLConnection is smaller than this, |
|
53 |
* then the downloaded file size will always be accepted (unit: bytes) |
|
54 |
*/ |
|
55 |
private static final int FILE_SIZE_ESTIMATED_MINIMUM = 10000; |
|
56 |
|
|
57 |
/** |
|
58 |
* Downloaded file size will be accepted if it is bigger than this ratio |
|
59 |
* multiplied by the estimated file size provided by URLConnection |
|
60 |
* (unit: bytes) |
|
61 |
*/ |
|
62 |
private static final double FILE_SIZE_ESTIMATED_RATIO = 0.85; |
|
63 |
|
|
64 |
private boolean selfCreatedTaskStatus = true; |
|
49 | 65 |
|
50 | 66 |
/** |
51 | 67 |
* @param taskName |
52 | 68 |
*/ |
53 | 69 |
public Download(SimpleTaskStatus taskStatus) { |
54 | 70 |
this(); |
71 |
|
|
72 |
/* |
|
73 |
* constructor without params is adding |
|
74 |
* itself to manager, so we remove it |
|
75 |
*/ |
|
76 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
|
77 |
manager.remove(getTaskStatus()); |
|
78 |
|
|
79 |
/* |
|
80 |
* The received taskstatus is being managed by somebody else, |
|
81 |
* we don not add it to the manager |
|
82 |
*/ |
|
55 | 83 |
this.taskStatus = taskStatus; |
56 |
isMyTaskStatus = false;
|
|
84 |
selfCreatedTaskStatus = false;
|
|
57 | 85 |
} |
58 | 86 |
|
59 | 87 |
public Download() { |
88 |
/** |
|
89 |
* this call is adding the task status to the manager |
|
90 |
*/ |
|
60 | 91 |
super("Downloading..."); |
61 |
isMyTaskStatus = true; |
|
62 | 92 |
} |
63 | 93 |
|
64 | 94 |
public File downloadFile(URL url, String defaultFileName) |
... | ... | |
102 | 132 |
BufferedOutputStream bos = new BufferedOutputStream( |
103 | 133 |
new FileOutputStream(localFile)); |
104 | 134 |
|
105 |
this.taskStatus.setRangeOfValues(0, connection.getContentLength()); |
|
135 |
int expected_size = connection.getContentLength(); |
|
136 |
this.taskStatus.setRangeOfValues(0, expected_size); |
|
106 | 137 |
try { |
107 | 138 |
byte[] data = new byte[1024]; |
108 | 139 |
int count = 0; |
109 |
long totalCount = 0; // total bytes readed
|
|
140 |
long totalCount = 0; // total bytes read |
|
110 | 141 |
while ((count = bis.read(data, 0, 1024)) >= 0) { |
142 |
|
|
111 | 143 |
bos.write(data, 0, count); |
112 | 144 |
totalCount += count; |
113 |
|
|
145 |
|
|
114 | 146 |
this.taskStatus.setCurValue(totalCount); |
115 | 147 |
|
116 | 148 |
if (this.taskStatus.isCancellationRequested()) { |
117 |
return null;
|
|
149 |
break;
|
|
118 | 150 |
} |
119 | 151 |
} |
120 |
if (isMyTaskStatus) { |
|
152 |
|
|
153 |
if (selfCreatedTaskStatus) { |
|
121 | 154 |
this.taskStatus.terminate(); |
122 | 155 |
this.taskStatus.remove(); |
123 | 156 |
} |
157 |
|
|
158 |
// out from the read loop |
|
159 |
// perhaps it was cancelled: |
|
160 |
if (this.taskStatus.isCancellationRequested()) { |
|
161 |
return null; |
|
162 |
} |
|
163 |
// check real size = expected size: |
|
164 |
if (!acceptableFileSize(localFile, expected_size)) { |
|
165 |
throw new IOException("Bad download file size (" |
|
166 |
+ localFile.length() |
|
167 |
+ " / " |
|
168 |
+ expected_size |
|
169 |
+ ")"); |
|
170 |
} |
|
171 |
|
|
172 |
// everything seems to be OK |
|
124 | 173 |
return localFile; |
125 | 174 |
} finally { |
126 | 175 |
bis.close(); |
... | ... | |
193 | 242 |
return null; |
194 | 243 |
|
195 | 244 |
} |
245 |
|
|
246 |
/** |
|
247 |
* {@link URLConnection}'s method getContentLength() does not give |
|
248 |
* exact size. |
|
249 |
* |
|
250 |
* @return whether the size is acceptable |
|
251 |
*/ |
|
252 |
public static boolean acceptableFileSize(File f, int estimated_size) { |
|
253 |
|
|
254 |
if (f == null) { |
|
255 |
return false; |
|
256 |
} |
|
257 |
|
|
258 |
if (estimated_size == -1) { |
|
259 |
// -1 means unknown |
|
260 |
return true; |
|
261 |
} |
|
262 |
|
|
263 |
if (estimated_size < FILE_SIZE_ESTIMATED_MINIMUM) { |
|
264 |
return true; |
|
265 |
} |
|
266 |
|
|
267 |
return f.length() > (FILE_SIZE_ESTIMATED_RATIO * estimated_size); |
|
268 |
} |
|
196 | 269 |
} |
Also available in: Unified diff