Skip to content

Commit 029838c

Browse files
committed
delete the cached file before renaming; fall back to copy
1 parent 7a58370 commit 029838c

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/main/java/cz/mallat/uasparser/OnlineUpdater.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.BufferedReader;
44
import java.io.File;
55
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
67
import java.io.FileOutputStream;
78
import java.io.FileReader;
89
import java.io.FileWriter;
@@ -191,27 +192,15 @@ protected List<Section> loadDataFromInternet() throws IOException {
191192
List<Section> sections = fp.getSections();
192193

193194
// 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+
}
195198

196-
if (!result) {
199+
if (!tmpFile.renameTo(cacheFile)) {
197200
// was across filesystems or target exists, or something else.
198201
// Try other another way
199202
// 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);
215204
}
216205

217206
return sections;
@@ -230,6 +219,30 @@ protected List<Section> loadDataFromInternet() throws IOException {
230219
}
231220
}
232221

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+
233246
/**
234247
* Gets the current version from user-agent-string.info
235248
*

0 commit comments

Comments
 (0)