diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2013-09-14 14:17:56 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2013-09-14 14:17:56 +0200 |
commit | 3d5d4c46327244244003982f71aa627d1407d5c3 (patch) | |
tree | 8b8fa0f0d95108cee03257f53bbb2b84350a17ce | |
parent | 371f6e7fe5bfc68fb208374ca108893a14b44100 (diff) | |
download | cgeo-3d5d4c46327244244003982f71aa627d1407d5c3.zip cgeo-3d5d4c46327244244003982f71aa627d1407d5c3.tar.gz cgeo-3d5d4c46327244244003982f71aa627d1407d5c3.tar.bz2 |
refactoring: do not use default encoding
For headers and mount point parsing, we use UTF-8, which is almost
certainly the default on the platform, even if only the ASCII plane
is likely to be used.
-rw-r--r-- | main/src/cgeo/geocaching/files/FileParser.java | 2 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/files/LocalStorage.java | 19 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/utils/IOUtils.java | 4 |
3 files changed, 17 insertions, 8 deletions
diff --git a/main/src/cgeo/geocaching/files/FileParser.java b/main/src/cgeo/geocaching/files/FileParser.java index f979d74..bafd858 100644 --- a/main/src/cgeo/geocaching/files/FileParser.java +++ b/main/src/cgeo/geocaching/files/FileParser.java @@ -51,7 +51,7 @@ public abstract class FileParser { protected static StringBuilder readStream(InputStream is, CancellableHandler progressHandler) throws IOException { final StringBuilder buffer = new StringBuilder(); ProgressInputStream progressInputStream = new ProgressInputStream(is); - final BufferedReader input = new BufferedReader(new InputStreamReader(progressInputStream)); + final BufferedReader input = new BufferedReader(new InputStreamReader(progressInputStream, "UTF-8")); try { String line; diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index 05713cc..e5ba8c0 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -8,7 +8,6 @@ import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.Header; import ch.boye.httpclientandroidlib.HttpResponse; - import org.apache.commons.lang3.StringUtils; import android.os.Environment; @@ -21,11 +20,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; @@ -204,7 +205,13 @@ public final class LocalStorage { 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); + } } } @@ -224,7 +231,7 @@ public final class LocalStorage { 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]; @@ -410,10 +417,10 @@ public final class LocalStorage { storages.add(new File(extStorage)); File file = new File("/system/etc/vold.fstab"); 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) { diff --git a/main/src/cgeo/geocaching/utils/IOUtils.java b/main/src/cgeo/geocaching/utils/IOUtils.java index df90da3..8e483d3 100644 --- a/main/src/cgeo/geocaching/utils/IOUtils.java +++ b/main/src/cgeo/geocaching/utils/IOUtils.java @@ -1,5 +1,7 @@ package cgeo.geocaching.utils; +import org.eclipse.jdt.annotation.Nullable; + import java.io.Closeable; import java.io.IOException; @@ -9,7 +11,7 @@ final public class IOUtils { // utility class } - public static void closeQuietly(final Closeable closeable) { + public static void closeQuietly(@Nullable final Closeable closeable) { if (closeable != null) { try { closeable.close(); |