diff options
Diffstat (limited to 'main/src/cgeo/geocaching/files/LocalStorage.java')
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocalStorage.java | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index fc82409..edbecf6 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -1,15 +1,16 @@ package cgeo.geocaching.files; -import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.utils.CryptUtils; import cgeo.geocaching.utils.FileUtils; -import cgeo.geocaching.utils.IOUtils; 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; import android.os.Environment; @@ -21,11 +22,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.Reader; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; @@ -35,6 +38,7 @@ import java.util.List; */ public final class LocalStorage { + private static final String FILE_SYSTEM_TABLE_PATH = "/system/etc/vold.fstab"; public static final String HEADER_LAST_MODIFIED = "last-modified"; public static final String HEADER_ETAG = "etag"; @@ -86,7 +90,7 @@ public final class LocalStorage { private static File getInternalStorageBase() { if (internalStorageBase == null) { // A race condition will do no harm as the operation is idempotent. No need to synchronize. - internalStorageBase = cgeoapplication.getInstance().getApplicationContext().getFilesDir().getParentFile(); + internalStorageBase = CgeoApplication.getInstance().getApplicationContext().getFilesDir().getParentFile(); } return internalStorageBase; } @@ -99,7 +103,14 @@ public final class LocalStorage { * @return the file extension, including the leading dot, or the empty string if none could be determined */ static String getExtension(final String url) { - final String urlExt = StringUtils.substringAfterLast(url, "."); + String urlExt; + if (url.startsWith("data:")) { + // "data:image/png;base64,i53…" -> ".png" + urlExt = StringUtils.substringAfter(StringUtils.substringBefore(url, ";"), "/"); + } else { + // "http://example.com/foo/bar.png" -> ".png" + urlExt = StringUtils.substringAfterLast(url, "."); + } return urlExt.length() >= 1 && urlExt.length() <= 4 ? "." + urlExt : ""; } @@ -198,13 +209,19 @@ public final class LocalStorage { return false; } - private static void saveHeader(final String name, final HttpResponse response, final File baseFile) { + private static void saveHeader(final String name, @Nullable final HttpResponse response, final File baseFile) { final Header header = response != null ? response.getFirstHeader(name) : null; final File file = filenameForHeader(baseFile, name); if (header == null) { FileUtils.deleteIgnoringFailure(file); } else { - saveToFile(new ByteArrayInputStream(header.getValue().getBytes()), file); + try { + saveToFile(new ByteArrayInputStream(header.getValue().getBytes("UTF-8")), file); + } catch (final UnsupportedEncodingException e) { + // Do not try to display the header in the log message, as our default encoding is + // likely to be UTF-8 and it will fail as well. + Log.e("LocalStorage.saveHeader: unable to decode header", e); + } } } @@ -219,12 +236,13 @@ public final class LocalStorage { * the name of the cached resource * @param name * the name of the header ("etag" or "last-modified") - * @return null if no value has been cached, the value otherwise + * @return the cached value, or <tt>null</tt> if none has been cached */ + @Nullable public static String getSavedHeader(final File baseFile, final String name) { try { final File file = filenameForHeader(baseFile, name); - final FileReader f = new FileReader(file); + final Reader f = new InputStreamReader(new FileInputStream(file), "UTF-8"); try { // No header will be more than 256 bytes final char[] value = new char[256]; @@ -408,12 +426,12 @@ public final class LocalStorage { String extStorage = Environment.getExternalStorageDirectory().getAbsolutePath(); List<File> storages = new ArrayList<File>(); storages.add(new File(extStorage)); - File file = new File("/system/etc/vold.fstab"); + File file = new File(FILE_SYSTEM_TABLE_PATH); if (file.canRead()) { - FileReader fr = null; + Reader fr = null; BufferedReader br = null; try { - fr = new FileReader(file); + fr = new InputStreamReader(new FileInputStream(file), "UTF-8"); br = new BufferedReader(fr); String s = br.readLine(); while (s != null) { |
