3
3
import java .io .BufferedReader ;
4
4
import java .io .File ;
5
5
import java .io .FileInputStream ;
6
+ import java .io .FileNotFoundException ;
6
7
import java .io .FileOutputStream ;
7
8
import java .io .FileReader ;
8
9
import java .io .FileWriter ;
@@ -191,27 +192,15 @@ protected List<Section> loadDataFromInternet() throws IOException {
191
192
List <Section > sections = fp .getSections ();
192
193
193
194
// now that we've finished parsing, we can save the temp copy
194
- Boolean result = tmpFile .renameTo (cacheFile );
195
+ if (cacheFile .exists ()) {
196
+ cacheFile .delete ();
197
+ }
195
198
196
- if (!result ) {
199
+ if (!tmpFile . renameTo ( cacheFile ) ) {
197
200
// was across filesystems or target exists, or something else.
198
201
// Try other another way
199
202
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4017593
200
- InputStream inStream = new FileInputStream (tmpFile );
201
- OutputStream outStream = new FileOutputStream (cacheFile );
202
-
203
- byte [] buffer = new byte [4096 ];
204
-
205
- int length ;
206
- while ((length = inStream .read (buffer )) > 0 ) {
207
- outStream .write (buffer , 0 , length );
208
- }
209
-
210
- inStream .close ();
211
- outStream .close ();
212
-
213
- // delete the original file
214
- tmpFile .delete ();
203
+ copyFile (tmpFile , cacheFile );
215
204
}
216
205
217
206
return sections ;
@@ -230,6 +219,30 @@ protected List<Section> loadDataFromInternet() throws IOException {
230
219
}
231
220
}
232
221
222
+ /**
223
+ * Copy source file to destination
224
+ *
225
+ * @param src
226
+ * @param dest
227
+ *
228
+ * @throws FileNotFoundException
229
+ * @throws IOException
230
+ */
231
+ protected void copyFile (File src , File dest ) throws FileNotFoundException , IOException {
232
+ InputStream inStream = new FileInputStream (src );
233
+ OutputStream outStream = new FileOutputStream (dest );
234
+
235
+ byte [] buffer = new byte [4096 ];
236
+
237
+ int length ;
238
+ while ((length = inStream .read (buffer )) > 0 ) {
239
+ outStream .write (buffer , 0 , length );
240
+ }
241
+
242
+ inStream .close ();
243
+ outStream .close ();
244
+ }
245
+
233
246
/**
234
247
* Gets the current version from user-agent-string.info
235
248
*
0 commit comments