title: What's up with AssetFileDescriptor.getFileDescriptor()?
body: <p>I have an uncompressed binary file in res/raw that I was reading this way:</p>
  
  <pre><code>public byte[] file2Bytes (int rid) {
      byte[] buffer = null;
      try {
          AssetFileDescriptor afd = res.openRawResourceFd(rid);
          FileInputStream in = new FileInputStream(afd.getFileDescriptor());
          int len = (int)afd.getLength();
          buffer = new byte[len];
          in.read(buffer, 0, len);
          in.close();
      } catch (Exception ex) {
          Log.w(ACTNAME, "file2Bytes() fail\n"+ex.toString());
          return null;
      }
      return buffer;
  }  
  </code></pre>
  
  <p>However, <code>buffer</code> did not contain what it was supposed to.  The source file is 1024 essentially random bytes (a binary key).  But <code>buffer</code>, when written out and examined, was not the same. Amongst unprintable bytes at beginning appeared <em>"res/layout/main.xml"</em> (the literal path) and then further down, part of the text content of another file from res/raw. <strong>O_O?</strong></p>
  
  <p>Exasperated after a while, I tried:</p>
  
  <pre><code>            AssetFileDescriptor afd = res.openRawResourceFd(rid);
              //FileInputStream in = new FileInputStream(afd.getFileDescriptor());
              FileInputStream in = afd.createInputStream(); 
  </code></pre>
  
  <p>Presto, I got the content correctly -- this is easily reproducible.</p>
  
  <p>So the <a href="http://developer.android.com/reference/android/content/res/AssetFileDescriptor.html#getFileDescriptor%28%29" rel="nofollow">relevant API docs</a> read:</p>
  
  <blockquote>
    <p><strong>public FileDescriptor getFileDescriptor ()</strong></p>
    
    <p>Returns the FileDescriptor that can be used to read the data in the
    file.</p>
    
    <p><strong>public FileInputStream createInputStream ()</strong></p>
    
    <p>Create and return a new auto-close input stream for this asset. This
    will either return a full asset
    AssetFileDescriptor.AutoCloseInputStream, or an underlying
    ParcelFileDescriptor.AutoCloseInputStream depending on whether the the
    object represents a complete file or sub-section of a file. You should
    only call this once for a particular asset.</p>
  </blockquote>
  
  <p>Why would a FileInputStream() constructed from getFileDescriptor() end up with garbage whereas createInputStream() gives proper access?</p>