diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2015-02-11 18:29:48 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2015-02-11 18:31:21 +0100 |
| commit | c5d3092ce9136a6d5c2eb436e2b0e0719859d3d1 (patch) | |
| tree | 0f6bdfbb98c287d091c2076b09168e65675f1048 /main/src/cgeo | |
| parent | 0147c238112b40c119b45b3328fe3e74ef10e5d2 (diff) | |
| download | cgeo-c5d3092ce9136a6d5c2eb436e2b0e0719859d3d1.zip cgeo-c5d3092ce9136a6d5c2eb436e2b0e0719859d3d1.tar.gz cgeo-c5d3092ce9136a6d5c2eb436e2b0e0719859d3d1.tar.bz2 | |
Use File where appropriate
Also, use more efficient concatenation by treating the simple case
explicitely.
Diffstat (limited to 'main/src/cgeo')
| -rw-r--r-- | main/src/cgeo/geocaching/export/GpxExport.java | 3 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/DebugUtils.java | 7 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/FileUtils.java | 24 |
3 files changed, 17 insertions, 17 deletions
diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java index b3201f8..af564da 100644 --- a/main/src/cgeo/geocaching/export/GpxExport.java +++ b/main/src/cgeo/geocaching/export/GpxExport.java @@ -117,8 +117,7 @@ public class GpxExport extends AbstractExport { private File getExportFile() { final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); - final Date now = new Date(); - return FileUtils.getUniqueNamedFile(Settings.getGpxExportDir() + File.separatorChar + "export_" + fileNameDateFormat.format(now) + ".gpx"); + return FileUtils.getUniqueNamedFile(new File(Settings.getGpxExportDir(), "export_" + fileNameDateFormat.format(new Date()) + ".gpx")); } @Override diff --git a/main/src/cgeo/geocaching/utils/DebugUtils.java b/main/src/cgeo/geocaching/utils/DebugUtils.java index 07aac64..1f95e7c 100644 --- a/main/src/cgeo/geocaching/utils/DebugUtils.java +++ b/main/src/cgeo/geocaching/utils/DebugUtils.java @@ -22,15 +22,14 @@ public class DebugUtils { public static void createMemoryDump(final @NonNull Context context) { try { - final Date now = new Date(); final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyy-MM-dd_hh-mm", Locale.US); - File file = FileUtils.getUniqueNamedFile(Environment.getExternalStorageDirectory().getPath() - + File.separatorChar + "cgeo_dump_" + fileNameDateFormat.format(now) + ".hprof"); + final File file = FileUtils.getUniqueNamedFile(new File(Environment.getExternalStorageDirectory(), + "cgeo_dump_" + fileNameDateFormat.format(new Date()) + ".hprof")); android.os.Debug.dumpHprofData(file.getPath()); Toast.makeText(context, context.getString(R.string.init_memory_dumped, file.getAbsolutePath()), Toast.LENGTH_LONG).show(); ShareUtils.share(context, file, R.string.init_memory_dump); - } catch (IOException e) { + } catch (final IOException e) { Log.e("createMemoryDump", e); } } diff --git a/main/src/cgeo/geocaching/utils/FileUtils.java b/main/src/cgeo/geocaching/utils/FileUtils.java index 979820c..0875308 100644 --- a/main/src/cgeo/geocaching/utils/FileUtils.java +++ b/main/src/cgeo/geocaching/utils/FileUtils.java @@ -86,18 +86,20 @@ public final class FileUtils { * </ul> * which does not yet exist. */ - public static File getUniqueNamedFile(final String baseNameAndPath) { - String extension = StringUtils.substringAfterLast(baseNameAndPath, "."); - String pathName = StringUtils.substringBeforeLast(baseNameAndPath, "."); - int number = 1; - while (new File(getNumberedFileName(pathName, extension, number)).exists()) { - number++; + public static File getUniqueNamedFile(final File file) { + if (!file.exists()) { + return file; } - return new File(getNumberedFileName(pathName, extension, number)); - } - - private static String getNumberedFileName(String pathName, String extension, int number) { - return pathName + (number > 1 ? "_" + Integer.toString(number) : "") + "." + extension; + final String baseNameAndPath = file.getPath(); + final String prefix = StringUtils.substringBeforeLast(baseNameAndPath, ".") + "_"; + final String extension = "." + StringUtils.substringAfterLast(baseNameAndPath, "."); + for (int i = 1; i < Integer.MAX_VALUE; i++) { + final File numbered = new File(prefix + i + extension); + if (!numbered.exists()) { + return numbered; + } + } + throw new IllegalStateException("Unable to generate a non-existing file name"); } /** |
