From 1c0874dabedee1eac0174c6e99885be92843f161 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 31 Oct 2012 16:56:23 +0100 Subject: [PATCH] Created FileNameGenerator class --- .../antennapod/storage/DownloadRequester.java | 13 ++++--- .../antennapod/util/FileNameGenerator.java | 36 +++++++++++++++++++ .../antennapod/util/NumberGenerator.java | 21 ----------- 3 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 src/de/danoeh/antennapod/util/FileNameGenerator.java delete mode 100644 src/de/danoeh/antennapod/util/NumberGenerator.java diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index 928b923fd..d977b4a3e 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -14,7 +14,7 @@ import de.danoeh.antennapod.feed.FeedFile; import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.service.download.DownloadService; -import de.danoeh.antennapod.util.NumberGenerator; +import de.danoeh.antennapod.util.FileNameGenerator; import de.danoeh.antennapod.util.URLChecker; public class DownloadRequester { @@ -94,7 +94,8 @@ public class DownloadRequester { } } - public void downloadMedia(Context context, FeedMedia feedmedia) throws DownloadRequestException { + public void downloadMedia(Context context, FeedMedia feedmedia) + throws DownloadRequestException { if (feedFileValid(feedmedia)) { download(context, feedmedia, new File(getMediafilePath(context, feedmedia), @@ -199,7 +200,8 @@ public class DownloadRequester { } public String getFeedfileName(Feed feed) { - return "feed-" + NumberGenerator.generateLong(feed.getDownload_url()); + return "feed-" + + FileNameGenerator.generateFileName(feed.getDownload_url()); } public String getImagefilePath(Context context) @@ -209,7 +211,8 @@ public class DownloadRequester { } public String getImagefileName(FeedImage image) { - return "image-" + NumberGenerator.generateLong(image.getDownload_url()); + return "image-" + + FileNameGenerator.generateFileName(image.getDownload_url()); } public String getMediafilePath(Context context, FeedMedia media) @@ -217,7 +220,7 @@ public class DownloadRequester { File externalStorage = getExternalFilesDirOrThrowException( context, MEDIA_DOWNLOADPATH - + NumberGenerator.generateLong(media.getItem() + + FileNameGenerator.generateFileName(media.getItem() .getFeed().getTitle()) + "/"); return externalStorage.toString(); } diff --git a/src/de/danoeh/antennapod/util/FileNameGenerator.java b/src/de/danoeh/antennapod/util/FileNameGenerator.java new file mode 100644 index 000000000..3bc193080 --- /dev/null +++ b/src/de/danoeh/antennapod/util/FileNameGenerator.java @@ -0,0 +1,36 @@ +package de.danoeh.antennapod.util; + +import java.util.Arrays; + +/** Generates valid filenames for a given string. */ +public class FileNameGenerator { + + private static final char[] ILLEGAL_CHARACTERS = { '/', '\\', '?', '%', + '*', ':', '|', '"', '<', '>' }; + static { + Arrays.sort(ILLEGAL_CHARACTERS); + } + + private FileNameGenerator() { + + } + + /** + * This method will return a new string that doesn't contain any illegal + * characters of the given string. + */ + public static String generateFileName(String string) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < string.length(); i++) { + char c = string.charAt(i); + if (Arrays.binarySearch(ILLEGAL_CHARACTERS, c) < 0) { + builder.append(c); + } + } + return builder.toString(); + } + + public static long generateLong(final String str) { + return str.hashCode(); + } +} diff --git a/src/de/danoeh/antennapod/util/NumberGenerator.java b/src/de/danoeh/antennapod/util/NumberGenerator.java deleted file mode 100644 index ff89180e1..000000000 --- a/src/de/danoeh/antennapod/util/NumberGenerator.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.danoeh.antennapod.util; - - -/**Utility class for creating numbers.*/ -public final class NumberGenerator { - /** Class shall not be instantiated.*/ - private NumberGenerator() { - } - - /**Logging tag.*/ - private static final String TAG = "NumberGenerator"; - - /** Takes a string and generates a random value out of - * the hash-value of that string. - * @param strSeed The string to take for the return value - * @return The generated random value - * */ - public static long generateLong(final String strSeed) { - return strSeed.hashCode(); - } -}