@@ -65,14 +65,15 @@ public enum Status {
65
65
private final URL downloadUrl ;
66
66
67
67
private final File outputFile ;
68
- private InputStream stream = null ;
68
+ private final boolean allowCache ;
69
69
private Exception error ;
70
70
71
- public FileDownloader (URL url , File file ) {
72
- downloadUrl = url ;
73
- outputFile = file ;
74
- downloaded = 0 ;
75
- initialSize = 0 ;
71
+ public FileDownloader (URL url , File file , boolean allowCache ) {
72
+ this .downloadUrl = url ;
73
+ this .outputFile = file ;
74
+ this .allowCache = allowCache ;
75
+ this .downloaded = 0 ;
76
+ this .initialSize = 0 ;
76
77
}
77
78
78
79
public long getInitialSize () {
@@ -118,11 +119,11 @@ public void setStatus(Status status) {
118
119
}
119
120
120
121
121
- public void download (boolean noResume , boolean allowCache ) throws InterruptedException {
122
+ public void download (boolean noResume ) throws InterruptedException {
122
123
if ("file" .equals (downloadUrl .getProtocol ())) {
123
124
saveLocalFile ();
124
125
} else {
125
- downloadFile (noResume , allowCache );
126
+ downloadFile (noResume );
126
127
}
127
128
}
128
129
@@ -136,66 +137,101 @@ private void saveLocalFile() {
136
137
}
137
138
}
138
139
139
- private void downloadFile (boolean noResume , boolean allowCache ) throws InterruptedException {
140
- RandomAccessFile randomAccessOutputFile = null ;
140
+ private void downloadFile (boolean noResume ) throws InterruptedException {
141
141
142
142
try {
143
143
setStatus (Status .CONNECTING );
144
144
145
145
final Optional <FileDownloaderCache .FileCached > fileCached = FileDownloaderCache .getFileCached (downloadUrl );
146
-
147
146
if (fileCached .isPresent () && fileCached .get ().isNotChange ()) {
148
- try {
149
- final Optional <File > fileFromCache =
150
- fileCached .get ().getFileFromCache ();
151
- if (fileFromCache .isPresent ()) {
152
- log .info ("No need to download using cached file: {}" , fileCached .get ());
153
- FileUtils .copyFile (fileFromCache .get (), outputFile );
154
- setStatus (Status .COMPLETE );
155
- return ;
147
+ final Optional <File > fileFromCache = getFileCached (fileCached .get ());
148
+ if (fileFromCache .isPresent ()) {
149
+ // Copy the cached file in the destination file
150
+ FileUtils .copyFile (fileFromCache .get (), outputFile );
151
+ } else {
152
+ downloadToFile (noResume );
153
+
154
+ if (allowCache ) {
155
+ fileCached .get ().updateCacheFile (outputFile );
156
156
} else {
157
- log .info (
158
- "The file in the cache is not in the path or the md5 validation failed: path={}, file exist={}, md5 validation={}" ,
159
- fileCached .get ().getLocalPath (), fileCached .get ().exists (), fileCached .get ().md5Check ());
157
+ log .info ("The file {} was not cached because allow cache is false" , downloadUrl );
160
158
}
161
- } catch (Exception e ) {
162
- log .warn (
163
- "Cannot get the file from the cache, will be downloaded a new one " , e );
164
159
}
165
- } else {
166
- log .info ("The file is change {}" , fileCached );
167
160
}
161
+ setStatus (Status .COMPLETE );
162
+
163
+ } catch (InterruptedException e ) {
164
+ setStatus (Status .CANCELLED );
165
+ // lets InterruptedException go up to the caller
166
+ throw e ;
167
+
168
+ } catch (SocketTimeoutException e ) {
169
+ setStatus (Status .CONNECTION_TIMEOUT_ERROR );
170
+ setError (e );
171
+ log .error ("The request went in socket timeout" , e );
168
172
169
- initialSize = outputFile .length ();
170
- if (noResume && initialSize > 0 ) {
171
- // delete file and restart downloading
172
- Files .deleteIfExists (outputFile .toPath ());
173
- initialSize = 0 ;
173
+ } catch (Exception e ) {
174
+ setStatus (Status .ERROR );
175
+ setError (e );
176
+ log .error ("The request stop" , e );
177
+ }
178
+
179
+ }
180
+
181
+ private Optional <File > getFileCached (FileDownloaderCache .FileCached fileCached ) {
182
+
183
+ try {
184
+ final Optional <File > fileFromCache =
185
+ fileCached .getFileFromCache ();
186
+ if (fileFromCache .isPresent ()) {
187
+ log .info ("No need to download using cached file: {}" , fileCached );
188
+ return fileFromCache ;
189
+ } else {
190
+ log .info (
191
+ "The file in the cache is not in the path or the md5 validation failed: path={}, file exist={}, md5 validation={}" ,
192
+ fileCached .getLocalPath (), fileCached .exists (), fileCached .md5Check ());
174
193
}
194
+ } catch (Exception e ) {
195
+ log .warn (
196
+ "Cannot get the file from the cache, will be downloaded a new one " , e );
197
+ }
198
+ log .info ("The file is change {}" , fileCached );
199
+ return Optional .empty ();
200
+ }
201
+
202
+ private void downloadToFile (boolean noResume ) throws Exception {
203
+ initialSize = outputFile .length ();
204
+ if (noResume && initialSize > 0 ) {
205
+ // delete file and restart downloading
206
+ Files .deleteIfExists (outputFile .toPath ());
207
+ initialSize = 0 ;
208
+ }
209
+
210
+ final HttpURLConnection connection = new HttpConnectionManager (downloadUrl )
211
+ .makeConnection ((c ) -> setDownloaded (0 ));
212
+ final int resp = connection .getResponseCode ();
213
+
214
+ if (resp < 200 || resp >= 300 ) {
215
+ Files .deleteIfExists (outputFile .toPath ());
216
+ throw new IOException ("Received invalid http status code from server: " + resp );
217
+ }
218
+
219
+ RandomAccessFile randomAccessOutputFile = null ;
220
+ InputStream stream = null ;
221
+ try {
175
222
// Open file and seek to the end of it
176
223
randomAccessOutputFile = new RandomAccessFile (outputFile , "rw" );
177
224
randomAccessOutputFile .seek (initialSize );
178
225
179
- final HttpURLConnection connection = new HttpConnectionManager (downloadUrl )
180
- .makeConnection ((c ) -> setDownloaded (0 ));
181
- final int resp = connection .getResponseCode ();
182
-
183
- if (resp < 200 || resp >= 300 ) {
184
- IOUtils .closeQuietly (randomAccessOutputFile );
185
- Files .deleteIfExists (outputFile .toPath ());
186
- throw new IOException ("Received invalid http status code from server: " + resp );
187
- }
188
-
189
226
// Check for valid content length.
190
227
long len = connection .getContentLength ();
191
228
if (len >= 0 ) {
192
229
setDownloadSize (len );
193
230
}
194
231
setStatus (Status .DOWNLOADING );
195
232
196
- synchronized (this ) {
197
- stream = connection .getInputStream ();
198
- }
233
+ stream = connection .getInputStream ();
234
+
199
235
byte [] buffer = new byte [10240 ];
200
236
while (status == Status .DOWNLOADING ) {
201
237
int read = stream .read (buffer );
@@ -217,35 +253,12 @@ private void downloadFile(boolean noResume, boolean allowCache) throws Interrupt
217
253
}
218
254
// Set the cache whe it finish to download the file
219
255
IOUtils .closeQuietly (randomAccessOutputFile );
220
- if (fileCached .isPresent () && allowCache ) {
221
- fileCached .get ().updateCacheFile (outputFile );
222
- }
223
- if (!allowCache ) {
224
- log .info ("The file {} was not cached because allow cache is false" , downloadUrl );
225
- }
226
- setStatus (Status .COMPLETE );
227
- } catch (InterruptedException e ) {
228
- setStatus (Status .CANCELLED );
229
- // lets InterruptedException go up to the caller
230
- throw e ;
231
-
232
- } catch (SocketTimeoutException e ) {
233
- setStatus (Status .CONNECTION_TIMEOUT_ERROR );
234
- setError (e );
235
- log .error ("The request went in socket timeout" , e );
236
-
237
- } catch (Exception e ) {
238
- setStatus (Status .ERROR );
239
- setError (e );
240
- log .error ("The request stop" , e );
241
256
242
257
} finally {
243
258
IOUtils .closeQuietly (randomAccessOutputFile );
244
-
245
- synchronized (this ) {
246
- IOUtils .closeQuietly (stream );
247
- }
259
+ IOUtils .closeQuietly (stream );
248
260
}
261
+
249
262
}
250
263
251
264
private void setError (Exception e ) {
0 commit comments