diff options
Diffstat (limited to 'main/src/cgeo/geocaching/files/LocalStorage.java')
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocalStorage.java | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index d57c247..626f6e6 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -7,7 +7,6 @@ import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.Header; import ch.boye.httpclientandroidlib.HttpResponse; - import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.Nullable; @@ -122,7 +121,7 @@ public final class LocalStorage { * the geocode * @return the cache directory */ - public static File getStorageDir(final String geocode) { + public static File getStorageDir(@Nullable final String geocode) { return storageDir(getStorage(), geocode); } @@ -134,11 +133,11 @@ public final class LocalStorage { * the geocode * @return the cache directory */ - private static File getStorageSecDir(final String geocode) { + private static File getStorageSecDir(@Nullable final String geocode) { return storageDir(getStorageSec(), geocode); } - private static File storageDir(final File base, final String geocode) { + private static File storageDir(final File base, @Nullable final String geocode) { return new File(base, StringUtils.defaultIfEmpty(geocode, "_others")); } @@ -155,7 +154,7 @@ public final class LocalStorage { * true if an url was given, false if a file name was given * @return the file */ - public static File getStorageFile(final String geocode, final String fileNameOrUrl, final boolean isUrl, final boolean createDirs) { + public static File getStorageFile(@Nullable final String geocode, final String fileNameOrUrl, final boolean isUrl, final boolean createDirs) { return buildFile(getStorageDir(geocode), fileNameOrUrl, isUrl, createDirs); } @@ -276,15 +275,18 @@ public final class LocalStorage { return false; } + try { try { - final FileOutputStream fos = new FileOutputStream(targetFile); + final File tempFile = File.createTempFile("download", null, targetFile.getParentFile()); + final FileOutputStream fos = new FileOutputStream(tempFile); final boolean written = copy(inputStream, fos); fos.close(); - if (!written) { - FileUtils.deleteIgnoringFailure(targetFile); + if (written) { + return tempFile.renameTo(targetFile); } - return written; + FileUtils.deleteIgnoringFailure(tempFile); + return false; } finally { IOUtils.closeQuietly(inputStream); } |
