title: Different behavior of XmlPullParser.getInputEncoding() on API11+ and pre-API11 versions of Android
body: <p>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:</p>
  
  <pre><code>XmlSerializer serializer = Xml.newSerializer();
  FileWriter fileWriter = new FileWriter(file, false);
  serializer.setOutput(fileWriter);
  serializer.startDocument("UTF-8", true);
  [... Write data to the file....]
  </code></pre>
  
  <p>This is how I try to import data from an XML file. First, I check if the encoding is correct:</p>
  
  <pre><code>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....]
  </code></pre>
  
  <p>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 <code>null</code>. I checked the actual XML files produced on 2.3.3 and later versions and they have exactly the same headers: <code>&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt;</code>. <strong>Why does getInputEncoding() return null on API11+?</strong></p>
  
  <p><strong>Additional findings:</strong></p>
  
  <p>I have discovered that there is a way to correctly detect file encoding on API11+ devices using <code>FileInputStream</code> instead of <code>FileReader</code> like this:</p>
  
  <pre><code>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....]
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>if(Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) {
      parser.setInput(stream, null);
  } else {
      parser.setInput(reader);
  }
  </code></pre>
  
  <p><strong>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?</strong></p>