I am developing a new feature for my android app to enable data backup and restore. I am using XML files to backup data. This is a piece of code that sets encoding for an output file:
XmlSerializer serializer = Xml.newSerializer();
FileWriter fileWriter = new FileWriter(file, false);
serializer.setOutput(fileWriter);
serializer.startDocument("UTF-8", true);
[... Write data to the file....]
This is how I try to import data from an XML file. First, I check if the encoding is correct:
XmlPullParser parser = Xml.newPullParser();
FileReader reader = new FileReader(file);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(reader);
if(!"UTF-8".equals(parser.getInputEncoding())) {
throw new IOException("Incorrect file encoding");
}
[... Read data from the file....]
And here I'm running into a problem. This code works fine on Android 2.3.3(both a device and an emulator), the encoding is correctly detected as "UTF-8". But on API11+ versions(Honeycomb, ICS, JB) the exception is thrown. When I run this in debug mode I can see that parser.getInputEncoding() returns null
. I checked the actual XML files produced on 2.3.3 and later versions and they have exactly the same headers: <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
. Why does getInputEncoding() return null on API11+?
Additional findings:
I have discovered that there is a way to correctly detect file encoding on API11+ devices using FileInputStream
instead of FileReader
like this:
XmlPullParser parser = Xml.newPullParser();
FileInputStream stream = new FileInputStream(file);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(stream, null);
if(!"UTF-8".equals(parser.getInputEncoding())) {
throw new IOException("Incorrect file encoding");
}
[... Read data from the file....]
In this case getInputEncoding() properly detects UTF-8 encoding on API11+ emulators and devices, but it returns null on 2.3.3. So for now I can insert a fork in code to use FileReader on API11+ and FileInputStream on pre-API11:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
parser.setInput(stream, null);
} else {
parser.setInput(reader);
}
But what's the proper way to check encoding with XmlPullParser.getInputEncoding()? Why are different versions of Android behave differently depending on which one I use: FileInputStream or FileReader?